Spaces:
Sleeping
Sleeping
File size: 3,496 Bytes
14607dc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
import os
import gradio as gr
import cv2
import pytesseract
from fastapi import FastAPI
#pytesseract.pytesseract.tesseract_cmd = r'./Tesseract-OCR/tesseract.exe'
print(os.popen(f'cat /etc/debian_version').read())
print(os.popen(f'cat /etc/issue').read())
print(os.popen(f'apt search tesseract').read())
def TextLineBox(img):
class Lines:
def __init__(self,x,y,w,h,text):
self.x = x
self.y = y
self.w = w
self.h = h
self.text = text
lineboxes = []
#read image
img = cv2.GaussianBlur(img,(3,3),0)
### Cofig
configname = r' --oem 3 --psm ' + str(12) + ' -l eng'
#### Text for testing
texttest = pytesseract.image_to_string(img ,config=configname)
### Box of words
boxes = pytesseract.image_to_data(img, config=configname)
# print(boxes)
#slit box and concatenate into line
skip = 0
for b in boxes.splitlines():
## skip header
if (skip == 0):
skip = 1
continue
## get box of word in 1 object
b = b.split()
if (len(b) < 12): ## it is a space not a word
continue
#print(b)
x,y,w,h,text = int(b[6]),int(b[7]),int(b[8]),int(b[9]), b[11]
### Begin New line if the word having num_word is 1
if (int(b[5]) == 1):
lineboxes.append( Lines(x,y,w,h,text) )
### Next word inline
else:
lineboxes[-1].text += ' ' + text
if (x > lineboxes[-1].x):
lineboxes[-1].w = x - lineboxes[-1].x + w
if (y < lineboxes[-1].y):
lineboxes[-1].y = y
if (y+h > lineboxes[-1].y + lineboxes[-1].h):
lineboxes[-1].h = y+h - lineboxes[-1].y
#draw the box of WORD
cv2.rectangle(img, (x,y) , (w+x,y+h), (255,0,0), 2 )
return texttest,img
def Download(text):
with open("test.txt", "w") as file:
file.write(text)
return "test.txt"
with gr.Blocks (theme="ParityError/Anime" , css="#SUBMIT {background-color: #cdb4db} #DOWNLOAD {background-color: #a2d2ff}") as demo:
with gr.Row():
with gr.Column():
input = gr.Image()
text_output = gr.Text(label="Result Text")
file_output = gr.File()
with gr.Row():
submit_btn = gr.Button("SUBMIT" , elem_id="SUBMIT")
download_btn = gr.Button("DOWNLOAD", elem_id="DOWNLOAD")
clear_btn = gr.Button("CLEAR")
with gr.Column():
image_output = gr.Image()
submit_btn.click(TextLineBox, input, outputs= [text_output, image_output, ] )
download_btn.click(Download, text_output, outputs= file_output )
clear_btn.click(lambda: [None,None,None], inputs=None, outputs= [text_output, file_output, image_output])
demo.load(
None,
None,
_js="""
() => {
const params = new URLSearchParams(window.location.search);
if (!params.has('__theme')) {
params.set('__theme', 'dark');
window.location.search = params.toString();
}
}""",
)
demo.queue().launch()
# Cach 1 dung app Gradio (share = True)
# Cach 2 dung Mount_Gradio_app trong FASTAPI app
# Cach 3
# app = FastAPI()
# app = gr.mount_gradio_app(app, demo, path="/OCR" )
#bt.click(fn=None, _js="window.open('https://google.com', '_blank')") |