@@ -48,6 +48,30 @@ def is_inside_aoi(x1, y1, x2, y2, aoi_info):
48
48
return True
49
49
return False
50
50
51
+ def parse_bbox (prediction , width , height ):
52
+ x1 = int (prediction ['boundingBox' ]['left' ] * width )
53
+ y1 = int (prediction ['boundingBox' ]['top' ] * height )
54
+ x2 = x1 + int (prediction ['boundingBox' ]['width' ] * width )
55
+ y2 = y1 + int (prediction ['boundingBox' ]['height' ] * height )
56
+ return (x1 , y1 ), (x2 , y2 )
57
+
58
+
59
+ def draw_confidence_level (img , prediction ):
60
+ height , width = img .shape [0 ], img .shape [1 ]
61
+
62
+ font = cv2 .FONT_HERSHEY_SIMPLEX
63
+ font_scale = 1
64
+ thickness = 3
65
+
66
+ prob_str = str (int (prediction ['probability' ]* 1000 )/ 10 )
67
+ prob_str = ' (' + prob_str + '%)'
68
+
69
+ (x1 , y1 ), (x2 , y2 ) = parse_bbox (prediction , width , height )
70
+
71
+ img = cv2 .putText (img , prediction ['tagName' ]+ prob_str , (x1 + 10 , y1 + 30 ), font , font_scale , (0 , 0 , 255 ), thickness )
72
+
73
+ return img
74
+
51
75
52
76
class ONNXRuntimeModelDeploy (ObjectDetection ):
53
77
"""Object Detection class for ONNX Runtime
@@ -225,20 +249,8 @@ def run(self):
225
249
detection = DETECTION_TYPE_NOTHING
226
250
if True :
227
251
send_counter += 1
228
- # Modify here to change the threshold (deprecated)
229
- #if send_counter == 200:
230
- # if iot:
231
- # iot.send_message_to_output(json.dumps(self.last_prediction), 'metrics')
232
- # else:
233
- # print('[METRICS]', json.dumps(self.last_prediction))
234
- # send_counter = 0
235
252
if self .iothub_is_send :
236
253
if self .iothub_last_send_time + self .iothub_interval < time .time ():
237
- #print('1', self.iothub_last_send_time)
238
- #print('2', self.iothub_interval)
239
- #print('3', self.iothub_last_send_time + self.iothub_interval)
240
- #print('4', time.time())
241
- #print('wew')
242
254
predictions_to_send = []
243
255
for prediction in self .last_prediction :
244
256
_tag = prediction ['tagName' ]
@@ -262,40 +274,38 @@ def run(self):
262
274
pass
263
275
self .iothub_last_send_time = time .time ()
264
276
265
-
266
-
267
-
268
277
for prediction in self .last_prediction :
269
278
270
279
tag = prediction ['tagName' ]
271
- if tag not in self .parts : continue
272
-
273
- if self .last_upload_time + UPLOAD_INTERVAL < time .time ():
274
- x1 = int (prediction ['boundingBox' ]['left' ] * width )
275
- y1 = int (prediction ['boundingBox' ]['top' ] * height )
276
- x2 = x1 + int (prediction ['boundingBox' ]['width' ] * width )
277
- y2 = y1 + int (prediction ['boundingBox' ]['height' ] * height )
278
- labels = json .dumps ([{'x1' : x1 , 'x2' : x2 , 'y1' : y1 , 'y2' : y2 }])
280
+ if tag not in self .parts :
281
+ continue
279
282
280
- if self .has_aoi :
281
- if not is_inside_aoi (x1 , y1 , x2 , y2 , self .aoi_info ): continue
283
+ (x1 , y1 ) , (x2 , y2 ) = parse_bbox (prediction , width , height )
284
+ if self .has_aoi :
285
+ if not is_inside_aoi (x1 , y1 , x2 , y2 , self .aoi_info ): continue
282
286
283
- if prediction ['probability' ] > self .confidence_max :
287
+ if detection != DETECTION_TYPE_SUCCESS :
288
+ if prediction ['probability' ] >= self .threshold :
284
289
detection = DETECTION_TYPE_SUCCESS
285
-
286
- elif self .confidence_min <= prediction ['probability' ] <= self .confidence_max :
287
-
288
- if detection != DETECTION_TYPE_SUCCESS : detection = DETECTION_TYPE_UNIDENTIFIED
290
+ else :
291
+ detection = DETECTION_TYPE_UNIDENTIFIED
289
292
290
293
294
+ if self .last_upload_time + UPLOAD_INTERVAL < time .time ():
295
+ if self .confidence_min <= prediction ['probability' ] <= self .confidence_max :
291
296
if self .is_upload_image :
292
- if tag in onnx .current_uploaded_images and self .current_uploaded_images [tag ] >= onnx .max_images :
293
- pass
294
- else :
295
- self .current_uploaded_images [tag ] = self .current_uploaded_images .get (tag , 0 ) + 1
296
- #print(tag, onnx.current_uploaded_images[tag], j)
297
+ #if tag in onnx.current_uploaded_images and self.current_uploaded_images[tag] >= onnx.max_images:
298
+ #if tag in onnx.current_uploaded_images:
299
+ # No limit for the max_images in inference module now, the logic is moved to webmodule
300
+ # pass
301
+ #else:
302
+ if True :
303
+
304
+ labels = json .dumps ([{'x1' : x1 , 'x2' : x2 , 'y1' : y1 , 'y2' : y2 }])
305
+ print ('[INFO] Sending Image to relabeling' , tag , onnx .current_uploaded_images .get (tag , 0 ), labels )
306
+ #self.current_uploaded_images[tag] = self.current_uploaded_images.get(tag, 0) + 1
297
307
self .last_upload_time = time .time ()
298
- print ( '[INFO] Sending Image to relabeling' , tag , onnx . current_uploaded_images [ tag ], labels )
308
+
299
309
jpg = cv2 .imencode ('.jpg' , img )[1 ].tobytes ()
300
310
try :
301
311
requests .post ('http://' + web_module_url ()+ '/api/relabel' , data = {
@@ -324,15 +334,14 @@ def run(self):
324
334
self .detection_unidentified_num += 1
325
335
elif detection == DETECTION_TYPE_SUCCESS :
326
336
self .detection_success_num += 1
327
-
328
337
else :
329
338
self .detections .append (detection )
330
339
if detection == DETECTION_TYPE_UNIDENTIFIED :
331
340
self .detection_unidentified_num += 1
332
341
elif detection == DETECTION_TYPE_SUCCESS :
333
342
self .detection_success_num += 1
334
343
self .detection_total += 1
335
-
344
+
336
345
self .lock .release ()
337
346
#print(detection)
338
347
else :
@@ -389,7 +398,7 @@ def metrics():
389
398
390
399
@app .route ('/update_retrain_parameters' )
391
400
def update_retrain_parameters ():
392
-
401
+
393
402
confidence_min = request .args .get ('confidence_min' )
394
403
if not confidence_min : return 'missing confidence_min'
395
404
@@ -485,15 +494,10 @@ def update_parts():
485
494
486
495
return 'ok'
487
496
488
- @app .route ('/update_threshold' )
489
- def update_threshold ():
490
- #threshold = float(request.args.get('threshold'))
491
-
492
- #print('[INFO] update theshold to', threshold)
493
-
494
- #onnx.threshold = threshold
495
- print ('[WARNING] is depreciated' )
496
- return 'ok'
497
+ #@app.route('/update_threshold')
498
+ #def update_threshold():
499
+ # print('[WARNING] is depreciated')
500
+ # return 'ok'
497
501
498
502
@app .route ('/update_iothub_parameters' )
499
503
def update_iothub_parameters ():
@@ -527,6 +531,13 @@ def update_prob_threshold():
527
531
print ('[INFO] updaing prob_threshold to' )
528
532
print (' prob_threshold:' , prob_threshold )
529
533
534
+ onnx .lock .acquire ()
535
+ onnx .detection_success_num = 0
536
+ onnx .detection_unidentified_num = 0
537
+ onnx .detection_total = 0
538
+ onnx .detections = []
539
+ onnx .lock .release ()
540
+
530
541
return 'ok'
531
542
532
543
@app .route ('/video_feed' )
@@ -539,9 +550,6 @@ def _gen():
539
550
if inference :
540
551
height , width = img .shape [0 ], img .shape [1 ]
541
552
predictions = onnx .last_prediction
542
- font = cv2 .FONT_HERSHEY_SIMPLEX
543
- font_scale = 1
544
- thickness = 3
545
553
for prediction in predictions :
546
554
tag = prediction ['tagName' ]
547
555
if tag not in onnx .parts : continue
@@ -550,33 +558,22 @@ def _gen():
550
558
for aoi_area in onnx .aoi_info :
551
559
img = cv2 .rectangle (img , (int (aoi_area ['x1' ]), int (aoi_area ['y1' ])), (int (aoi_area ['x2' ]), int (aoi_area ['y2' ])), (0 , 255 , 255 ), 2 )
552
560
553
- #if prediction['probability'] > onnx.confidence_max:
554
- #print(prediction)
555
561
if prediction ['probability' ] > onnx .threshold :
556
- x1 = int (prediction ['boundingBox' ]['left' ] * width )
557
- y1 = int (prediction ['boundingBox' ]['top' ] * height )
558
- x2 = x1 + int (prediction ['boundingBox' ]['width' ] * width )
559
- y2 = y1 + int (prediction ['boundingBox' ]['height' ] * height )
562
+ (x1 , y1 ), (x2 , y2 ) = parse_bbox (prediction , width , height )
560
563
if onnx .has_aoi :
561
564
if not is_inside_aoi (x1 , y1 , x2 , y2 , onnx .aoi_info ): continue
562
565
563
566
img = cv2 .rectangle (img , (x1 , y1 ), (x2 , y2 ), (0 , 0 , 255 ), 2 )
564
- prob_str = str (int (prediction ['probability' ]* 1000 )/ 10 )
565
- prob_str = ' (' + prob_str + '%)'
566
- img = cv2 .putText (img , prediction ['tagName' ]+ prob_str , (x1 + 10 , y1 + 30 ), font , font_scale , (0 , 0 , 255 ), thickness )
567
+ img = draw_confidence_level (img , prediction )
567
568
568
569
569
- time .sleep (0.03 )
570
+ time .sleep (0.02 )
570
571
yield (b'--frame\r \n '
571
572
b'Content-Type: image/jpeg\r \n \r \n ' + cv2 .imencode ('.jpg' , img )[1 ].tobytes () + b'\r \n ' )
572
573
573
574
return Response (_gen (),
574
575
mimetype = 'multipart/x-mixed-replace; boundary=frame' )
575
576
576
- # for debugging
577
- #onnx.update_cam(cam_type='rtsp', cam_source='sample_video/video.mp4', has_aoi=True, aoi_info={'x1': 100, 'x2': 1000, 'y1': 100, 'y2': 500})
578
- #requests.get('http://localhost:5000/update_cam', params={'cam_type':'rtsp', 'cam_source':'0', 'aoi':'{\"useAOI\":true,\"AOIs\":[{\"x1\":100,\"y1\":216.36363636363637,\"x2\":3314.909090909091,\"y2\":1762.181818181818}]}'})
579
-
580
577
581
578
def main ():
582
579
0 commit comments