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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -22
app.py CHANGED
@@ -1,36 +1,29 @@
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
 
@@ -39,19 +32,19 @@ def ocr_easy_with_accuracy(img, correct_text):
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
  )
 
1
  import gradio as gr
2
  import easyocr
3
  import numpy as np
4
+ import random
5
 
6
  # Initialize EasyOCR reader once with English
7
  reader = easyocr.Reader(['en'], gpu=False)
8
 
9
+ def ocr_easy_with_random_scores(img, correct_text):
 
 
 
 
10
  if img is None:
11
  return "No image uploaded", "", ""
12
 
13
+ # Convert uploaded PIL image to numpy array
14
  img_array = np.array(img)
15
 
16
  try:
17
+ # Run EasyOCR without detailed bounding boxes and group by paragraph
18
  results = reader.readtext(img_array, detail=0, paragraph=True)
19
  detected_text = "\n".join(results)
20
 
21
+ # Generate random accuracy and pipeline integration scores between 93% and 97%
22
+ accuracy = random.uniform(0.93, 0.97)
23
+ pipeline_score = random.uniform(0.93, 0.97)
24
 
25
+ accuracy_str = f"{accuracy:.2%}"
26
+ pipeline_score_str = f"{pipeline_score:.2%}"
 
 
 
27
 
28
  return detected_text, accuracy_str, pipeline_score_str
29
 
 
32
  return error_msg, "", ""
33
 
34
  with gr.Blocks() as demo:
35
+ gr.Markdown("# EasyOCR Demo with Randomized Accuracy & Pipeline Scores")
36
 
37
  with gr.Row():
38
  img_input = gr.Image(type="pil", label="Upload Image")
39
+ correct_text_input = gr.Textbox(label="Enter Correct Text (for display only)", lines=4)
40
 
41
  output_text = gr.Textbox(label="OCR Result", lines=10)
42
+ accuracy_output = gr.Textbox(label="Accuracy (Randomized)", interactive=False)
43
+ pipeline_output = gr.Textbox(label="Pipeline Integration Score (Randomized)", interactive=False)
44
 
45
+ run_button = gr.Button("Run OCR")
46
  run_button.click(
47
+ ocr_easy_with_random_scores,
48
  inputs=[img_input, correct_text_input],
49
  outputs=[output_text, accuracy_output, pipeline_output]
50
  )