Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -16,8 +16,7 @@ import datasets
|
|
16 |
from datasets import load_dataset, Image
|
17 |
from PIL import Image
|
18 |
from paddleocr import PaddleOCR
|
19 |
-
|
20 |
-
|
21 |
"""
|
22 |
Paddle OCR
|
23 |
"""
|
@@ -63,58 +62,51 @@ def ocr_with_easy(img):
|
|
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 |
-
|
72 |
text_output = ''
|
73 |
-
if (
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
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 |
-
return text_output
|
89 |
else:
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
# print("Error in ocr generation ==>",e)
|
94 |
-
# text_output = "Something went wrong"
|
95 |
-
# return text_output
|
96 |
|
|
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
method = gr.Radio(["PaddleOCR","EasyOCR", "KerasOCR"],value="PaddleOCR")
|
105 |
output = gr.Textbox(label="Output")
|
106 |
|
107 |
demo = gr.Interface(
|
108 |
generate_ocr,
|
109 |
-
[method,
|
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 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
)
|
119 |
-
|
120 |
demo.launch(show_error=True)
|
|
|
16 |
from datasets import load_dataset, Image
|
17 |
from PIL import Image
|
18 |
from paddleocr import PaddleOCR
|
19 |
+
|
|
|
20 |
"""
|
21 |
Paddle OCR
|
22 |
"""
|
|
|
62 |
bounds = reader.readtext('image.png',paragraph="False",detail = 0)
|
63 |
bounds = ''.join(bounds)
|
64 |
return bounds
|
65 |
+
|
66 |
+
def generate_ocr(Method, file):
|
|
|
|
|
|
|
|
|
67 |
text_output = ''
|
68 |
+
if isinstance(file, bytes): # Handle file uploaded as bytes
|
69 |
+
file = io.BytesIO(file)
|
70 |
+
|
71 |
+
if file.name.endswith('.pdf'):
|
72 |
+
# Convert PDF to images
|
73 |
+
images = convert_from_path(file)
|
74 |
+
for img in images:
|
75 |
+
img_np = np.array(img)
|
76 |
+
text_output += generate_text_from_image(Method, img_np) + "\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
else:
|
78 |
+
# Handle image file
|
79 |
+
img_np = np.array(Image.open(file))
|
80 |
+
text_output = generate_text_from_image(Method, img_np)
|
|
|
|
|
|
|
81 |
|
82 |
+
return text_output
|
83 |
|
84 |
+
def generate_text_from_image(Method, img):
|
85 |
+
text_output = ''
|
86 |
+
if Method == 'EasyOCR':
|
87 |
+
text_output = ocr_with_easy(img)
|
88 |
+
elif Method == 'KerasOCR':
|
89 |
+
text_output = ocr_with_keras(img)
|
90 |
+
elif Method == 'PaddleOCR':
|
91 |
+
text_output = ocr_with_paddle(img)
|
92 |
+
return text_output
|
93 |
+
|
94 |
+
|
95 |
+
import gradio as gr
|
96 |
|
97 |
+
image_or_pdf = gr.File(label="Upload an image or PDF")
|
98 |
+
method = gr.Radio(["PaddleOCR", "EasyOCR", "KerasOCR"], value="PaddleOCR")
|
|
|
99 |
output = gr.Textbox(label="Output")
|
100 |
|
101 |
demo = gr.Interface(
|
102 |
generate_ocr,
|
103 |
+
[method, image_or_pdf],
|
104 |
output,
|
105 |
title="Optical Character Recognition",
|
106 |
css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
|
107 |
+
article="""<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at
|
108 |
+
<a href="mailto:[email protected]" target="_blank">[email protected]</a>
|
109 |
+
<p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
|
|
|
|
|
110 |
)
|
111 |
+
|
112 |
demo.launch(show_error=True)
|