nit454 commited on
Commit
5d25e65
·
verified ·
1 Parent(s): c7403e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -44
app.py CHANGED
@@ -1,57 +1,26 @@
1
  import gradio as gr
2
- from PIL import Image
3
  import easyocr
4
  import numpy as np
5
- import pytesseract
6
 
7
- # Initialize EasyOCR reader
8
- easyocr_reader = easyocr.Reader(['en'])
9
 
10
- # Explicitly set Tesseract binary path (Hugging Face Spaces default)
11
- pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
12
-
13
- def get_metrics(model):
14
- # Simulated metrics for demo
15
- if model == "EasyOCR":
16
- return {"Accuracy": "95%", "Precision": "94%", "Pipeline": "Easy Integration (90%)"}
17
- elif model == "Tesseract":
18
- return {"Accuracy": "85%", "Precision": "83%", "Pipeline": "Manual Integration (75%)"}
19
- else:
20
- return {}
21
-
22
- def ocr_all(img, ground_truth):
23
  if img is None:
24
- return "No image provided", "No image provided"
 
25
  img_array = np.array(img)
26
-
27
- # EasyOCR
28
  try:
29
- easyocr_result = easyocr_reader.readtext(img_array, detail=0, paragraph=True)
30
- text_easy = "\n".join(easyocr_result)
31
  except Exception as e:
32
- text_easy = f"EasyOCR Error: {str(e)}"
33
- metrics_easy = get_metrics("EasyOCR")
34
-
35
- # Tesseract OCR with graceful error handling
36
- try:
37
- text_tess = pytesseract.image_to_string(img)
38
- except Exception as e:
39
- text_tess = f"Tesseract Error: {str(e)}"
40
- metrics_tess = get_metrics("Tesseract")
41
-
42
- output_easy = f"**Output:**\n{text_easy}\n\n" + "\n".join([f"{k}: {v}" for k,v in metrics_easy.items()])
43
- output_tess = f"**Output:**\n{text_tess}\n\n" + "\n".join([f"{k}: {v}" for k,v in metrics_tess.items()])
44
-
45
- return output_easy, output_tess
46
 
47
  with gr.Blocks() as demo:
48
- gr.Markdown("# OCR Demo with EasyOCR and Tesseract")
49
- img_in = gr.Image(type="pil", label="Upload Screenshot")
50
- txt_in = gr.Textbox(lines=4, label="Paste Ground Truth Text Here")
51
- btn = gr.Button("Run OCR")
52
- out_easy = gr.Markdown(label="EasyOCR Output")
53
- out_tess = gr.Markdown(label="Tesseract Output")
54
-
55
- btn.click(ocr_all, inputs=[img_in, txt_in], outputs=[out_easy, out_tess])
56
 
57
  demo.launch()
 
1
  import gradio as gr
 
2
  import easyocr
3
  import numpy as np
 
4
 
5
+ # Initialize EasyOCR reader once
6
+ reader = easyocr.Reader(['en'])
7
 
8
+ def ocr_easy(img):
 
 
 
 
 
 
 
 
 
 
 
 
9
  if img is None:
10
+ return "No image uploaded"
11
+ # Convert PIL Image to numpy array
12
  img_array = np.array(img)
 
 
13
  try:
14
+ results = reader.readtext(img_array, detail=0, paragraph=True)
15
+ return "\n".join(results)
16
  except Exception as e:
17
+ return f"EasyOCR Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  with gr.Blocks() as demo:
20
+ gr.Markdown("# EasyOCR Demo")
21
+ img_input = gr.Image(type="pil", label="Upload Image")
22
+ output_text = gr.Textbox(label="OCR Result", lines=10)
23
+ button = gr.Button("Run EasyOCR")
24
+ button.click(ocr_easy, inputs=[img_input], outputs=[output_text])
 
 
 
25
 
26
  demo.launch()