Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,14 @@ import easyocr
|
|
| 4 |
import numpy as np
|
| 5 |
import pytesseract
|
| 6 |
|
| 7 |
-
# Initialize EasyOCR
|
| 8 |
easyocr_reader = easyocr.Reader(['en'])
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
def get_metrics(model):
|
| 11 |
-
#
|
| 12 |
if model == "EasyOCR":
|
| 13 |
return {"Accuracy": "95%", "Precision": "94%", "Pipeline": "Easy Integration (90%)"}
|
| 14 |
elif model == "Tesseract":
|
|
@@ -19,7 +22,9 @@ def get_metrics(model):
|
|
| 19 |
def ocr_all(img, ground_truth):
|
| 20 |
if img is None:
|
| 21 |
return "No image provided", "No image provided"
|
|
|
|
| 22 |
img_array = np.array(img)
|
|
|
|
| 23 |
try:
|
| 24 |
easyocr_result = easyocr_reader.readtext(img_array, detail=0, paragraph=True)
|
| 25 |
text_easy = "\n".join(easyocr_result)
|
|
@@ -33,19 +38,19 @@ def ocr_all(img, ground_truth):
|
|
| 33 |
text_tess = f"Tesseract Error: {str(e)}"
|
| 34 |
metrics_tess = get_metrics("Tesseract")
|
| 35 |
|
| 36 |
-
output_easy = f"**Output:**\n{text_easy}\n\n" + "\n".join([f"{k}: {v}" for k,v in metrics_easy.items()])
|
| 37 |
-
output_tess = f"**Output:**\n{text_tess}\n\n" + "\n".join([f"{k}: {v}" for k,v in metrics_tess.items()])
|
| 38 |
-
|
| 39 |
return output_easy, output_tess
|
| 40 |
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
-
gr.Markdown("# OCR Demo
|
| 43 |
img_in = gr.Image(type="pil", label="Upload Screenshot")
|
| 44 |
-
txt_in = gr.Textbox(lines=
|
| 45 |
btn = gr.Button("Run OCR")
|
| 46 |
out_easy = gr.Markdown(label="EasyOCR Output")
|
| 47 |
out_tess = gr.Markdown(label="Tesseract Output")
|
| 48 |
-
|
| 49 |
btn.click(ocr_all, inputs=[img_in, txt_in], outputs=[out_easy, out_tess])
|
| 50 |
|
| 51 |
demo.launch()
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import pytesseract
|
| 6 |
|
| 7 |
+
# Initialize EasyOCR reader
|
| 8 |
easyocr_reader = easyocr.Reader(['en'])
|
| 9 |
|
| 10 |
+
# Explicitly set Tesseract command path for Hugging Face Spaces
|
| 11 |
+
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
|
| 12 |
+
|
| 13 |
def get_metrics(model):
|
| 14 |
+
# Hardcoded metrics for demonstration
|
| 15 |
if model == "EasyOCR":
|
| 16 |
return {"Accuracy": "95%", "Precision": "94%", "Pipeline": "Easy Integration (90%)"}
|
| 17 |
elif model == "Tesseract":
|
|
|
|
| 22 |
def ocr_all(img, ground_truth):
|
| 23 |
if img is None:
|
| 24 |
return "No image provided", "No image provided"
|
| 25 |
+
|
| 26 |
img_array = np.array(img)
|
| 27 |
+
|
| 28 |
try:
|
| 29 |
easyocr_result = easyocr_reader.readtext(img_array, detail=0, paragraph=True)
|
| 30 |
text_easy = "\n".join(easyocr_result)
|
|
|
|
| 38 |
text_tess = f"Tesseract Error: {str(e)}"
|
| 39 |
metrics_tess = get_metrics("Tesseract")
|
| 40 |
|
| 41 |
+
output_easy = f"**Output:**\n{text_easy}\n\n" + "\n".join([f"{k}: {v}" for k, v in metrics_easy.items()])
|
| 42 |
+
output_tess = f"**Output:**\n{text_tess}\n\n" + "\n".join([f"{k}: {v}" for k, v in metrics_tess.items()])
|
| 43 |
+
|
| 44 |
return output_easy, output_tess
|
| 45 |
|
| 46 |
with gr.Blocks() as demo:
|
| 47 |
+
gr.Markdown("# OCR Demo on Hugging Face Spaces\nUpload an image and paste optional ground truth text.")
|
| 48 |
img_in = gr.Image(type="pil", label="Upload Screenshot")
|
| 49 |
+
txt_in = gr.Textbox(lines=3, label="Paste Ground Truth Text")
|
| 50 |
btn = gr.Button("Run OCR")
|
| 51 |
out_easy = gr.Markdown(label="EasyOCR Output")
|
| 52 |
out_tess = gr.Markdown(label="Tesseract Output")
|
| 53 |
+
|
| 54 |
btn.click(ocr_all, inputs=[img_in, txt_in], outputs=[out_easy, out_tess])
|
| 55 |
|
| 56 |
demo.launch()
|