Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import pytesseract
|
4 |
+
from pytesseract import Output
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
def text_detection(img, config="--psm 11 --oem 3"):
|
8 |
+
data = pytesseract.image_to_data(img, config=config, output_type=Output.DICT)
|
9 |
+
horizontal_text = []
|
10 |
+
vertical_text = []
|
11 |
+
|
12 |
+
for i in range(len(data['text'])):
|
13 |
+
if int(data['conf'][i]) > 20:
|
14 |
+
x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
|
15 |
+
text = data['text'][i]
|
16 |
+
if w > h:
|
17 |
+
horizontal_text.append(text)
|
18 |
+
else:
|
19 |
+
vertical_text.append(text)
|
20 |
+
return horizontal_text, vertical_text, data
|
21 |
+
|
22 |
+
def draw_boxes(img, data):
|
23 |
+
for i in range(len(data['text'])):
|
24 |
+
if int(data['conf'][i]) > 20:
|
25 |
+
x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
|
26 |
+
text = data['text'][i]
|
27 |
+
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
28 |
+
cv2.putText(img, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
|
29 |
+
return img
|
30 |
+
|
31 |
+
def word_level_accuracy(data, ground_truth):
|
32 |
+
ocr_text = ' '.join([text for text in data['text'] if text.strip()])
|
33 |
+
gt_words = set(ground_truth.split())
|
34 |
+
ocr_words = set(ocr_text.split())
|
35 |
+
correct = gt_words.intersection(ocr_words)
|
36 |
+
return (len(correct) / len(gt_words)) * 100 if gt_words else 0
|
37 |
+
|
38 |
+
def character_level_accuracy(data, ground_truth):
|
39 |
+
ocr_text = ''.join([text.strip() for text in data['text']])
|
40 |
+
gt_chars = set(ground_truth.replace(" ", ""))
|
41 |
+
ocr_chars = set(ocr_text.replace(" ", ""))
|
42 |
+
correct = gt_chars.intersection(ocr_chars)
|
43 |
+
return (len(correct) / len(gt_chars)) * 100 if gt_chars else 0
|
44 |
+
|
45 |
+
def process(image, ground_truth):
|
46 |
+
if image is None:
|
47 |
+
return None, "Please upload an image."
|
48 |
+
|
49 |
+
img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
50 |
+
h_text, v_text, data = text_detection(img_bgr)
|
51 |
+
word_acc = word_level_accuracy(data, ground_truth)
|
52 |
+
char_acc = character_level_accuracy(data, ground_truth)
|
53 |
+
|
54 |
+
result_img = draw_boxes(img_bgr.copy(), data)
|
55 |
+
result_img_rgb = cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)
|
56 |
+
|
57 |
+
results = f"**Horizontal Text**: {' '.join(h_text) if h_text else 'None'}\n\n"
|
58 |
+
results += f"**Vertical Text**: {' '.join(v_text) if v_text else 'None'}\n\n"
|
59 |
+
results += f"**Word-Level Accuracy**: {word_acc:.2f}%\n"
|
60 |
+
results += f"**Character-Level Accuracy**: {char_acc:.2f}%"
|
61 |
+
|
62 |
+
return result_img_rgb, results
|
63 |
+
|
64 |
+
demo = gr.Interface(
|
65 |
+
fn=process,
|
66 |
+
inputs=[
|
67 |
+
gr.Image(type="numpy", label="Upload Image"),
|
68 |
+
gr.Textbox(lines=4, placeholder="Enter ground truth text here", label="Ground Truth")
|
69 |
+
],
|
70 |
+
outputs=[
|
71 |
+
gr.Image(type="numpy", label="Detected Text with Bounding Boxes"),
|
72 |
+
gr.Markdown()
|
73 |
+
],
|
74 |
+
title="OCR Accuracy Evaluator with Bounding Boxes",
|
75 |
+
description="Upload an image and ground truth text to evaluate Tesseract OCR accuracy by word and character. Bounding boxes are drawn around detected text."
|
76 |
+
)
|
77 |
+
|
78 |
+
if __name__ == "__main__":
|
79 |
+
demo.launch()
|