jaimin commited on
Commit
d790a81
1 Parent(s): 26f1b23

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +83 -0
  2. classifier-model.pt +3 -0
  3. detector-model.pt +3 -0
  4. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ BOX_COLORS = {
8
+ "unchecked": (242, 48, 48),
9
+ "checked": (38, 115, 101),
10
+ "block": (242, 159, 5)
11
+ }
12
+ BOX_PADDING = 2
13
+
14
+ # Load models
15
+ DETECTION_MODEL = YOLO("models/detector-model.pt")
16
+ CLASSIFICATION_MODEL = YOLO("models/classifier-model.pt") # 0: block, 1: checked, 2: unchecked
17
+
18
+ def detect(image_path):
19
+ """
20
+ Output inference image with bounding box
21
+
22
+ Args:
23
+ - image: to check for checkboxes
24
+
25
+ Return: image with bounding boxes drawn
26
+ """
27
+ image = cv2.imread(image_path)
28
+ if image is None:
29
+ return image
30
+
31
+ # Predict on image
32
+ results = DETECTION_MODEL.predict(source=image, conf=0.2, iou=0.8) # Predict on image
33
+ boxes = results[0].boxes # Get bounding boxes
34
+
35
+ if len(boxes) == 0:
36
+ return image
37
+
38
+ # Get bounding boxes
39
+ for box in boxes:
40
+ detection_class_conf = round(box.conf.item(), 2)
41
+ detection_class = list(BOX_COLORS)[int(box.cls)]
42
+ # Get start and end points of the current box
43
+ start_box = (int(box.xyxy[0][0]), int(box.xyxy[0][1]))
44
+ end_box = (int(box.xyxy[0][2]), int(box.xyxy[0][3]))
45
+ box = image[start_box[1]:end_box[1], start_box[0]: end_box[0], :]
46
+
47
+ # Determine the class of the box using classification model
48
+ cls_results = CLASSIFICATION_MODEL.predict(source=box, conf=0.5)
49
+ probs = cls_results[0].probs # cls prob, (num_class, )
50
+ classification_class = list(BOX_COLORS)[2 - int(probs.top1)]
51
+ classification_class_conf = round(probs.top1conf.item(), 2)
52
+
53
+ cls = classification_class if classification_class_conf > 0.9 else detection_class
54
+
55
+ # 01. DRAW BOUNDING BOX OF OBJECT
56
+ line_thickness = round(0.002 * (image.shape[0] + image.shape[1]) / 2) + 1
57
+ image = cv2.rectangle(img=image,
58
+ pt1=start_box,
59
+ pt2=end_box,
60
+ color=BOX_COLORS[cls],
61
+ thickness = line_thickness) # Draw the box with predefined colors
62
+
63
+ # 02. DRAW LABEL
64
+ text = cls + " " + str(detection_class_conf)
65
+ # Get text dimensions to draw wrapping box
66
+ font_thickness = max(line_thickness - 1, 1)
67
+ (text_w, text_h), _ = cv2.getTextSize(text=text, fontFace=2, fontScale=line_thickness/3, thickness=font_thickness)
68
+ # Draw wrapping box for text
69
+ image = cv2.rectangle(img=image,
70
+ pt1=(start_box[0], start_box[1] - text_h - BOX_PADDING*2),
71
+ pt2=(start_box[0] + text_w + BOX_PADDING * 2, start_box[1]),
72
+ color=BOX_COLORS[cls],
73
+ thickness=-1)
74
+ # Put class name on image
75
+ start_text = (start_box[0] + BOX_PADDING, start_box[1] - BOX_PADDING)
76
+ image = cv2.putText(img=image, text=text, org=start_text, fontFace=0, color=(255,255,255), fontScale=line_thickness/3, thickness=font_thickness)
77
+
78
+ return image
79
+
80
+ iface = gr.Interface(fn=detect,
81
+ inputs=gr.inputs.Image(label="Upload scanned document", type="filepath"),
82
+ outputs="image")
83
+ iface.launch()
classifier-model.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f4a0af5bf0ccd1ca422775a744a80b0916ffb0a6427828b5caaa4bd954e462b4
3
+ size 10377603
detector-model.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9c7c3b6f1858e045cec68db2fd3b2c28c6d67fa937ed34cf2a20231dc3ea2e8e
3
+ size 87617854
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ ultralytics
2
+ opencv-python