-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.py
More file actions
80 lines (65 loc) · 3.01 KB
/
visualizer.py
File metadata and controls
80 lines (65 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import cv2
import numpy as np
from dist_calculator import DistanceCalculator
class Visualizer:
def __init__(self):
self.dist_calc = DistanceCalculator("weights/distance_model.pkl")
self.FONT = cv2.FONT_HERSHEY_SIMPLEX
self.TEXT_WEIGHT = 1
self.BORDER_WIDTH = 2
self.COLOR_RED = (255, 0, 0)
self.COLOR_WHITE = (255, 255, 255)
self.COLOR_GREEN = (0, 255, 0)
self.COLOR_YELLOW = (0, 255, 255)
self.COLOR_BLACK = (0, 0, 0)
def draw_contour(self, original_image, segmentation_mask):
"""
This method draws biggest contour of the segmentation mask
:param original_image: Image where the contour should be drawn
:param segmentation_mask: Inference result from segmentation model
:return: Original image with segmentation mask and contour drawn on it
"""
contours, _ = cv2.findContours(segmentation_mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
original_image[segmentation_mask.astype(np.uint8) > 0] = self.COLOR_GREEN
biggest_area = 0
# Finding the biggest contour
for contour in contours:
area = cv2.contourArea(contour)
if area > biggest_area:
biggest_area = area
self.biggest_contour = contour
original_image = cv2.drawContours(original_image,
self.biggest_contour,
-1,
self.COLOR_BLACK,
self.BORDER_WIDTH)
return original_image
def draw_detection_boxes(self, image, detection_boxes):
"""
This method draws detection boxes along with their approximated
distance in the image if they are inside the detected footpath.
IMPORTANT: THIS METHOD SHOULD BE CALLED AFTER draw_contour() METHOD
OF THIS CLASS.
:param image: Source image to draw.
:param detection_boxes: Detection box co-ordinates from YOLO-COCO inference.
:return: Returns the resultant image after drawing.
"""
for det in detection_boxes:
x, y, w, h, label, confidence = det
# Check if detected object is inside footpath
# +1 --> Inside, 0 --> Edge, -1 --> Outside
flag = cv2.pointPolygonTest(self.biggest_contour, (x, y + h), measureDist=False)
if flag != -1:
cv2.rectangle(image,
(x, y),
(x + w, y + h),
self.COLOR_RED,
self.BORDER_WIDTH)
dist = self.dist_calc.predict(y + h)[0][0]
cv2.putText(image,
label + " " + str(round(dist, 2)),
(x, y + h + 10),
self.FONT, 0.35,
self.COLOR_RED,
self.TEXT_WEIGHT)
return image