66import depthai as dai
77import numpy as np
88
9+
10+ flipRectified = True
11+
912# Get argument first
1013nnPath = str ((Path (__file__ ).parent / Path ('models/mobilenet-ssd_openvino_2021.2_6shave.blob' )).resolve ().absolute ())
1114if len (sys .argv ) > 1 :
@@ -91,6 +94,16 @@ def frameNorm(frame, bbox):
9194 normVals [::2 ] = frame .shape [1 ]
9295 return (np .clip (np .array (bbox ), 0 , 1 ) * normVals ).astype (int )
9396
97+ # Add bounding boxes and text to the frame and show it to the user
98+ def show (name , frame ):
99+ for detection in detections :
100+ bbox = frameNorm (frame , (detection .xmin , detection .ymin , detection .xmax , detection .ymax ))
101+ cv2 .rectangle (frame , (bbox [0 ], bbox [1 ]), (bbox [2 ], bbox [3 ]), (255 , 0 , 0 ), 2 )
102+ cv2 .putText (frame , labelMap [detection .label ], (bbox [0 ] + 10 , bbox [1 ] + 20 ), cv2 .FONT_HERSHEY_TRIPLEX , 0.5 , 255 )
103+ cv2 .putText (frame , f"{ int (detection .confidence * 100 )} %" , (bbox [0 ] + 10 , bbox [1 ] + 40 ), cv2 .FONT_HERSHEY_TRIPLEX , 0.5 , 255 )
104+ # Show the frame
105+ cv2 .imshow (name , frame )
106+
94107 while True :
95108 # Instead of get (blocking), we use tryGet (nonblocking) which will return the available data or None otherwise
96109 inRight = qRight .tryGet ()
@@ -99,32 +112,30 @@ def frameNorm(frame, bbox):
99112
100113 if inRight is not None :
101114 rightFrame = inRight .getCvFrame ()
115+ if flipRectified :
116+ rightFrame = cv2 .flip (rightFrame , 1 )
117+
102118
103119 if inDet is not None :
104120 detections = inDet .detections
121+ if flipRectified :
122+ for detection in detections :
123+ swap = detection .xmin
124+ detection .xmin = 1 - detection .xmax
125+ detection .xmax = 1 - swap
105126
106127 if inDepth is not None :
107128 # Frame is transformed, the color map will be applied to highlight the depth info
108- depthFrame = cv2 .flip (inDepth .getFrame (), 1 )
109129 # Available color maps: https://docs.opencv.org/3.4/d3/d50/group__imgproc__colormap.html
110- depthFrame = cv2 .applyColorMap (depthFrame , cv2 .COLORMAP_JET )
130+ depthFrame = cv2 .applyColorMap (inDepth .getFrame (), cv2 .COLORMAP_JET )
131+
132+ if depthFrame is not None :
133+ show ("depth" , depthFrame )
111134
112135 if rightFrame is not None :
113- for detection in detections :
114- bbox = frameNorm (rightFrame , (detection .xmin , detection .ymin , detection .xmax , detection .ymax ))
115- cv2 .rectangle (rightFrame , (bbox [0 ], bbox [1 ]), (bbox [2 ], bbox [3 ]), (255 , 0 , 0 ), 2 )
116- cv2 .putText (rightFrame , labelMap [detection .label ], (bbox [0 ] + 10 , bbox [1 ] + 20 ), cv2 .FONT_HERSHEY_TRIPLEX , 0.5 , 255 )
117- cv2 .putText (rightFrame , f"{ int (detection .confidence * 100 )} %" , (bbox [0 ] + 10 , bbox [1 ] + 40 ), cv2 .FONT_HERSHEY_TRIPLEX , 0.5 , 255 )
118- cv2 .imshow ("rectified right" , rightFrame )
136+ show ("rectified right" , rightFrame )
119137
120- if depthFrame is not None :
121- for detection in detections :
122- bbox = frameNorm (croppedFrame , (detection .xmin , detection .ymin , detection .xmax , detection .ymax ))
123- bbox [::2 ] += offsetX
124- cv2 .rectangle (depthFrame , (bbox [0 ], bbox [1 ]), (bbox [2 ], bbox [3 ]), (255 , 0 , 0 ), 2 )
125- cv2 .putText (depthFrame , labelMap [detection .label ], (bbox [0 ] + 10 , bbox [1 ] + 20 ), cv2 .FONT_HERSHEY_TRIPLEX , 0.5 , 255 )
126- cv2 .putText (depthFrame , f"{ int (detection .confidence * 100 )} %" , (bbox [0 ] + 10 , bbox [1 ] + 40 ), cv2 .FONT_HERSHEY_TRIPLEX , 0.5 , 255 )
127- cv2 .imshow ("depth" , depthFrame )
138+ detections = []
128139
129140 if cv2 .waitKey (1 ) == ord ('q' ):
130141 break
0 commit comments