6
6
import depthai as dai
7
7
import numpy as np
8
8
9
+ flipRectified = True
10
+
9
11
# Get argument first
10
12
nnPath = str ((Path (__file__ ).parent / Path ('models/mobilenet-ssd_openvino_2021.2_6shave.blob' )).resolve ().absolute ())
11
13
if len (sys .argv ) > 1 :
40
42
depth .setConfidenceThreshold (255 )
41
43
# Note: the rectified streams are horizontally mirrored by default
42
44
depth .setOutputRectified (True )
45
+ depth .setRectifyMirrorFrame (False )
43
46
depth .setRectifyEdgeFillColor (0 ) # Black, to better see the cutout
44
47
camLeft .out .link (depth .left )
45
48
camRight .out .link (depth .right )
49
+ # Disparity range is 0..95, used for normalization
50
+ disparity_multiplier = 255 / 95
46
51
47
- depthOut = pipeline .createXLinkOut ()
48
- depthOut .setStreamName ("depth " )
49
- depth .disparity .link (depthOut .input )
52
+ disparityOut = pipeline .createXLinkOut ()
53
+ disparityOut .setStreamName ("disparity " )
54
+ depth .disparity .link (disparityOut .input )
50
55
51
56
nn = pipeline .createMobileNetDetectionNetwork ()
52
57
nn .setConfidenceThreshold (0.5 )
85
90
86
91
queueSize = 8
87
92
qRight = device .getOutputQueue ("right" , queueSize )
88
- qDepth = device .getOutputQueue ("depth " , queueSize )
93
+ qDisparity = device .getOutputQueue ("disparity " , queueSize )
89
94
qManip = device .getOutputQueue ("manip" , queueSize )
90
95
qDet = device .getOutputQueue ("nn" , queueSize )
91
96
qRgbEnc = device .getOutputQueue ('h265' , maxSize = 30 , blocking = True )
92
97
93
98
frame = None
94
99
frameManip = None
95
- frameDepth = None
100
+ frameDisparity = None
96
101
detections = []
97
102
offsetX = (camRight .getResolutionWidth () - camRight .getResolutionHeight ()) // 2
98
103
croppedFrame = np .zeros ((camRight .getResolutionHeight (), camRight .getResolutionHeight ()))
@@ -111,21 +116,26 @@ def frameNorm(frame, bbox):
111
116
inRight = qRight .tryGet ()
112
117
inManip = qManip .tryGet ()
113
118
inDet = qDet .tryGet ()
114
- inDepth = qDepth .tryGet ()
119
+ inDisparity = qDisparity .tryGet ()
115
120
116
121
while qRgbEnc .has ():
117
122
qRgbEnc .get ().getData ().tofile (videoFile )
118
123
119
124
if inRight is not None :
120
125
frame = cv2 .flip (inRight .getCvFrame (), 1 )
126
+ if flipRectified :
127
+ frame = cv2 .flip (frame , 1 )
121
128
122
129
if inManip is not None :
123
130
frameManip = inManip .getCvFrame ()
124
131
125
- if inDepth is not None :
126
- frameDepth = cv2 .flip (inDepth .getFrame (), 1 )
127
- frameDepth = cv2 .normalize (frameDepth , None , 0 , 255 , cv2 .NORM_MINMAX )
128
- frameDepth = cv2 .applyColorMap (frameDepth , cv2 .COLORMAP_JET )
132
+ if inDisparity is not None :
133
+ # Flip disparity frame, normalize it and apply color map for better visualization
134
+ frameDisparity = inDisparity .getFrame ()
135
+ if flipRectified :
136
+ frameDisparity = cv2 .flip (frameDisparity , 1 )
137
+ frameDisparity = (frameDisparity * disparity_multiplier ).astype (np .uint8 )
138
+ frameDisparity = cv2 .applyColorMap (frameDisparity , cv2 .COLORMAP_JET )
129
139
130
140
if inDet is not None :
131
141
detections = inDet .detections
@@ -139,14 +149,14 @@ def frameNorm(frame, bbox):
139
149
cv2 .putText (frame , f"{ int (detection .confidence * 100 )} %" , (bbox [0 ] + 10 , bbox [1 ] + 40 ), cv2 .FONT_HERSHEY_TRIPLEX , 0.5 , 255 )
140
150
cv2 .imshow ("right" , frame )
141
151
142
- if frameDepth is not None :
152
+ if frameDisparity is not None :
143
153
for detection in detections :
144
154
bbox = frameNorm (croppedFrame , (detection .xmin , detection .ymin , detection .xmax , detection .ymax ))
145
155
bbox [::2 ] += offsetX
146
- cv2 .rectangle (frameDepth , (bbox [0 ], bbox [1 ]), (bbox [2 ], bbox [3 ]), (255 , 0 , 0 ), 2 )
147
- cv2 .putText (frameDepth , labelMap [detection .label ], (bbox [0 ] + 10 , bbox [1 ] + 20 ), cv2 .FONT_HERSHEY_TRIPLEX , 0.5 , 255 )
148
- cv2 .putText (frameDepth , f"{ int (detection .confidence * 100 )} %" , (bbox [0 ] + 10 , bbox [1 ] + 40 ), cv2 .FONT_HERSHEY_TRIPLEX , 0.5 , 255 )
149
- cv2 .imshow ("depth " , frameDepth )
156
+ cv2 .rectangle (frameDisparity , (bbox [0 ], bbox [1 ]), (bbox [2 ], bbox [3 ]), (255 , 0 , 0 ), 2 )
157
+ cv2 .putText (frameDisparity , labelMap [detection .label ], (bbox [0 ] + 10 , bbox [1 ] + 20 ), cv2 .FONT_HERSHEY_TRIPLEX , 0.5 , 255 )
158
+ cv2 .putText (frameDisparity , f"{ int (detection .confidence * 100 )} %" , (bbox [0 ] + 10 , bbox [1 ] + 40 ), cv2 .FONT_HERSHEY_TRIPLEX , 0.5 , 255 )
159
+ cv2 .imshow ("disparity " , frameDisparity )
150
160
151
161
if frameManip is not None :
152
162
for detection in detections :
0 commit comments