Host Camera
This example demonstrates how to use a host machine's camera (such as a laptop webcam) within a DepthAI pipeline using theHostCamera
node. This enables you to run your webcam (or any other camera connected to the host machine) as part of a DepthAI pipeline - utilizing the power of RVC hardware for processing.How It Works
HostCamera
class is a custom host node (link) that captures frames from the host machine camera using OpenCV. The captured frames are then sent to the DepthAI pipeline as ImgFrame
messages. The pipeline can then process these frames using other nodes, such as neural networks, object trackers, etc.The HostCamera
node is a threaded host node, which means it runs in a separate thread from the main pipeline. This allows the camera to capture frames independently of the rest of the pipeline, ensuring smooth operation.Setup
This example requires the DepthAI v3 API, see installation instructions.Pipeline
Source Code
Python
C++
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.isRunning():
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
Need assistance?
Head over to Discussion Forum for technical support or any other questions you might have.