Spaces:
Runtime error
Runtime error
File size: 4,341 Bytes
8680c69 424fc11 fe7ba04 424fc11 31ac4b3 424fc11 31ac4b3 424fc11 fe7ba04 d49c34e fe7ba04 1eb8827 fe7ba04 424fc11 fe7ba04 424fc11 fe7ba04 424fc11 8522404 424fc11 fe7ba04 424fc11 fe7ba04 424fc11 05b83c8 424fc11 |
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
import os
import gradio as gr
import cv2
import pytesseract
from fastapi import FastAPI
import numpy as np
#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 PreprocessIMG(image):
# Convert to grayscale
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply Canny edge detection
image = cv2.GaussianBlur(image,(3,3),0)
edges = cv2.Canny(image, 90, 120, apertureSize=3)
# Apply Hough line transform to detect lines
lines = cv2.HoughLines(edges, 1, np.pi/180, threshold=300)
# Remove lines from the image
for rho, theta in lines[:, 0]:
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))
cv2.line(image, (x1, y1), (x2, y2), (255, 255, 255), 3)
return image
def TextLineBox(img, engine):
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 = PreprocessIMG(img)
### Cofig
configname = r' --oem 3 --psm ' + str(engine) + ' -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()
engine_input = gr.Text(label="Engine Mode Number")
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, inputs=[input,engine_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')") |