-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProcessing.py
136 lines (82 loc) · 3.46 KB
/
Processing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
import cv2
import os
import depthai as dai
import numpy as np
import time
# Closer-in minimum depth, disparity range is doubled (from 95 to 190):
extended_disparity = False
# Better accuracy for longer distance, fractional disparity 32-levels:
subpixel = False
# Better handling for occlusions:
lr_check = True
blobfolder=os.path.dirname(os.path.abspath(__file__))+"/scripts/Blob"
blobfilename=os.listdir(blobfolder)[0]
blobpath=blobfolder+"/"+blobfilename
if not os.path.isfile(blobpath):
blobpath=blobpath+"/"+os.listdir(blobpath)[0]
if blobfilename==".gitkeep":
raise Exception("Blob file has not been correctly stored inside scripts/Blob folder")
def getFrame(queue):
frame = queue.get()
return frame.getCvFrame()
# Create pipeline
pipeline = dai.Pipeline()
pipeline.setOpenVINOVersion(dai.OpenVINO.VERSION_2021_4)
# Define sources and outputs
monoLeft = pipeline.create(dai.node.MonoCamera)
monoRight = pipeline.create(dai.node.MonoCamera)
manipLeft = pipeline.create(dai.node.ImageManip)
manipRight = pipeline.create(dai.node.ImageManip)
manipLeft2 = pipeline.create(dai.node.ImageManip)
manipRight2 = pipeline.create(dai.node.ImageManip)
depth = pipeline.create(dai.node.StereoDepth)
depth.setDefaultProfilePreset(dai.node.StereoDepth.PresetMode.HIGH_DENSITY)
depth.initialConfig.setMedianFilter(dai.MedianFilter.KERNEL_7x7)
depth.setLeftRightCheck(lr_check)
depth.setExtendedDisparity(extended_disparity)
depth.setSubpixel(subpixel)
config = depth.initialConfig.get()
config.postProcessing.speckleFilter.enable = False
config.postProcessing.speckleFilter.speckleRange = 50
config.postProcessing.temporalFilter.enable = True
config.postProcessing.spatialFilter.enable = True
config.postProcessing.spatialFilter.holeFillingRadius = 2
config.postProcessing.spatialFilter.numIterations = 1
config.postProcessing.thresholdFilter.minRange = 400
config.postProcessing.thresholdFilter.maxRange = 15000
config.postProcessing.decimationFilter.decimationFactor = 1
depth.initialConfig.set(config)
monoLeft.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
monoLeft.setBoardSocket(dai.CameraBoardSocket.LEFT)
monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
monoRight.setBoardSocket(dai.CameraBoardSocket.RIGHT)
monoLeft.out.link(manipLeft.inputImage)
monoRight.out.link(manipRight.inputImage)
manipLeft.initialConfig.setResize(288, 300)
manipRight.initialConfig.setResize(288, 300)
manipLeft2.initialConfig.setResize(640, 400)
manipRight2.initialConfig.setResize(640, 400)
nnL = pipeline.create(dai.node.NeuralNetwork)
nnL.setBlobPath(blobpath)
manipLeft.out.link(nnL.input)
nnR = pipeline.create(dai.node.NeuralNetwork)
nnR.setBlobPath(blobpath)
manipRight.out.link(nnR.input)
nnL.passthrough.link(manipLeft2.inputImage)
nnR.passthrough.link(manipRight2.inputImage)
manipLeft2.out.link(depth.left)
manipRight2.out.link(depth.right)
# print("sj")
xout = pipeline.create(dai.node.XLinkOut)
xout.setStreamName("disparity")
depth.disparity.link(xout.input)
with dai.Device(pipeline) as device:
q = device.getOutputQueue(name="disparity", maxSize=1, blocking=False)
while True:
frame = getFrame(q)
frame = (frame * (255 / depth.initialConfig.getMaxDisparity())).astype(np.uint8)
frame = cv2.applyColorMap(frame, cv2.COLORMAP_MAGMA)
cv2.imshow("Stereo_Processed", frame)
if cv2.waitKey(1) == ord('q'):
break