forked from luxonis/depthai-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
92 lines (76 loc) · 3.07 KB
/
main.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
#!/usr/bin/env python3
import cv2
import depthai as dai
from calc import HostSpatialsCalc
from utility import *
import numpy as np
import math
# Create pipeline
pipeline = dai.Pipeline()
# Define sources and outputs
monoLeft = pipeline.create(dai.node.MonoCamera)
monoRight = pipeline.create(dai.node.MonoCamera)
stereo = pipeline.create(dai.node.StereoDepth)
# Properties
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)
stereo.initialConfig.setConfidenceThreshold(255)
stereo.setLeftRightCheck(True)
stereo.setSubpixel(False)
# Linking
monoLeft.out.link(stereo.left)
monoRight.out.link(stereo.right)
xoutDepth = pipeline.create(dai.node.XLinkOut)
xoutDepth.setStreamName("depth")
stereo.depth.link(xoutDepth.input)
xoutDepth = pipeline.create(dai.node.XLinkOut)
xoutDepth.setStreamName("disp")
stereo.disparity.link(xoutDepth.input)
# Connect to device and start pipeline
with dai.Device(pipeline) as device:
# Output queue will be used to get the depth frames from the outputs defined above
depthQueue = device.getOutputQueue(name="depth")
dispQ = device.getOutputQueue(name="disp")
text = TextHelper()
hostSpatials = HostSpatialsCalc(device)
y = 200
x = 300
step = 3
delta = 5
hostSpatials.setDeltaRoi(delta)
print("Use WASD keys to move ROI.\nUse 'r' and 'f' to change ROI size.")
while True:
depthFrame = depthQueue.get().getFrame()
# Calculate spatial coordiantes from depth frame
spatials, centroid = hostSpatials.calc_spatials(depthFrame, (x,y)) # centroid == x/y in our case
# Get disparity frame for nicer depth visualization
disp = dispQ.get().getFrame()
disp = (disp * (255 / stereo.initialConfig.getMaxDisparity())).astype(np.uint8)
disp = cv2.applyColorMap(disp, cv2.COLORMAP_JET)
text.rectangle(disp, (x-delta, y-delta), (x+delta, y+delta))
text.putText(disp, "X: " + ("{:.1f}m".format(spatials['x']/1000) if not math.isnan(spatials['x']) else "--"), (x + 10, y + 20))
text.putText(disp, "Y: " + ("{:.1f}m".format(spatials['y']/1000) if not math.isnan(spatials['y']) else "--"), (x + 10, y + 35))
text.putText(disp, "Z: " + ("{:.1f}m".format(spatials['z']/1000) if not math.isnan(spatials['z']) else "--"), (x + 10, y + 50))
# Show the frame
cv2.imshow("depth", disp)
key = cv2.waitKey(1)
if key == ord('q'):
break
elif key == ord('w'):
y -= step
elif key == ord('a'):
x -= step
elif key == ord('s'):
y += step
elif key == ord('d'):
x += step
elif key == ord('r'): # Increase Delta
if delta < 50:
delta += 1
hostSpatials.setDeltaRoi(delta)
elif key == ord('f'): # Decrease Delta
if 3 < delta:
delta -= 1
hostSpatials.setDeltaRoi(delta)