演示

流水线
源代码
Python
PythonGitHub
1#!/usr/bin/env python3
2
3import cv2
4import depthai as dai
5import time
6from datetime import timedelta
7FPS = 25.0
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.StereoDepth)
36sync = pipeline.create(dai.node.Sync)
37if platform == dai.Platform.RVC4:
38 align = pipeline.create(dai.node.ImageAlign)
39
40stereo.setExtendedDisparity(True)
41sync.setSyncThreshold(timedelta(seconds=1/(2*FPS)))
42
43rgbOut = camRgb.requestOutput(size = (1280, 960), fps = FPS, enableUndistortion=True)
44leftOut = left.requestOutput(size = (640, 400), fps = FPS)
45rightOut = right.requestOutput(size = (640, 400), fps = FPS)
46
47# Linking
48rgbOut.link(sync.inputs["rgb"])
49leftOut.link(stereo.left)
50rightOut.link(stereo.right)
51if platform == dai.Platform.RVC4:
52 stereo.depth.link(align.input)
53 rgbOut.link(align.inputAlignTo)
54 align.outputAligned.link(sync.inputs["depth_aligned"])
55else:
56 stereo.depth.link(sync.inputs["depth_aligned"])
57 rgbOut.link(stereo.inputAlignTo)
58
59queue = sync.out.createOutputQueue()
60
61rgbWeight = 0.4
62depthWeight = 0.6
63
64
65def updateBlendWeights(percentRgb):
66 """
67 Update the rgb and depth weights used to blend depth/rgb image
68
69 @param[in] percent_rgb The rgb weight expressed as a percentage (0..100)
70 """
71 global depthWeight
72 global rgbWeight
73 rgbWeight = float(percentRgb) / 100.0
74 depthWeight = 1.0 - rgbWeight
75
76
77# Connect to device and start pipeline
78with pipeline:
79 pipeline.start()
80
81 # Configure windows; trackbar adjusts blending ratio of rgb/depth
82 windowName = "rgb-depth"
83
84 # Set the window to be resizable and the initial size
85 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
86 cv2.resizeWindow(windowName, 1280, 720)
87 cv2.createTrackbar(
88 "RGB Weight %",
89 windowName,
90 int(rgbWeight * 100),
91 100,
92 updateBlendWeights,
93 )
94 fpsCounter = FPSCounter()
95 while True:
96 messageGroup = queue.get()
97 fpsCounter.tick()
98 assert isinstance(messageGroup, dai.MessageGroup)
99 frameRgb = messageGroup["rgb"]
100 assert isinstance(frameRgb, dai.ImgFrame)
101 frameDepth = messageGroup["depth_aligned"]
102 assert isinstance(frameDepth, dai.ImgFrame)
103
104 # Blend when both received
105 if frameDepth is not None:
106 cvFrame = frameRgb.getCvFrame()
107 # Colorize the aligned depth
108 alignedDepthColorized = dai.utility.colorizeDepthFrame(frameDepth).getCvFrame()
109 # Resize depth to match the rgb frame
110 cv2.imshow("Depth aligned", alignedDepthColorized)
111
112 if len(cvFrame.shape) == 2:
113 cvFrameUndistorted = cv2.cvtColor(cvFrame, cv2.COLOR_GRAY2BGR)
114 blended = cv2.addWeighted(
115 cvFrame, rgbWeight, alignedDepthColorized, depthWeight, 0
116 )
117 cv2.putText(
118 blended,
119 f"FPS: {fpsCounter.getFps():.2f}",
120 (10, 30),
121 cv2.FONT_HERSHEY_SIMPLEX,
122 1,
123 (255, 255, 255),
124 2,
125 )
126 cv2.imshow(windowName, blended)
127
128 key = cv2.waitKey(1)
129 if key == ord("q"):
130 break需要帮助?
请前往 OAKChina 官网 获取技术支持或解答您的任何疑问。