linhdo commited on
Commit
4dc4616
·
1 Parent(s): b6057af

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -95
app.py DELETED
@@ -1,95 +0,0 @@
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("runs/detect/train_all_classes/best_yolov8l_640_with_background_mixup50.pt")
16
- CLASSIFICATION_MODEL = YOLO("runs/classify/train/weights/best.pt") # 0: block, 1: checked, 2: unchecked
17
-
18
- def detect(image):
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
- # Predict on image
28
- results = DETECTION_MODEL.predict(source=image, conf=0.2, iou=0.8) # Predict on image
29
- boxes = results[0].boxes # Get bounding boxes
30
-
31
- if len(boxes) == 0:
32
- return image
33
-
34
- # Get bounding boxes
35
- for box in boxes:
36
- detection_class_conf = round(box.conf.item(), 2)
37
- detection_class = list(BOX_COLORS)[int(box.cls)]
38
- # Get start and end points of the current box
39
- start_box = (int(box.xyxy[0][0]), int(box.xyxy[0][1]))
40
- end_box = (int(box.xyxy[0][2]), int(box.xyxy[0][3]))
41
- box = image[start_box[1]:end_box[1], start_box[0]: end_box[0], :]
42
-
43
- # Determine the class of the box using classification model
44
- cls_results = CLASSIFICATION_MODEL.predict(source=box, conf=0.5)
45
- probs = cls_results[0].probs # cls prob, (num_class, )
46
- classification_class = list(BOX_COLORS)[2 - int(probs.top1)]
47
- classification_class_conf = round(probs.top1conf.item(), 2)
48
-
49
- cls = classification_class if classification_class_conf > 0.9 else detection_class
50
-
51
- # 01. DRAW BOUNDING BOX OF OBJECT
52
- line_thickness = round(0.002 * (image.shape[0] + image.shape[1]) / 2) + 1
53
- image = cv2.rectangle(img=image,
54
- pt1=start_box,
55
- pt2=end_box,
56
- color=BOX_COLORS[cls],
57
- thickness = line_thickness) # Draw the box with predefined colors
58
-
59
- # 02. DRAW LABEL
60
- text = cls + " " + str(detection_class_conf)
61
- # Get text dimensions to draw wrapping box
62
- font_thickness = max(line_thickness - 1, 1)
63
- (text_w, text_h), _ = cv2.getTextSize(text=text, fontFace=2, fontScale=line_thickness/3, thickness=font_thickness)
64
- # Draw wrapping box for text
65
- image = cv2.rectangle(img=image,
66
- pt1=(start_box[0], start_box[1] - text_h - BOX_PADDING*2),
67
- pt2=(start_box[0] + text_w + BOX_PADDING * 2, start_box[1]),
68
- color=BOX_COLORS[cls],
69
- thickness=-1)
70
- # Put class name on image
71
- start_text = (start_box[0] + BOX_PADDING, start_box[1] - BOX_PADDING)
72
- image = cv2.putText(img=image, text=text, org=start_text, fontFace=0, color=(255,255,255), fontScale=line_thickness/3, thickness=font_thickness)
73
-
74
- return image
75
-
76
- iface = gr.Interface(fn=detect,
77
- inputs=gr.inputs.Image(label="Upload scanned document", type="filepath"),
78
- outputs="image")
79
- iface.launch()
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-