Spaces:
Sleeping
Sleeping
Update app.py
Browse files6 unmodified lines78910111213141516174 unmodified lines2223242526272829301 unmodified line323334353637383940414243444546474849505152535455566 unmodified lines# Initialize EasyOCR readereasyocr_reader = easyocr.Reader(['en'])# Explicitly set Tesseract command path for Hugging Face Spacespytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'def get_metrics(model):# Hardcoded metrics for demonstrationif model == "EasyOCR":return {"Accuracy": "95%", "Precision": "94%", "Pipeline": "Easy Integration (90%)"}elif model == "Tesseract":4 unmodified linesdef ocr_all(img, ground_truth):if img is None:return "No image provided", "No image provided"img_array = np.array(img)try:easyocr_result = easyocr_reader.readtext(img_array, detail=0, paragraph=True)text_easy = "\n".join(easyocr_result)1 unmodified linetext_easy = f"EasyOCR Error: {str(e)}"metrics_easy = get_metrics("EasyOCR")try:text_tess = pytesseract.image_to_string(img)except Exception as e:text_tess = f"Tesseract Error: {str(e)}"metrics_tess = get_metrics("Tesseract")output_easy = f"**Output:**\n{text_easy}\n\n" + "\n".join([f"{k}: {v}" for k, v in metrics_easy.items()])output_tess = f"**Output:**\n{text_tess}\n\n" + "\n".join([f"{k}: {v}" for k, v in metrics_tess.items()])return output_easy, output_tesswith gr.Blocks() as demo:gr.Markdown("# OCR Demo on Hugging Face Spaces\nUpload an image and paste optional ground truth text.")img_in = gr.Image(type="pil", label="Upload Screenshot")txt_in = gr.Textbox(lines=3, label="Paste Ground Truth Text")btn = gr.Button("Run OCR")out_easy = gr.Markdown(label="EasyOCR Output")out_tess = gr.Markdown(label="Tesseract Output")btn.click(ocr_all, inputs=[img_in, txt_in], outputs=[out_easy, out_tess])demo.launch()6 unmodified lines78910111213141516174 unmodified lines2223242526272829301 unmodified line32333435363738394041424344454647484950515253545556576 unmodified lines# Initialize EasyOCR readereasyocr_reader = easyocr.Reader(['en'])# Explicitly set Tesseract binary path (Hugging Face Spaces default)pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'def get_metrics(model):# Simulated metrics for demoif model == "EasyOCR":return {"Accuracy": "95%", "Precision": "94%", "Pipeline": "Easy Integration (90%)"}elif model == "Tesseract":4 unmodified linesdef ocr_all(img, ground_truth):if img is None:return "No image provided", "No image provided"img_array = np.array(img)# EasyOCRtry:easyocr_result = easyocr_reader.readtext(img_array, detail=0, paragraph=True)text_easy = "\n".join(easyocr_result)1 unmodified linetext_easy = f"EasyOCR Error: {str(e)}"metrics_easy = get_metrics("EasyOCR")# Tesseract OCR with graceful error handlingtry:text_tess = pytesseract.image_to_string(img)except Exception as e:text_tess = f"Tesseract Error: {str(e)}"metrics_tess = get_metrics("Tesseract")output_easy = f"**Output:**\n{text_easy}\n\n" + "\n".join([f"{k}: {v}" for k,v in metrics_easy.items()])output_tess = f"**Output:**\n{text_tess}\n\n" + "\n".join([f"{k}: {v}" for k,v in metrics_tess.items()])return output_easy, output_tesswith gr.Blocks() as demo:gr.Markdown("# OCR Demo with EasyOCR and Tesseract")img_in = gr.Image(type="pil", label="Upload Screenshot")txt_in = gr.Textbox(lines=4, label="Paste Ground Truth Text Here")btn = gr.Button("Run OCR")out_easy = gr.Markdown(label="EasyOCR Output")out_tess = gr.Markdown(label="Tesseract Output")btn.click(ocr_all, inputs=[img_in, txt_in], outputs=[out_easy, out_tess])demo.launch()
app.py+10-9