70 self.
mean = [127.5, 127.5, 127.5]
82 backend = cv.dnn.DNN_BACKEND_OPENCV
83 target = cv.dnn.DNN_TARGET_CPU
86 if (model ==
'MobileNetSSD'):
87 classnames = pyjevois.share +
'/darknet/yolo/data/voc.names'
88 modelname = pyjevois.share +
'/opencv-dnn/detection/MobileNetSSD_deploy.caffemodel'
89 configname = pyjevois.share +
'/opencv-dnn/detection/MobileNetSSD_deploy.prototxt'
91 elif (model ==
'MobileNetV2SSD'):
92 classnames = pyjevois.share +
'/darknet/yolo/data/coco.names'
93 modelname = pyjevois.share +
'/opencv-dnn/detection/ssd_mobilenet_v2_coco_2018_03_29.pb'
94 configname = pyjevois.share +
'/opencv-dnn/detection/ssd_mobilenet_v2_coco_2018_03_29.pbtxt'
95 elif (model ==
'MobileNetSSDcoco'):
96 classnames = pyjevois.share +
'/darknet/yolo/data/coco.names'
97 modelname = pyjevois.share +
'/opencv-dnn/detection/ssd_mobilenet_v1_coco_2017_11_17.pb'
98 configname = pyjevois.share +
'/opencv-dnn/detection/ssd_mobilenet_v1_coco_2017_11_17.pbtxt'
101 elif (model ==
'YOLOv3'):
102 classnames = pyjevois.share +
'/darknet/yolo/data/coco.names'
103 modelname = pyjevois.share +
'/darknet/yolo/weights/yolov3-tiny.weights'
104 configname = pyjevois.share +
'/darknet/yolo/cfg/yolov3-tiny.cfg'
105 elif (model ==
'YOLOv2'):
106 classnames = pyjevois.share +
'/darknet/yolo/data/voc.names'
107 modelname = pyjevois.share +
'/darknet/yolo/weights/yolov2-tiny-voc.weights'
108 configname = pyjevois.share +
'/darknet/yolo/cfg/yolov2-tiny-voc.cfg'
112 classnames = pyjevois.share +
'/opencv-dnn/detection/opencv_face_detector.classes'
113 modelname = pyjevois.share +
'/opencv-dnn/detection/opencv_face_detector.caffemodel'
114 configname = pyjevois.share +
'/opencv-dnn/detection/opencv_face_detector.prototxt'
116 self.
mean = [104.0, 177.0, 123.0]
121 with open(classnames,
'rt')
as f:
122 self.
classes = f.read().rstrip(
'\n').split(
'\n')
125 self.
net = cv.dnn.readNet(modelname, configname)
126 self.
net.setPreferableBackend(backend)
127 self.
net.setPreferableTarget(target)
135 frameHeight = frame.shape[0]
136 frameWidth = frame.shape[1]
138 def drawPred(classId, conf, left, top, right, bottom):
140 cv.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
142 label =
'%.2f' % (conf * 100)
146 if (classId >= len(self.
classes)):
147 label =
'Oooops id=%d: %s' % (classId, label)
149 label =
'%s: %s' % (self.
classes[classId], label)
151 labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.4, 1)
152 top = max(top, labelSize[1])
153 cv.rectangle(frame, (left, top - labelSize[1]-2), (left + labelSize[0], top + baseLine),
154 (255, 255, 255), cv.FILLED)
155 cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 0))
157 layerNames = self.
net.getLayerNames()
158 lastLayerId = self.
net.getLayerId(layerNames[-1])
159 lastLayer = self.
net.getLayer(lastLayerId)
164 if self.
net.getLayer(0).outputNameToIndex(
'im_info') != -1:
169 for detection
in out[0, 0]:
170 confidence = detection[2]
172 left = int(detection[3])
173 top = int(detection[4])
174 right = int(detection[5])
175 bottom = int(detection[6])
176 width = right - left + 1
177 height = bottom - top + 1
178 classIds.append(int(detection[1]) - 1)
179 confidences.append(float(confidence))
180 boxes.append([left, top, width, height])
181 elif lastLayer.type ==
'DetectionOutput':
186 for detection
in out[0, 0]:
187 confidence = detection[2]
189 left = int(detection[3] * frameWidth)
190 top = int(detection[4] * frameHeight)
191 right = int(detection[5] * frameWidth)
192 bottom = int(detection[6] * frameHeight)
193 width = right - left + 1
194 height = bottom - top + 1
195 classIds.append(int(detection[1]) - 1)
196 confidences.append(float(confidence))
197 boxes.append([left, top, width, height])
198 elif lastLayer.type ==
'Region':
206 for detection
in out:
207 scores = detection[5:]
208 classId = np.argmax(scores)
209 confidence = scores[classId]
211 center_x = int(detection[0] * frameWidth)
212 center_y = int(detection[1] * frameHeight)
213 width = int(detection[2] * frameWidth)
214 height = int(detection[3] * frameHeight)
215 left = int(center_x - width / 2)
216 top = int(center_y - height / 2)
217 classIds.append(classId)
218 confidences.append(float(confidence))
219 boxes.append([left, top, width, height])
221 jevois.LERROR(
'Unknown output layer type: ' + lastLayer.type)
228 classIds = np.array(classIds)
229 boxes = np.array(boxes)
230 confidences = np.array(confidences)
231 unique_classes = set(classIds)
232 for cl
in unique_classes:
233 class_indices = np.where(classIds == cl)[0]
234 conf = confidences[class_indices]
235 box = boxes[class_indices].tolist()
238 indices.extend(class_indices[nms_indices])
240 indices = np.arange(0, len(classIds))
248 drawPred(classIds[i], confidences[i], left, top, left + width, top + height)
253 frame = inframe.getCvBGR()
256 frameHeight = frame.shape[0]
257 frameWidth = frame.shape[1]
263 self.
net.setInput(blob)
264 if self.
net.getLayer(0).outputNameToIndex(
'im_info') != -1:
272 msgbox = np.zeros((22, frame.shape[1], 3), dtype = np.uint8) + 80
275 cv.putText(frame,
'JeVois Python Object Detection DNN - ' + self.
model, (3, 15),
276 cv.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, cv.LINE_AA)
277 t, _ = self.
net.getPerfProfile()
278 fps = self.
timer.stop()
279 label = fps +
' - Inference time: %.2fms' % (t * 1000.0 / cv.getTickFrequency())
280 cv.putText(msgbox, label, (3, 15), cv.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, cv.LINE_AA)
283 frame = np.vstack((frame, msgbox))
286 outframe.sendCv(frame)