1+ #!/usr/bin/env python3
2+
3+ import cv2
4+ import depthai as dai
5+ import numpy as np
6+
7+ # Closer-in minimum depth, disparity range is doubled (from 95 to 190):
8+ extended_disparity = True
9+ # Better accuracy for longer distance, fractional disparity 32-levels:
10+ subpixel = True
11+ # Better handling for occlusions:
12+ lr_check = True
13+
14+ # Create pipeline
15+ pipeline = dai .Pipeline ()
16+
17+ # Define sources and outputs
18+ left = pipeline .create (dai .node .ColorCamera )
19+ right = pipeline .create (dai .node .ColorCamera )
20+ depth = pipeline .create (dai .node .StereoDepth )
21+ xout = pipeline .create (dai .node .XLinkOut )
22+ xoutl = pipeline .create (dai .node .XLinkOut )
23+ xoutr = pipeline .create (dai .node .XLinkOut )
24+
25+ xout .setStreamName ("disparity" )
26+ xoutl .setStreamName ("rectifiedLeft" )
27+ xoutr .setStreamName ("rectifiedRight" )
28+
29+ # Properties
30+ left .setResolution (dai .ColorCameraProperties .SensorResolution .THE_1200_P )
31+ left .setBoardSocket (dai .CameraBoardSocket .LEFT )
32+ right .setResolution (dai .ColorCameraProperties .SensorResolution .THE_1200_P )
33+ right .setBoardSocket (dai .CameraBoardSocket .RIGHT )
34+ right .setIspScale (2 , 3 )
35+ left .setIspScale (2 , 3 )
36+
37+
38+ # Create a node that will produce the depth map (using disparity output as it's easier to visualize depth this way)
39+ depth .setDefaultProfilePreset (dai .node .StereoDepth .PresetMode .HIGH_DENSITY )
40+ # Options: MEDIAN_OFF, KERNEL_3x3, KERNEL_5x5, KERNEL_7x7 (default)
41+ depth .initialConfig .setMedianFilter (dai .MedianFilter .KERNEL_7x7 )
42+ depth .setInputResolution (1280 , 800 )
43+ depth .setLeftRightCheck (lr_check )
44+ depth .setExtendedDisparity (extended_disparity )
45+ depth .setSubpixel (subpixel )
46+ depth .setInputResolution (1280 , 800 )
47+
48+ # Linking
49+ left .isp .link (depth .left )
50+ right .isp .link (depth .right )
51+ depth .disparity .link (xout .input )
52+ depth .rectifiedLeft .link (xoutl .input )
53+ depth .rectifiedRight .link (xoutr .input )
54+
55+ # Connect to device and start pipeline
56+ with dai .Device (pipeline ) as device :
57+ while not device .isClosed ():
58+ queueNames = device .getQueueEvents ()
59+ for q in queueNames :
60+ message = device .getOutputQueue (q ).get ()
61+ # Display arrived frames
62+ if type (message ) == dai .ImgFrame :
63+ frame = message .getCvFrame ()
64+ if 'disparity' in q :
65+ maxDisp = depth .initialConfig .getMaxDisparity ()
66+ disp = (frame * (255.0 / maxDisp )).astype (np .uint8 )
67+ disp = cv2 .applyColorMap (disp , cv2 .COLORMAP_JET )
68+ cv2 .imshow (q , disp )
69+ else :
70+ cv2 .imshow (q , frame )
71+ if cv2 .waitKey (1 ) == ord ('q' ):
72+ break
0 commit comments