Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,19 +17,18 @@ from datasets import load_dataset, Image
|
|
17 |
from PIL import Image
|
18 |
from paddleocr import PaddleOCR
|
19 |
from save_data import flag
|
20 |
-
|
|
|
21 |
"""
|
22 |
Paddle OCR
|
23 |
"""
|
24 |
def ocr_with_paddle(img):
|
25 |
finaltext = ''
|
26 |
ocr = PaddleOCR(lang='en', use_angle_cls=True)
|
27 |
-
# img_path = 'exp.jpeg'
|
28 |
result = ocr.ocr(img)
|
29 |
-
|
30 |
for i in range(len(result[0])):
|
31 |
text = result[0][i][1][0]
|
32 |
-
finaltext += ' '+ text
|
33 |
return finaltext
|
34 |
|
35 |
"""
|
@@ -37,84 +36,88 @@ Keras OCR
|
|
37 |
"""
|
38 |
def ocr_with_keras(img):
|
39 |
output_text = ''
|
40 |
-
pipeline=keras_ocr.pipeline.Pipeline()
|
41 |
-
images=[keras_ocr.tools.read(img)]
|
42 |
-
predictions=pipeline.recognize(images)
|
43 |
-
first=predictions[0]
|
44 |
-
for text,box in first:
|
45 |
-
output_text += ' '+ text
|
46 |
return output_text
|
47 |
|
48 |
"""
|
49 |
easy OCR
|
50 |
"""
|
51 |
-
#
|
52 |
def get_grayscale(image):
|
53 |
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
54 |
|
55 |
# Thresholding or Binarization
|
56 |
def thresholding(src):
|
57 |
-
return cv2.threshold(src,127,255, cv2.THRESH_TOZERO)[1]
|
|
|
58 |
def ocr_with_easy(img):
|
59 |
-
gray_scale_image=get_grayscale(img)
|
60 |
thresholding(gray_scale_image)
|
61 |
-
cv2.imwrite('image.png',gray_scale_image)
|
62 |
-
reader = easyocr.Reader(['th','en'])
|
63 |
-
bounds = reader.readtext('image.png',paragraph="False",detail
|
64 |
bounds = ''.join(bounds)
|
65 |
return bounds
|
66 |
-
|
67 |
"""
|
68 |
Generate OCR
|
69 |
"""
|
70 |
-
def generate_ocr(Method,img):
|
71 |
-
|
72 |
text_output = ''
|
73 |
if (img).any():
|
74 |
-
|
75 |
-
image_id = 1
|
76 |
-
print("Method___________________",Method)
|
77 |
if Method == 'EasyOCR':
|
78 |
text_output = ocr_with_easy(img)
|
79 |
if Method == 'KerasOCR':
|
80 |
text_output = ocr_with_keras(img)
|
81 |
if Method == 'PaddleOCR':
|
82 |
text_output = ocr_with_paddle(img)
|
83 |
-
|
84 |
try:
|
85 |
-
flag(Method,text_output,img)
|
86 |
except Exception as e:
|
87 |
print(e)
|
88 |
-
|
|
|
|
|
|
|
89 |
else:
|
90 |
raise gr.Error("Please upload an image!!!!")
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
"""
|
99 |
Create user interface for OCR demo
|
100 |
"""
|
101 |
|
102 |
-
# image = gr.Image(shape=(300, 300))
|
103 |
image = gr.Image()
|
104 |
-
method = gr.Radio(["PaddleOCR","EasyOCR", "KerasOCR"],value="PaddleOCR")
|
105 |
-
output = gr.Textbox(label="
|
106 |
|
107 |
demo = gr.Interface(
|
108 |
generate_ocr,
|
109 |
-
[method,image],
|
110 |
output,
|
111 |
-
title="Optical Character Recognition",
|
112 |
css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
|
113 |
-
article
|
114 |
<a href="mailto:[email protected]" target="_blank">[email protected]</a>
|
115 |
<p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
|
116 |
-
|
117 |
-
|
118 |
)
|
119 |
-
|
120 |
-
demo.launch()
|
|
|
17 |
from PIL import Image
|
18 |
from paddleocr import PaddleOCR
|
19 |
from save_data import flag
|
20 |
+
from transformers import pipeline # Importing the pipeline
|
21 |
+
|
22 |
"""
|
23 |
Paddle OCR
|
24 |
"""
|
25 |
def ocr_with_paddle(img):
|
26 |
finaltext = ''
|
27 |
ocr = PaddleOCR(lang='en', use_angle_cls=True)
|
|
|
28 |
result = ocr.ocr(img)
|
|
|
29 |
for i in range(len(result[0])):
|
30 |
text = result[0][i][1][0]
|
31 |
+
finaltext += ' ' + text
|
32 |
return finaltext
|
33 |
|
34 |
"""
|
|
|
36 |
"""
|
37 |
def ocr_with_keras(img):
|
38 |
output_text = ''
|
39 |
+
pipeline = keras_ocr.pipeline.Pipeline()
|
40 |
+
images = [keras_ocr.tools.read(img)]
|
41 |
+
predictions = pipeline.recognize(images)
|
42 |
+
first = predictions[0]
|
43 |
+
for text, box in first:
|
44 |
+
output_text += ' ' + text
|
45 |
return output_text
|
46 |
|
47 |
"""
|
48 |
easy OCR
|
49 |
"""
|
50 |
+
# grayscale image
|
51 |
def get_grayscale(image):
|
52 |
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
53 |
|
54 |
# Thresholding or Binarization
|
55 |
def thresholding(src):
|
56 |
+
return cv2.threshold(src, 127, 255, cv2.THRESH_TOZERO)[1]
|
57 |
+
|
58 |
def ocr_with_easy(img):
|
59 |
+
gray_scale_image = get_grayscale(img)
|
60 |
thresholding(gray_scale_image)
|
61 |
+
cv2.imwrite('image.png', gray_scale_image)
|
62 |
+
reader = easyocr.Reader(['th', 'en'])
|
63 |
+
bounds = reader.readtext('image.png', paragraph="False", detail=0)
|
64 |
bounds = ''.join(bounds)
|
65 |
return bounds
|
66 |
+
|
67 |
"""
|
68 |
Generate OCR
|
69 |
"""
|
70 |
+
def generate_ocr(Method, img):
|
|
|
71 |
text_output = ''
|
72 |
if (img).any():
|
73 |
+
print("Method___________________", Method)
|
|
|
|
|
74 |
if Method == 'EasyOCR':
|
75 |
text_output = ocr_with_easy(img)
|
76 |
if Method == 'KerasOCR':
|
77 |
text_output = ocr_with_keras(img)
|
78 |
if Method == 'PaddleOCR':
|
79 |
text_output = ocr_with_paddle(img)
|
80 |
+
|
81 |
try:
|
82 |
+
flag(Method, text_output, img)
|
83 |
except Exception as e:
|
84 |
print(e)
|
85 |
+
|
86 |
+
# Generate Text using FLAN-T5 model
|
87 |
+
text_gen = generate_text_with_flan_t5(text_output)
|
88 |
+
return text_gen
|
89 |
else:
|
90 |
raise gr.Error("Please upload an image!!!!")
|
91 |
+
|
92 |
+
"""
|
93 |
+
Text Generation using FLAN-T5
|
94 |
+
"""
|
95 |
+
def generate_text_with_flan_t5(input_text):
|
96 |
+
# Load the pre-trained FLAN-T5 model
|
97 |
+
pipe = pipeline("text2text-generation", model="google/flan-t5-large")
|
98 |
+
|
99 |
+
# Use the model to generate a response based on the OCR output
|
100 |
+
output = pipe(input_text)
|
101 |
+
return output[0]['generated_text']
|
102 |
+
|
103 |
|
104 |
"""
|
105 |
Create user interface for OCR demo
|
106 |
"""
|
107 |
|
|
|
108 |
image = gr.Image()
|
109 |
+
method = gr.Radio(["PaddleOCR", "EasyOCR", "KerasOCR"], value="PaddleOCR")
|
110 |
+
output = gr.Textbox(label="Generated Text")
|
111 |
|
112 |
demo = gr.Interface(
|
113 |
generate_ocr,
|
114 |
+
[method, image],
|
115 |
output,
|
116 |
+
title="Optical Character Recognition and Text Generation",
|
117 |
css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
|
118 |
+
article="""<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at
|
119 |
<a href="mailto:[email protected]" target="_blank">[email protected]</a>
|
120 |
<p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
|
|
|
|
|
121 |
)
|
122 |
+
|
123 |
+
demo.launch()
|