omoured commited on
Commit
810fff1
·
verified ·
1 Parent(s): d782b49

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/yolov10x_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
+ line_thickness = round(0.002 * (image.shape[0] + image.shape[1]) / 2) + 1
54
+ image = cv2.rectangle(img=image,
55
+ pt1=start_box,
56
+ pt2=end_box,
57
+ color=ENTITIES_COLORS[cls],
58
+ thickness = line_thickness) # Draw the box with predefined colors
59
+
60
+ # 02. DRAW LABEL
61
+ text = cls + " " + str(detection_class_conf)
62
+ # Get text dimensions to draw wrapping box
63
+ font_thickness = max(line_thickness - 1, 1)
64
+ (text_w, text_h), _ = cv2.getTextSize(text=text, fontFace=2, fontScale=line_thickness/3, thickness=font_thickness)
65
+ # Draw wrapping box for text
66
+ image = cv2.rectangle(img=image,
67
+ pt1=(start_box[0], start_box[1] - text_h - BOX_PADDING*2),
68
+ pt2=(start_box[0] + text_w + BOX_PADDING * 2, start_box[1]),
69
+ color=ENTITIES_COLORS[cls],
70
+ thickness=-1)
71
+ # Put class name on image
72
+ start_text = (start_box[0] + BOX_PADDING, start_box[1] - BOX_PADDING)
73
+ image = cv2.putText(img=image, text=text, org=start_text, fontFace=0, color=(255,255,255), fontScale=line_thickness/3, thickness=font_thickness)
74
+
75
+ return image
76
+
77
+ iface = gr.Interface(fn=detect,
78
+ inputs=gr.Image(label="Upload scanned document", type="filepath"),
79
+ outputs="image")
80
+ iface.launch()