Norakneath commited on
Commit
6d3c4df
·
verified ·
1 Parent(s): fa2b0fb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ from PIL import Image, ImageDraw
4
+ import pytesseract
5
+
6
+ # Set the correct Tesseract path for Hugging Face Spaces
7
+ pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
8
+
9
+ YOLO_MODEL_PATH = "best.pt"
10
+ model = YOLO(YOLO_MODEL_PATH, task='detect').to("cpu")
11
+
12
+ def merge_boxes_into_lines(boxes, y_threshold=10):
13
+ if len(boxes) == 0:
14
+ return []
15
+
16
+ boxes = sorted(boxes, key=lambda b: b[1])
17
+ merged_lines = []
18
+ current_line = list(boxes[0])
19
+
20
+ for i in range(1, len(boxes)):
21
+ x1, y1, x2, y2 = boxes[i]
22
+
23
+ if abs(y1 - current_line[1]) < y_threshold:
24
+ current_line[0] = min(current_line[0], x1)
25
+ current_line[2] = max(current_line[2], x2)
26
+ current_line[3] = max(current_line[3], y2)
27
+ else:
28
+ merged_lines.append(current_line)
29
+ current_line = list(boxes[i])
30
+
31
+ merged_lines.append(current_line)
32
+ return merged_lines
33
+
34
+ def detect_and_ocr(image):
35
+ image = Image.fromarray(image)
36
+ original_image = image.copy()
37
+
38
+ results = model.predict(image, conf=0.3, iou=0.5, device="cpu")
39
+ detected_boxes = results[0].boxes.xyxy.tolist()
40
+ detected_boxes = [list(map(int, box)) for box in detected_boxes]
41
+
42
+ merged_boxes = merge_boxes_into_lines(detected_boxes)
43
+
44
+ draw = ImageDraw.Draw(original_image)
45
+ extracted_text_lines = []
46
+
47
+ for idx, (x1, y1, x2, y2) in enumerate(merged_boxes):
48
+ draw.rectangle([x1, y1, x2, y2], outline="blue", width=2)
49
+ draw.text((x1, y1 - 10), f"Line {idx}", fill="blue")
50
+
51
+ cropped_line = image.crop((x1, y1, x2, y2))
52
+
53
+ ocr_text = pytesseract.image_to_string(cropped_line, lang="eng").strip()
54
+ if ocr_text:
55
+ extracted_text_lines.append(ocr_text)
56
+
57
+ full_text = "\n".join(extracted_text_lines)
58
+
59
+ return original_image, full_text
60
+
61
+ with gr.Blocks() as iface:
62
+ gr.Markdown("# Text Line Detection with OCR")
63
+ gr.Markdown("## Upload an image to detect text lines and extract text")
64
+
65
+ with gr.Row():
66
+ with gr.Column(scale=1):
67
+ gr.Markdown("### Upload Image")
68
+ image_input = gr.Image(type="numpy", label="Upload an image")
69
+
70
+ with gr.Column(scale=1):
71
+ gr.Markdown("### Annotated Image with Bounding Boxes")
72
+ output_annotated = gr.Image(type="pil", label="Detected Text Lines")
73
+
74
+ gr.Markdown("### Extracted Text (OCR Result)")
75
+ output_text = gr.Textbox(label="Extracted Text", lines=10)
76
+
77
+ image_input.upload(
78
+ detect_and_ocr,
79
+ inputs=image_input,
80
+ outputs=[output_annotated, output_text]
81
+ )
82
+
83
+ iface.launch()