-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmask_rcnn.py
133 lines (99 loc) · 4.55 KB
/
mask_rcnn.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import cv2
import numpy as np
class MaskRCNN:
def __init__(self):
# Loading Mask RCNN
self.net = cv2.dnn.readNetFromTensorflow("dnn/frozen_inference_graph_coco.pb",
"dnn/mask_rcnn_inception_v2_coco_2018_01_28.pbtxt")
self.net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
self.net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
# Generate random colors
np.random.seed(2)
self.colors = np.random.randint(0, 255, (90, 3))
# Conf threshold
self.detection_threshold = 0.2
self.mask_threshold = 0.2
self.classes = []
with open("dnn/classes.txt", "r") as file_object:
for class_name in file_object.readlines():
class_name = class_name.strip()
self.classes.append(class_name)
self.obj_boxes = []
self.obj_classes = []
self.obj_centers = []
self.obj_contours = []
# Distances
self.distances = []
def detect_objects_mask(self, bgr_frame):
blob = cv2.dnn.blobFromImage(bgr_frame, swapRB=True)
self.net.setInput(blob)
print(bgr_frame)
boxes, masks = self.net.forward(["detection_out_final", "detection_masks"])
print(boxes)
# Detect objects
frame_height, frame_width, _ = bgr_frame.shape
detection_count = boxes.shape[2]
# Object Boxes
self.obj_boxes = []
self.obj_classes = []
self.obj_centers = []
self.obj_contours = []
for i in range(detection_count):
box = boxes[0, 0, i]
class_id = box[1]
score = box[2]
color = self.colors[int(class_id)]
if score < self.detection_threshold:
continue
# Get box Coordinates
x = int(box[3] * frame_width)
y = int(box[4] * frame_height)
x2 = int(box[5] * frame_width)
y2 = int(box[6] * frame_height)
self.obj_boxes.append([x, y, x2, y2])
cx = (x + x2) // 2
cy = (y + y2) // 2
self.obj_centers.append((cx, cy))
# append class
self.obj_classes.append(class_id)
# Contours
# Get mask coordinates
# Get the mask
mask = masks[i, int(class_id)]
roi_height, roi_width = y2 - y, x2 - x
mask = cv2.resize(mask, (roi_width, roi_height))
_, mask = cv2.threshold(mask, self.mask_threshold, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(np.array(mask, np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
self.obj_contours.append(contours)
return self.obj_boxes, self.obj_classes, self.obj_contours, self.obj_centers
def draw_object_mask(self, bgr_frame):
# loop through the detection
for box, class_id, contours in zip(self.obj_boxes, self.obj_classes, self.obj_contours):
x, y, x2, y2 = box
roi = bgr_frame[y: y2, x: x2]
roi_height, roi_width, _ = roi.shape
color = self.colors[int(class_id)]
roi_copy = np.zeros_like(roi)
for cnt in contours:
# cv2.f(roi, [cnt], (int(color[0]), int(color[1]), int(color[2])))
cv2.drawContours(roi, [cnt], - 1, (int(color[0]), int(color[1]), int(color[2])), 3)
cv2.fillPoly(roi_copy, [cnt], (int(color[0]), int(color[1]), int(color[2])))
roi = cv2.addWeighted(roi, 1, roi_copy, 0.5, 0.0)
bgr_frame[y: y2, x: x2] = roi
return bgr_frame
def draw_object_info(self, bgr_frame, depth_frame):
# loop through the detection
for box, class_id, obj_center in zip(self.obj_boxes, self.obj_classes, self.obj_centers):
x, y, x2, y2 = box
color = self.colors[int(class_id)]
color = (int(color[0]), int(color[1]), int(color[2]))
cx, cy = obj_center
depth_mm = depth_frame[cy, cx]
cv2.line(bgr_frame, (cx, y), (cx, y2), color, 1)
cv2.line(bgr_frame, (x, cy), (x2, cy), color, 1)
class_name = self.classes[int(class_id)]
cv2.rectangle(bgr_frame, (x, y), (x + 250, y + 70), color, -1)
cv2.putText(bgr_frame, class_name.capitalize(), (x + 5, y + 25), 0, 0.8, (255, 255, 255), 2)
cv2.putText(bgr_frame, "{} cm".format(depth_mm / 10), (x + 5, y + 60), 0, 1.0, (255, 255, 255), 2)
cv2.rectangle(bgr_frame, (x, y), (x2, y2), color, 1)
return bgr_frame