Spaces:
Runtime error
Runtime error
Update Preprocess
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
|
|
3 |
import cv2
|
4 |
import pytesseract
|
5 |
from fastapi import FastAPI
|
|
|
6 |
|
7 |
#pytesseract.pytesseract.tesseract_cmd = r'./Tesseract-OCR/tesseract.exe'
|
8 |
|
@@ -11,8 +12,34 @@ print(os.popen(f'cat /etc/issue').read())
|
|
11 |
print(os.popen(f'apt search tesseract').read())
|
12 |
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
|
|
|
|
16 |
|
17 |
class Lines:
|
18 |
def __init__(self,x,y,w,h,text):
|
@@ -25,11 +52,10 @@ def TextLineBox(img):
|
|
25 |
lineboxes = []
|
26 |
|
27 |
#read image
|
28 |
-
img =
|
29 |
-
|
30 |
|
31 |
### Cofig
|
32 |
-
configname = r' --oem 3 --psm ' + str(
|
33 |
|
34 |
#### Text for testing
|
35 |
texttest = pytesseract.image_to_string(img ,config=configname)
|
@@ -90,6 +116,7 @@ with gr.Blocks (theme="ParityError/Anime" , css="#SUBMIT {background-color: #cd
|
|
90 |
with gr.Row():
|
91 |
with gr.Column():
|
92 |
input = gr.Image()
|
|
|
93 |
text_output = gr.Text(label="Result Text")
|
94 |
file_output = gr.File()
|
95 |
with gr.Row():
|
@@ -100,7 +127,7 @@ with gr.Blocks (theme="ParityError/Anime" , css="#SUBMIT {background-color: #cd
|
|
100 |
with gr.Column():
|
101 |
image_output = gr.Image()
|
102 |
|
103 |
-
submit_btn.click(TextLineBox, input, outputs= [text_output, image_output, ] )
|
104 |
download_btn.click(Download, text_output, outputs= file_output )
|
105 |
clear_btn.click(lambda: [None,None,None], inputs=None, outputs= [text_output, file_output, image_output])
|
106 |
|
|
|
3 |
import cv2
|
4 |
import pytesseract
|
5 |
from fastapi import FastAPI
|
6 |
+
import numpy as np
|
7 |
|
8 |
#pytesseract.pytesseract.tesseract_cmd = r'./Tesseract-OCR/tesseract.exe'
|
9 |
|
|
|
12 |
print(os.popen(f'apt search tesseract').read())
|
13 |
|
14 |
|
15 |
+
def PreprocessIMG(image):
|
16 |
+
|
17 |
+
# Convert to grayscale
|
18 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
19 |
+
# Apply Canny edge detection
|
20 |
+
image = cv2.GaussianBlur(image,(3,3),0)
|
21 |
+
edges = cv2.Canny(image, 90, 120, apertureSize=3)
|
22 |
+
# Apply Hough line transform to detect lines
|
23 |
+
lines = cv2.HoughLines(edges, 1, np.pi/180, threshold=300)
|
24 |
+
|
25 |
+
# Remove lines from the image
|
26 |
+
for rho, theta in lines[:, 0]:
|
27 |
+
a = np.cos(theta)
|
28 |
+
b = np.sin(theta)
|
29 |
+
x0 = a * rho
|
30 |
+
y0 = b * rho
|
31 |
+
x1 = int(x0 + 1000 * (-b))
|
32 |
+
y1 = int(y0 + 1000 * (a))
|
33 |
+
x2 = int(x0 - 1000 * (-b))
|
34 |
+
y2 = int(y0 - 1000 * (a))
|
35 |
+
cv2.line(image, (x1, y1), (x2, y2), (255, 255, 255), 2)
|
36 |
+
|
37 |
+
return image
|
38 |
+
|
39 |
|
40 |
+
|
41 |
+
|
42 |
+
def TextLineBox(img, engine):
|
43 |
|
44 |
class Lines:
|
45 |
def __init__(self,x,y,w,h,text):
|
|
|
52 |
lineboxes = []
|
53 |
|
54 |
#read image
|
55 |
+
img = PreprocessIMG(img)
|
|
|
56 |
|
57 |
### Cofig
|
58 |
+
configname = r' --oem 3 --psm ' + str(engine) + ' -l eng'
|
59 |
|
60 |
#### Text for testing
|
61 |
texttest = pytesseract.image_to_string(img ,config=configname)
|
|
|
116 |
with gr.Row():
|
117 |
with gr.Column():
|
118 |
input = gr.Image()
|
119 |
+
engine_input = gr.Text(label="Engine Mode Number")
|
120 |
text_output = gr.Text(label="Result Text")
|
121 |
file_output = gr.File()
|
122 |
with gr.Row():
|
|
|
127 |
with gr.Column():
|
128 |
image_output = gr.Image()
|
129 |
|
130 |
+
submit_btn.click(TextLineBox, inputs=[input,engine_input,], outputs= [text_output, image_output, ] )
|
131 |
download_btn.click(Download, text_output, outputs= file_output )
|
132 |
clear_btn.click(lambda: [None,None,None], inputs=None, outputs= [text_output, file_output, image_output])
|
133 |
|