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