-
Notifications
You must be signed in to change notification settings - Fork 1
/
previewer.py
139 lines (111 loc) · 4.23 KB
/
previewer.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
137
138
139
from recorder import Recorder
import cv2
import depthai as dai
import numpy as np
class Previewer(Recorder):
def __createPreviewWindow(self, windowName: str):
self.__depthWeight: int = 50
self.__rgbWeight: int = 0
cv2.namedWindow(windowName)
cv2.createTrackbar(
"Depth %", windowName, self.__depthWeight, 100, self.__updateBlendWeights
)
def __initPreview(self):
self.__depthWeight: int = 100
self.__rgbWeight: int = 0
pipeline = dai.Pipeline()
# Init nodes
rgbCam = pipeline.create(dai.node.ColorCamera)
leftCam = pipeline.create(dai.node.MonoCamera)
rightCam = pipeline.create(dai.node.MonoCamera)
## Preview
stereo = pipeline.create(dai.node.StereoDepth)
rgbOut = pipeline.create(dai.node.XLinkOut)
disparityOut = pipeline.create(dai.node.XLinkOut)
# Link nodes
rgbCam.video.link(rgbOut.input)
leftCam.out.link(stereo.left)
rightCam.out.link(stereo.right)
stereo.disparity.link(disparityOut.input)
# Config cameras
rgbCam.setBoardSocket(dai.CameraBoardSocket.RGB)
leftCam.setBoardSocket(dai.CameraBoardSocket.LEFT)
rightCam.setBoardSocket(dai.CameraBoardSocket.RIGHT)
rgbCam.setResolution(self.rgbResPreset)
leftCam.setResolution(self.monoResPreset)
rightCam.setResolution(self.monoResPreset)
rgbCam.setFps(self.fps)
leftCam.setFps(self.fps)
rightCam.setFps(self.fps)
# Config depth preview
rgbOut.setStreamName("rgbOut")
disparityOut.setStreamName("disparityOut")
stereo.setDefaultProfilePreset(dai.node.StereoDepth.PresetMode.HIGH_DENSITY)
stereo.setLeftRightCheck(True)
stereo.setDepthAlign(dai.CameraBoardSocket.RGB)
maxDisparity = stereo.initialConfig.getMaxDisparity()
self.__pipeline = pipeline
self.__rgbCam = rgbCam
self.__maxDisparity = maxDisparity
def __showPreview(
self,
rgbFrame: cv2.Mat,
disparityFrame: np.ndarray,
scaleFactor: float = 1,
windowsName: str = "",
):
disparityFrame = (disparityFrame * 255.0 / self.__maxDisparity).astype(np.uint8)
disparityFrame = cv2.applyColorMap(disparityFrame, cv2.COLORMAP_BONE)
disparityFrame = np.ascontiguousarray(disparityFrame)
if len(disparityFrame.shape) < 3:
disparityFrame = cv2.cvtColor(disparityFrame, cv2.COLOR_GRAY2BGR)
blended = cv2.addWeighted(
rgbFrame,
float(self.__rgbWeight) / 100,
disparityFrame,
float(self.__depthWeight) / 100,
0,
)
scaledSize = (
int(blended.shape[1] * scaleFactor),
int(blended.shape[0] * scaleFactor),
)
scaled = cv2.resize(blended, scaledSize)
cv2.imshow(windowsName, scaled)
def __updateBlendWeights(self, depthPercent: int):
self.__depthWeight: int = depthPercent
self.__rgbWeight: int = 100 - self.__depthWeight
def preview(self):
self.__initPreview()
with dai.Device(self.__pipeline) as device:
# RGB needs fixed focus to properly align with depth
calibrationbData = device.readCalibration()
lensPosition = calibrationbData.getLensPosition(dai.CameraBoardSocket.RGB)
self.__rgbCam.initialControl.setManualFocus(lensPosition)
print("Previewing... (Press Q on frame or Ctrl+C to stop)")
self.__createPreviewWindow("Depth")
rgbFrame = None
disparityFrame = None
while True:
try:
latestFrame = {"rgbOut": None, "disparityOut": None}
queues = device.getQueueEvents([x for x in latestFrame.keys()])
for queue in queues:
packets = device.getOutputQueue(queue).tryGetAll()
if len(packets) > 0:
latestFrame[queue] = packets[-1]
if latestFrame["rgbOut"] is not None:
rgbFrame = latestFrame["rgbOut"].getCvFrame()
if latestFrame["disparityOut"] is not None:
disparityFrame = latestFrame["disparityOut"].getFrame()
if (rgbFrame is not None) and (disparityFrame is not None):
scaleFactor: float = 1 / 2
self.__showPreview(rgbFrame, disparityFrame, scaleFactor, "Depth")
rgbFrame = None
disparityFrame = None
if cv2.waitKey(1) == ord("q"):
break
except KeyboardInterrupt:
break
print("Stop preview...")
cv2.destroyAllWindows()