1+ #!/usr/bin/env python3
2+ import depthai as dai
3+ import cv2
4+ from pathlib import Path
5+ import numpy as np
6+
7+ parentDir = Path (__file__ ).parent
8+ nnPath = str ((parentDir / Path ('../models/mobilenet-ssd_openvino_2021.4_5shave.blob' )).resolve ().absolute ())
9+
10+ pipeline = dai .Pipeline ()
11+
12+ cam = pipeline .createColorCamera ()
13+ cam .setBoardSocket (dai .CameraBoardSocket .RGB )
14+ cam .setIspScale (2 ,3 )
15+ cam .setVideoSize (720 ,720 )
16+ cam .setPreviewSize (300 ,300 )
17+
18+ xoutRgb = pipeline .create (dai .node .XLinkOut )
19+ xoutRgb .setStreamName ('rgb' )
20+ cam .video .link (xoutRgb .input )
21+
22+ script = pipeline .createScript ()
23+
24+ xin = pipeline .create (dai .node .XLinkIn )
25+ xin .setStreamName ('in' )
26+ xin .out .link (script .inputs ['toggle' ])
27+
28+ cam .preview .link (script .inputs ['rgb' ])
29+ script .setScript ("""
30+ toggle = True
31+ while True:
32+ msg = node.io['toggle'].tryGet()
33+ if msg is not None:
34+ toggle = not toggle
35+ node.warn('Toggle! Perform NN inferencing: ' + str(toggle))
36+
37+ frame = node.io['rgb'].get()
38+
39+ if toggle:
40+ node.io['nn'].send(frame)
41+ """ )
42+
43+ nn = pipeline .create (dai .node .MobileNetDetectionNetwork )
44+ nn .setBlobPath (nnPath )
45+ script .outputs ['nn' ].link (nn .input )
46+
47+ xoutNn = pipeline .create (dai .node .XLinkOut )
48+ xoutNn .setStreamName ('nn' )
49+ nn .out .link (xoutNn .input )
50+
51+ # Connect to device with pipeline
52+ with dai .Device (pipeline ) as device :
53+ inQ = device .getInputQueue ("in" )
54+ qRgb = device .getOutputQueue ("rgb" )
55+ qNn = device .getOutputQueue ("nn" )
56+
57+ def frameNorm (frame , bbox ):
58+ normVals = np .full (len (bbox ), frame .shape [0 ])
59+ normVals [::2 ] = frame .shape [1 ]
60+ return (np .clip (np .array (bbox ), 0 , 1 ) * normVals ).astype (int )
61+
62+ def drawDetections (frame , detections ):
63+ color = (255 , 0 , 0 )
64+ for detection in detections :
65+ bbox = frameNorm (frame , (detection .xmin , detection .ymin , detection .xmax , detection .ymax ))
66+ cv2 .putText (frame , f"{ int (detection .confidence * 100 )} %" , (bbox [0 ] + 10 , bbox [1 ] + 40 ), cv2 .FONT_HERSHEY_TRIPLEX , 0.5 , color )
67+ cv2 .rectangle (frame , (bbox [0 ], bbox [1 ]), (bbox [2 ], bbox [3 ]), color , 2 )
68+
69+ def send_trigger ():
70+ inQ .send (dai .Buffer ())
71+
72+ while True :
73+ frame = qRgb .get ().getCvFrame ()
74+
75+ if qNn .has ():
76+ detections = qNn .get ().detections
77+ drawDetections (frame , detections )
78+
79+ cv2 .imshow ('Color frame' , frame )
80+
81+ key = cv2 .waitKey (1 )
82+ if key == ord ('q' ):
83+ break
84+ elif key == ord ('t' ):
85+ print ('send trigger to IR filter' )
86+ send_trigger ()
0 commit comments