|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import cv2 |
| 3 | +import depthai as dai |
| 4 | +import queue |
| 5 | + |
| 6 | +# Create pipeline |
| 7 | +pipeline = dai.Pipeline() |
| 8 | + |
| 9 | +# Add all three cameras |
| 10 | +camRgb = pipeline.createColorCamera() |
| 11 | +left = pipeline.createMonoCamera() |
| 12 | +right = pipeline.createMonoCamera() |
| 13 | + |
| 14 | +# Create XLink output |
| 15 | +xout = pipeline.createXLinkOut() |
| 16 | +xout.setStreamName("frames") |
| 17 | + |
| 18 | +# Properties |
| 19 | +camRgb.setPreviewSize(300, 300) |
| 20 | +left.setBoardSocket(dai.CameraBoardSocket.LEFT) |
| 21 | +left.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P) |
| 22 | +right.setBoardSocket(dai.CameraBoardSocket.RIGHT) |
| 23 | +right.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P) |
| 24 | + |
| 25 | +# Stream all the camera streams through the same XLink node |
| 26 | +camRgb.preview.link(xout.input) |
| 27 | +left.out.link(xout.input) |
| 28 | +right.out.link(xout.input) |
| 29 | + |
| 30 | +q = queue.Queue() |
| 31 | + |
| 32 | +def newFrame(inFrame): |
| 33 | + global q |
| 34 | + # Get "stream name" from the instance number |
| 35 | + num = inFrame.getInstanceNum() |
| 36 | + name = "color" if num == 0 else "left" if num == 1 else "right" |
| 37 | + frame = inFrame.getCvFrame() |
| 38 | + # This is a different thread and you could use it to |
| 39 | + # run image processing algorithms here |
| 40 | + q.put({"name": name, "frame": frame}) |
| 41 | + |
| 42 | +# Connect to device and start pipeline |
| 43 | +with dai.Device(pipeline) as device: |
| 44 | + |
| 45 | + # Add callback to the output queue "frames" for all newly arrived frames (color, left, right) |
| 46 | + device.getOutputQueue(name="frames", maxSize=4, blocking=False).addCallback(newFrame) |
| 47 | + |
| 48 | + while True: |
| 49 | + # You could also get the data as non-blocking (block=False) |
| 50 | + data = q.get(block=True) |
| 51 | + cv2.imshow(data["name"], data["frame"]) |
| 52 | + |
| 53 | + if cv2.waitKey(1) == ord('q'): |
| 54 | + break |
0 commit comments