Software Stack
DepthAI
  • DepthAI Components
    • AprilTags
    • Benchmark
    • Camera
    • DetectionNetwork
    • EdgeDetector
    • Events
    • FeatureTracker
    • HostNodes
    • ImageAlign
    • ImageManip
    • IMU
    • Misc
    • Modelzoo
    • NeuralNetwork
    • RecordReplay
    • RGBD
    • Script
    • SpatialDetectionNetwork
    • SpatialLocationCalculator
    • StereoDepth
    • Sync
    • SystemLogger
    • VideoEncoder
    • Visualizer
    • Warp
    • RVC2-specific
  • Advanced Tutorials
  • API Reference
  • Tools

ON THIS PAGE

  • Script Switch All Cameras
  • Setup
  • Pipeline
  • Source code

Script Switch All Cameras

Switch between cameras by pressing a key.

Setup

This example requires the DepthAI v3 API, see installation instructions.

Pipeline

Source code

Python
C++

Python

Python
GitHub
1import depthai as dai
2import time
3import cv2
4
5# Create pipeline
6device = dai.Device()
7pipeline = dai.Pipeline(device)
8
9cameras = [
10    pipeline.create(dai.node.Camera).build(socket)
11    for socket in device.getConnectedCameras()
12]
13inputKeys = []
14script = pipeline.create(dai.node.Script)
15
16for i, camera in enumerate(cameras):
17    inputName = str(i)
18    camera.requestFullResolutionOutput().link(script.inputs[inputName])
19    script.inputs[inputName].setBlocking(False)
20    script.inputs[inputName].setMaxSize(1)
21    inputKeys.append(inputName)
22
23controlQueue = script.inputs["control"].createInputQueue()
24preview = script.outputs["out"].createOutputQueue()
25
26inputKeysSize = len(inputKeys)
27script.setScript(
28    f"""
29    inputToStream = 0
30    maxID = {inputKeysSize} - 1
31    while True:
32        controlMessage = node.inputs["control"].tryGet()
33        if controlMessage is not None:
34            if(inputToStream < maxID):
35                inputToStream += 1
36            else:
37                inputToStream = 0
38        frame = node.inputs[str(inputToStream)].get()
39        node.outputs["out"].send(frame)
40"""
41)
42
43pipeline.start()
44print("To switch between streams, press 's'")
45with pipeline:
46    while pipeline.isRunning():
47        previewMessage = preview.get()
48        assert isinstance(previewMessage, dai.ImgFrame)
49        cv2.imshow("preview", previewMessage.getCvFrame())
50        key = cv2.waitKey(1)
51        if key == ord("s"):
52            controlQueue.send(dai.Buffer())
53        if key == ord("q"):
54            break

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.