HostCamera 节点在 DepthAI 管道中使用主机(例如笔记本电脑的摄像头)的摄像头。这使您可以将网络摄像头(或连接 到主机上的任何其他摄像头)作为 DepthAI 管道的一部分运行,并利用 RVC 硬件进行处理。工作原理
HostCamera 类是一个自定义主机节点(链接),它使用 OpenCV 从主机摄像头捕获帧。然后,捕获的帧将作为 ImgFrame 消息发送到 DepthAI 管道。然后,管道可以使用其他节点(如神经网络、对象跟踪器等)来处理这些帧。HostCamera 节点是一个线程化主机节点,这意味着它在与主管道不同的线程中运行。这允许摄像头独立于管道的其余部分捕获帧,从而确保平稳运行。此示例需要 DepthAI v3 API,请参阅 安装说明。源代码
Python
PythonGitHub
1import depthai as dai
2import cv2
3import time
4
5
6class HostCamera(dai.node.ThreadedHostNode):
7 def __init__(self):
8 super().__init__()
9 self.output = self.createOutput()
10 def run(self):
11 # Create a VideoCapture object
12 cap = cv2.VideoCapture(0)
13 if not cap.isOpened():
14 p.stop()
15 raise RuntimeError("Error: Couldn't open host camera")
16 while self.mainLoop():
17 # Read the frame from the camera
18 ret, frame = cap.read()
19 if not ret:
20 break
21 # Create an ImgFrame message
22 imgFrame = dai.ImgFrame()
23 imgFrame.setData(frame)
24 imgFrame.setWidth(frame.shape[1])
25 imgFrame.setHeight(frame.shape[0])
26 imgFrame.setType(dai.ImgFrame.Type.BGR888i)
27 # Send the message
28 self.output.send(imgFrame)
29 # Wait for the next frame
30 time.sleep(0.1)
31
32with dai.Pipeline(createImplicitDevice=False) as p:
33 hostCamera = p.create(HostCamera)
34 camQueue = hostCamera.output.createOutputQueue()
35
36 p.start()
37 while p.isRunning():
38 image : dai.ImgFrame = camQueue.get()
39 cv2.imshow("HostCamera", image.getCvFrame())
40 key = cv2.waitKey(1)
41 if key == ord('q'):
42 p.stop()
43 break需要帮助?
请前往 Discussion Forum 获取技术支持或提出您可能有的任何其他问题。