Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +98 -0
- models/dla-model.pt +3 -0
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import libraries
|
2 |
+
import cv2 # for reading images, draw bounding boxes
|
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/dla-model.pt")
|
24 |
+
|
25 |
+
def detect(image_path):
|
26 |
+
"""
|
27 |
+
Output inference image with bounding box
|
28 |
+
|
29 |
+
Args:
|
30 |
+
- image: to check for checkboxes
|
31 |
+
|
32 |
+
Return: image with bounding boxes drawn
|
33 |
+
"""
|
34 |
+
image = cv2.imread(image_path)
|
35 |
+
if image is None:
|
36 |
+
return image
|
37 |
+
|
38 |
+
# Predict on image
|
39 |
+
results = DETECTION_MODEL.predict(source=image, conf=0.2, iou=0.8) # Predict on image
|
40 |
+
boxes = results[0].boxes # Get bounding boxes
|
41 |
+
|
42 |
+
if len(boxes) == 0:
|
43 |
+
return image
|
44 |
+
|
45 |
+
# Get bounding boxes
|
46 |
+
for box in boxes:
|
47 |
+
detection_class_conf = round(box.conf.item(), 2)
|
48 |
+
cls = list(ENTITIES_COLORS)[int(box.cls)]
|
49 |
+
# Get start and end points of the current box
|
50 |
+
start_box = (int(box.xyxy[0][0]), int(box.xyxy[0][1]))
|
51 |
+
end_box = (int(box.xyxy[0][2]), int(box.xyxy[0][3]))
|
52 |
+
|
53 |
+
|
54 |
+
# 01. DRAW BOUNDING BOX OF OBJECT
|
55 |
+
line_thickness = round(0.002 * (image.shape[0] + image.shape[1]) / 2) + 1
|
56 |
+
image = cv2.rectangle(img=image,
|
57 |
+
pt1=start_box,
|
58 |
+
pt2=end_box,
|
59 |
+
color=ENTITIES_COLORS[cls],
|
60 |
+
thickness = line_thickness) # Draw the box with predefined colors
|
61 |
+
|
62 |
+
# 02. DRAW LABEL
|
63 |
+
text = cls + " " + str(detection_class_conf)
|
64 |
+
# Get text dimensions to draw wrapping box
|
65 |
+
font_thickness = max(line_thickness - 1, 1)
|
66 |
+
(text_w, text_h), _ = cv2.getTextSize(text=text, fontFace=2, fontScale=line_thickness/3, thickness=font_thickness)
|
67 |
+
# Draw wrapping box for text
|
68 |
+
image = cv2.rectangle(img=image,
|
69 |
+
pt1=(start_box[0], start_box[1] - text_h - BOX_PADDING*2),
|
70 |
+
pt2=(start_box[0] + text_w + BOX_PADDING * 2, start_box[1]),
|
71 |
+
color=ENTITIES_COLORS[cls],
|
72 |
+
thickness=-1)
|
73 |
+
# Put class name on image
|
74 |
+
start_text = (start_box[0] + BOX_PADDING, start_box[1] - BOX_PADDING)
|
75 |
+
image = cv2.putText(img=image, text=text, org=start_text, fontFace=0, color=(255,255,255), fontScale=line_thickness/3, thickness=font_thickness)
|
76 |
+
|
77 |
+
return image
|
78 |
+
|
79 |
+
iface = gr.Interface(fn=detect,
|
80 |
+
inputs=gr.inputs.Image(label="Upload scanned document", type="filepath"),
|
81 |
+
outputs="image")
|
82 |
+
iface.launch()
|
83 |
+
|
84 |
+
|
85 |
+
|
86 |
+
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
+
|
98 |
+
|
models/dla-model.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6c4d16b4321bdd20c7a3d8cf4b4fac86207d92e7686d01ae86eb292c040fb0fa
|
3 |
+
size 22518254
|