first push
Browse files- app.py +87 -0
- requirements.txt +12 -0
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import libraries
|
2 |
+
import cv2
|
3 |
+
from ultralytics import YOLO
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# Define constants
|
7 |
+
ENTITIES_COLORS = {
|
8 |
+
"Caption": (191, 100, 21),
|
9 |
+
"Footnote": (2, 62, 115),
|
10 |
+
"Formula": (140, 80, 58),
|
11 |
+
"List-item": (168, 181, 69),
|
12 |
+
"Page-footer": (2, 69, 84),
|
13 |
+
"Page-header": (83, 115, 106),
|
14 |
+
"Picture": (255, 72, 88),
|
15 |
+
"Section-header": (0, 204, 192),
|
16 |
+
"Table": (116, 127, 127),
|
17 |
+
"Text": (0, 153, 221),
|
18 |
+
"Title": (196, 51, 2)
|
19 |
+
}
|
20 |
+
BOX_PADDING = 2
|
21 |
+
|
22 |
+
# Load models
|
23 |
+
DETECTION_MODEL = YOLO("models/yolov11x_best.pt")
|
24 |
+
|
25 |
+
def detect(image_path):
|
26 |
+
"""
|
27 |
+
Output inference image with bounding box
|
28 |
+
Args:
|
29 |
+
- image: to check for checkboxes
|
30 |
+
Return: image with bounding boxes drawn
|
31 |
+
"""
|
32 |
+
image = cv2.imread(image_path)
|
33 |
+
if image is None:
|
34 |
+
return image
|
35 |
+
|
36 |
+
# Predict on image
|
37 |
+
results = DETECTION_MODEL.predict(source=image, conf=0.2, iou=0.8) # Predict on image
|
38 |
+
boxes = results[0].boxes # Get bounding boxes
|
39 |
+
|
40 |
+
if len(boxes) == 0:
|
41 |
+
return image
|
42 |
+
|
43 |
+
# Get bounding boxes
|
44 |
+
for box in boxes:
|
45 |
+
detection_class_conf = round(box.conf.item(), 2)
|
46 |
+
cls = list(ENTITIES_COLORS)[int(box.cls)]
|
47 |
+
# Get start and end points of the current box
|
48 |
+
start_box = (int(box.xyxy[0][0]), int(box.xyxy[0][1]))
|
49 |
+
end_box = (int(box.xyxy[0][2]), int(box.xyxy[0][3]))
|
50 |
+
|
51 |
+
|
52 |
+
# 01. DRAW BOUNDING BOX OF OBJECT
|
53 |
+
# Adjust the scale factors for bounding box and label
|
54 |
+
box_scale_factor = 0.001 # Reduce this value to make the bounding box thinner
|
55 |
+
label_scale_factor = 0.5 # Reduce this value to make the label smaller
|
56 |
+
|
57 |
+
# 01. DRAW BOUNDING BOX OF OBJECT
|
58 |
+
line_thickness = round(box_scale_factor * (image.shape[0] + image.shape[1]) / 2) + 1
|
59 |
+
image = cv2.rectangle(img=image,
|
60 |
+
pt1=start_box,
|
61 |
+
pt2=end_box,
|
62 |
+
color=ENTITIES_COLORS[cls],
|
63 |
+
thickness=line_thickness) # Draw the box with predefined colors
|
64 |
+
|
65 |
+
# 02. DRAW LABEL
|
66 |
+
text = cls + " " + str(detection_class_conf)
|
67 |
+
# Get text dimensions to draw wrapping box
|
68 |
+
font_thickness = max(line_thickness - 1, 1)
|
69 |
+
(font_scale_w, font_scale_h) = (line_thickness * label_scale_factor, line_thickness * label_scale_factor)
|
70 |
+
(text_w, text_h), _ = cv2.getTextSize(text=text, fontFace=2, fontScale=font_scale_w, thickness=font_thickness)
|
71 |
+
# Draw wrapping box for text
|
72 |
+
image = cv2.rectangle(img=image,
|
73 |
+
pt1=(start_box[0], start_box[1] - text_h - BOX_PADDING*2),
|
74 |
+
pt2=(start_box[0] + text_w + BOX_PADDING * 2, start_box[1]),
|
75 |
+
color=ENTITIES_COLORS[cls],
|
76 |
+
thickness=-1)
|
77 |
+
# Put class name on image
|
78 |
+
start_text = (start_box[0] + BOX_PADDING, start_box[1] - BOX_PADDING)
|
79 |
+
image = cv2.putText(img=image, text=text, org=start_text, fontFace=0, color=(255,255,255), fontScale=font_scale_w, thickness=font_thickness)
|
80 |
+
|
81 |
+
|
82 |
+
return image
|
83 |
+
|
84 |
+
iface = gr.Interface(fn=detect,
|
85 |
+
inputs=gr.Image(label="Upload scanned document", type="filepath"),
|
86 |
+
outputs="image")
|
87 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
ultralytics
|
2 |
+
torch==2.0.1
|
3 |
+
torchvision==0.15.2
|
4 |
+
pycocotools==2.0.7
|
5 |
+
PyYAML==6.0.1
|
6 |
+
scipy==1.13.0
|
7 |
+
gradio==4.31.5
|
8 |
+
opencv-python==4.9.0.80
|
9 |
+
psutil==5.9.8
|
10 |
+
py-cpuinfo==9.0.0
|
11 |
+
safetensors==0.4.3
|
12 |
+
|