Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,20 +2,16 @@ import gradio as gr
|
|
| 2 |
from PIL import Image
|
| 3 |
import easyocr
|
| 4 |
import numpy as np
|
| 5 |
-
from paddleocr import PaddleOCR
|
| 6 |
import pytesseract
|
| 7 |
|
| 8 |
# Initialize OCR models
|
| 9 |
easyocr_reader = easyocr.Reader(['en'])
|
| 10 |
-
paddleocr_reader = PaddleOCR()
|
| 11 |
# Tesseract uses pytesseract directly
|
| 12 |
|
| 13 |
def get_metrics(model):
|
| 14 |
-
#
|
| 15 |
if model == "EasyOCR":
|
| 16 |
return {"Accuracy": "95%", "Precision": "94%", "Pipeline": "Easy Integration (90%)"}
|
| 17 |
-
elif model == "PaddleOCR":
|
| 18 |
-
return {"Accuracy": "90%", "Precision": "89%", "Pipeline": "Moderate Integration (85%)"}
|
| 19 |
elif model == "Tesseract":
|
| 20 |
return {"Accuracy": "85%", "Precision": "83%", "Pipeline": "Manual Integration (75%)"}
|
| 21 |
else:
|
|
@@ -29,35 +25,25 @@ def ocr_all(img, ground_truth):
|
|
| 29 |
text_easy = "\n".join(result_easy)
|
| 30 |
metrics_easy = get_metrics("EasyOCR")
|
| 31 |
|
| 32 |
-
# PaddleOCR
|
| 33 |
-
result_paddle = paddleocr_reader.ocr(img_array, cls=True)
|
| 34 |
-
try:
|
| 35 |
-
text_paddle = " ".join([line[1][0] for line in result_paddle])
|
| 36 |
-
except Exception:
|
| 37 |
-
text_paddle = "Error during PaddleOCR"
|
| 38 |
-
metrics_paddle = get_metrics("PaddleOCR")
|
| 39 |
-
|
| 40 |
# Tesseract
|
| 41 |
text_tess = pytesseract.image_to_string(img)
|
| 42 |
metrics_tess = get_metrics("Tesseract")
|
| 43 |
|
| 44 |
output_easy = f"**Output:**\n{text_easy}\n\n" + "\n".join([f"{k}: {v}" for k,v in metrics_easy.items()])
|
| 45 |
-
output_paddle = f"**Output:**\n{text_paddle}\n\n" + "\n".join([f"{k}: {v}" for k,v in metrics_paddle.items()])
|
| 46 |
output_tess = f"**Output:**\n{text_tess}\n\n" + "\n".join([f"{k}: {v}" for k,v in metrics_tess.items()])
|
| 47 |
|
| 48 |
-
return output_easy,
|
| 49 |
|
| 50 |
with gr.Blocks() as demo:
|
| 51 |
-
gr.Markdown("# Multi-OCR Model Demo\nUpload an image and paste the correct ground-truth text below.")
|
| 52 |
with gr.Row():
|
| 53 |
img_in = gr.Image(type="pil", label="Screenshot")
|
| 54 |
txt_in = gr.Textbox(lines=4, label="Ground Truth Text Here")
|
| 55 |
btn = gr.Button("Run OCR")
|
| 56 |
with gr.Row():
|
| 57 |
out_easy = gr.Markdown(label="EasyOCR Output")
|
| 58 |
-
out_paddle = gr.Markdown(label="PaddleOCR Output")
|
| 59 |
out_tess = gr.Markdown(label="Tesseract Output")
|
| 60 |
|
| 61 |
-
btn.click(ocr_all, inputs=[img_in, txt_in], outputs=[out_easy,
|
| 62 |
|
| 63 |
demo.launch()
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import easyocr
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
import pytesseract
|
| 6 |
|
| 7 |
# Initialize OCR models
|
| 8 |
easyocr_reader = easyocr.Reader(['en'])
|
|
|
|
| 9 |
# Tesseract uses pytesseract directly
|
| 10 |
|
| 11 |
def get_metrics(model):
|
| 12 |
+
# Updated simulated fixed scores without PaddleOCR
|
| 13 |
if model == "EasyOCR":
|
| 14 |
return {"Accuracy": "95%", "Precision": "94%", "Pipeline": "Easy Integration (90%)"}
|
|
|
|
|
|
|
| 15 |
elif model == "Tesseract":
|
| 16 |
return {"Accuracy": "85%", "Precision": "83%", "Pipeline": "Manual Integration (75%)"}
|
| 17 |
else:
|
|
|
|
| 25 |
text_easy = "\n".join(result_easy)
|
| 26 |
metrics_easy = get_metrics("EasyOCR")
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
# Tesseract
|
| 29 |
text_tess = pytesseract.image_to_string(img)
|
| 30 |
metrics_tess = get_metrics("Tesseract")
|
| 31 |
|
| 32 |
output_easy = f"**Output:**\n{text_easy}\n\n" + "\n".join([f"{k}: {v}" for k,v in metrics_easy.items()])
|
|
|
|
| 33 |
output_tess = f"**Output:**\n{text_tess}\n\n" + "\n".join([f"{k}: {v}" for k,v in metrics_tess.items()])
|
| 34 |
|
| 35 |
+
return output_easy, output_tess
|
| 36 |
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
+
gr.Markdown("# Multi-OCR Model Demo without PaddleOCR\nUpload an image and paste the correct ground-truth text below.")
|
| 39 |
with gr.Row():
|
| 40 |
img_in = gr.Image(type="pil", label="Screenshot")
|
| 41 |
txt_in = gr.Textbox(lines=4, label="Ground Truth Text Here")
|
| 42 |
btn = gr.Button("Run OCR")
|
| 43 |
with gr.Row():
|
| 44 |
out_easy = gr.Markdown(label="EasyOCR Output")
|
|
|
|
| 45 |
out_tess = gr.Markdown(label="Tesseract Output")
|
| 46 |
|
| 47 |
+
btn.click(ocr_all, inputs=[img_in, txt_in], outputs=[out_easy, out_tess])
|
| 48 |
|
| 49 |
demo.launch()
|