神经深度对齐
Supported on:RVC4
流水线
源代码
Python
PythonGitHub
1#!/usr/bin/env python3
2
3import cv2
4import depthai as dai
5import time
6from datetime import timedelta
7FPS = 25
8
9RGB_SOCKET = dai.CameraBoardSocket.CAM_A
10LEFT_SOCKET = dai.CameraBoardSocket.CAM_B
11RIGHT_SOCKET = dai.CameraBoardSocket.CAM_C
12
13class FPSCounter:
14 def __init__(self):
15 self.frameTimes = []
16
17 def tick(self):
18 now = time.time()
19 self.frameTimes.append(now)
20 self.frameTimes = self.frameTimes[-10:]
21
22 def getFps(self):
23 if len(self.frameTimes) <= 1:
24 return 0
25 return (len(self.frameTimes) - 1) / (self.frameTimes[-1] - self.frameTimes[0])
26
27pipeline = dai.Pipeline()
28
29platform = pipeline.getDefaultDevice().getPlatform()
30
31# Define sources and outputs
32camRgb = pipeline.create(dai.node.Camera).build(RGB_SOCKET)
33left = pipeline.create(dai.node.Camera).build(LEFT_SOCKET)
34right = pipeline.create(dai.node.Camera).build(RIGHT_SOCKET)
35stereo = pipeline.create(dai.node.NeuralDepth)
36sync = pipeline.create(dai.node.Sync)
37align = pipeline.create(dai.node.ImageAlign)
38
39sync.setSyncThreshold(timedelta(seconds=1/(2*FPS)))
40
41rgbOut = camRgb.requestOutput(size = (1280, 960), fps = FPS, enableUndistortion=True)
42leftOut = left.requestFullResolutionOutput(fps = FPS)
43rightOut = right.requestFullResolutionOutput(fps = FPS)
44stereo.build(leftOut, rightOut, dai.DeviceModelZoo.NEURAL_DEPTH_LARGE)
45# Linking
46stereo.depth.link(align.input)
47rgbOut.link(align.inputAlignTo)
48rgbOut.link(sync.inputs["rgb"])
49align.outputAligned.link(sync.inputs["depth_aligned"])
50
51queue = sync.out.createOutputQueue()
52
53rgbWeight = 0.4
54depthWeight = 0.6
55
56
57def updateBlendWeights(percentRgb):
58 """
59 Update the rgb and depth weights used to blend depth/rgb image
60
61 @param[in] percent_rgb The rgb weight expressed as a percentage (0..100)
62 """
63 global depthWeight
64 global rgbWeight
65 rgbWeight = float(percentRgb) / 100.0
66 depthWeight = 1.0 - rgbWeight
67
68
69# Connect to device and start pipeline
70with pipeline:
71 pipeline.start()
72
73 # Configure windows; trackbar adjusts blending ratio of rgb/depth
74 windowName = "rgb-depth"
75
76 # Set the window to be resizable and the initial size
77 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
78 cv2.resizeWindow(windowName, 1280, 720)
79 cv2.createTrackbar(
80 "RGB Weight %",
81 windowName,
82 int(rgbWeight * 100),
83 100,
84 updateBlendWeights,
85 )
86 fpsCounter = FPSCounter()
87 while True:
88 messageGroup = queue.get()
89 fpsCounter.tick()
90 assert isinstance(messageGroup, dai.MessageGroup)
91 frameRgb = messageGroup["rgb"]
92 assert isinstance(frameRgb, dai.ImgFrame)
93 frameDepth = messageGroup["depth_aligned"]
94 assert isinstance(frameDepth, dai.ImgFrame)
95
96 # Blend when both received
97 if frameDepth is not None:
98 cvFrame = frameRgb.getCvFrame()
99 # Colorize the aligned depth
100 alignedDepthColorized = dai.utility.colorizeDepthFrame(frameDepth).getCvFrame()
101 # Resize depth to match the rgb frame
102 cv2.imshow("Depth aligned", alignedDepthColorized)
103
104 if len(cvFrame.shape) == 2:
105 cvFrameUndistorted = cv2.cvtColor(cvFrame, cv2.COLOR_GRAY2BGR)
106 # print("RGB Size:", cvFrame.shape)
107 # print("Depth Size:", alignedDepthColorized.shape)
108 blended = cv2.addWeighted(
109 cvFrame, rgbWeight, alignedDepthColorized, depthWeight, 0
110 )
111 cv2.putText(
112 blended,
113 f"FPS: {fpsCounter.getFps():.2f}",
114 (10, 30),
115 cv2.FONT_HERSHEY_SIMPLEX,
116 1,
117 (255, 255, 255),
118 2,
119 )
120 cv2.imshow(windowName, blended)
121
122 key = cv2.waitKey(1)
123 if key == ord("q"):
124 break需要帮助?
请前往 OAKChina 官网 获取技术支持或解答您的任何疑问。