nit454 commited on
Commit
e66e36b
·
verified ·
1 Parent(s): 7f4bad9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -10
app.py CHANGED
@@ -1,26 +1,59 @@
1
  import gradio as gr
2
  import easyocr
3
  import numpy as np
 
4
 
5
- # Initialize EasyOCR reader once with English language
6
  reader = easyocr.Reader(['en'], gpu=False)
7
 
8
- def ocr_easy(img):
 
 
 
 
9
  if img is None:
10
- return "No image uploaded"
 
 
11
  img_array = np.array(img)
 
12
  try:
13
- # OCR with detail=0 returns text only, paragraph=True groups lines
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 OCR Demo")
21
- img_input = gr.Image(type="pil", label="Upload Image")
 
 
 
 
22
  output_text = gr.Textbox(label="OCR Result", lines=10)
23
- run_button = gr.Button("Run OCR")
24
- run_button.click(ocr_easy, inputs=img_input, outputs=output_text)
 
 
 
 
 
 
 
25
 
26
  demo.launch()
 
1
  import gradio as gr
2
  import easyocr
3
  import numpy as np
4
+ from difflib import SequenceMatcher
5
 
6
+ # Initialize EasyOCR reader once with English
7
  reader = easyocr.Reader(['en'], gpu=False)
8
 
9
+ def calculate_similarity(text1, text2):
10
+ # Calculate similarity ratio for accuracy and pipeline score
11
+ return SequenceMatcher(None, text1.lower().strip(), text2.lower().strip()).ratio()
12
+
13
+ def ocr_easy_with_accuracy(img, correct_text):
14
  if img is None:
15
+ return "No image uploaded", "", ""
16
+
17
+ # Convert uploaded image (PIL) to numpy array
18
  img_array = np.array(img)
19
+
20
  try:
21
+ # Run EasyOCR with paragraph grouping and no detailed bounding boxes
22
  results = reader.readtext(img_array, detail=0, paragraph=True)
23
+ detected_text = "\n".join(results)
24
+
25
+ if correct_text.strip():
26
+ accuracy = calculate_similarity(detected_text, correct_text)
27
+ pipeline_score = accuracy # Using same metric for demo
28
+
29
+ accuracy_str = f"{accuracy:.2%}"
30
+ pipeline_score_str = f"{pipeline_score:.2%}"
31
+ else:
32
+ accuracy_str = "No ground truth provided"
33
+ pipeline_score_str = "No ground truth provided"
34
+
35
+ return detected_text, accuracy_str, pipeline_score_str
36
+
37
  except Exception as e:
38
+ error_msg = f"EasyOCR Error: {str(e)}"
39
+ return error_msg, "", ""
40
 
41
  with gr.Blocks() as demo:
42
+ gr.Markdown("# EasyOCR with Accuracy and Pipeline Score")
43
+
44
+ with gr.Row():
45
+ img_input = gr.Image(type="pil", label="Upload Image")
46
+ correct_text_input = gr.Textbox(label="Enter Correct Text for Accuracy Check", lines=4)
47
+
48
  output_text = gr.Textbox(label="OCR Result", lines=10)
49
+ accuracy_output = gr.Textbox(label="Accuracy", interactive=False)
50
+ pipeline_output = gr.Textbox(label="Pipeline Integration Score", interactive=False)
51
+
52
+ run_button = gr.Button("Run OCR and Check Accuracy")
53
+ run_button.click(
54
+ ocr_easy_with_accuracy,
55
+ inputs=[img_input, correct_text_input],
56
+ outputs=[output_text, accuracy_output, pipeline_output]
57
+ )
58
 
59
  demo.launch()