nit454 commited on
Commit
65e1130
·
verified ·
1 Parent(s): 80dce17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -7,11 +7,11 @@ import pytesseract
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,9 +22,9 @@ def get_metrics(model):
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)
@@ -32,25 +32,26 @@ def ocr_all(img, ground_truth):
32
  text_easy = f"EasyOCR Error: {str(e)}"
33
  metrics_easy = get_metrics("EasyOCR")
34
 
 
35
  try:
36
  text_tess = pytesseract.image_to_string(img)
37
  except Exception as e:
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()
 
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":
 
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)
 
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()