I have a setup with more than 10 camera's so i want to use the script node to reduce network bandwidth. I build my code based on two examples:
#!/usr/bin/env python3
import depthai as dai
import cv2
import time
# Create pipeline
pipeline = dai.Pipeline()
# Mono cameras setup
monoLeft = pipeline.create(dai.node.MonoCamera)
monoLeft.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)
monoLeft.setBoardSocket(dai.CameraBoardSocket.LEFT)
monoLeft.initialControl.setFrameSyncMode(dai.CameraControl.FrameSyncMode.INPUT)
monoLeft.initialControl.setExternalTrigger(4, 3)
xoutLeft = pipeline.create(dai.node.XLinkOut)
xoutLeft.setStreamName("left")
monoLeft.out.link(xoutLeft.input)
monoRight = pipeline.createMonoCamera()
monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)
monoRight.setBoardSocket(dai.CameraBoardSocket.RIGHT)
monoRight.initialControl.setFrameSyncMode(dai.CameraControl.FrameSyncMode.INPUT)
monoRight.initialControl.setExternalTrigger(4, 3)
xoutRight = pipeline.create(dai.node.XLinkOut)
xoutRight.setStreamName("right")
monoRight.out.link(xoutRight.input)
# RGB camera setup
rgbCam = pipeline.create(dai.node.ColorCamera)
rgbCam.setResolution(dai.ColorCameraProperties.SensorResolution.THE_4_K)
# rgbCam.setInterleaved(False)
rgbCam.setBoardSocket(dai.CameraBoardSocket.RGB)
# rgbCam.initialControl.setExternalTrigger(4, 3)
rgbCam.initialControl.setCaptureStill(True)
rgbCam.setFps(15)
# Trigger script setup
xin = pipeline.create(dai.node.XLinkIn)
xin.setStreamName('in')
script = pipeline.create(dai.node.Script)
xin.out.link(script.inputs['in'])
# script.inputs["in"].setBlocking(False)
# script.inputs["in"].setQueueSize(1)
script.setScript("""
import GPIO
import time
GPIO_PIN = 41 # Trigger
GPIO.setup(GPIO_PIN, GPIO.OUT, GPIO.PULL_DOWN)
ctrl = CameraControl()
ctrl.setCaptureStill(True)
def capture():
GPIO.write(GPIO_PIN, True)
time.sleep(0.001) # 1ms pulse is enough
GPIO.write(GPIO_PIN, False)
node.io['out'].send(ctrl)
# jpegImage = node.io['jpeg'].get()
# frame = node.io['rgb'].get()
# node.io['rgb_out'].send(frame)
while True:
wait_for_trigger = node.io['in'].get()
capture()
node.warn('Trigger successful')
""")
xout = pipeline.create(dai.node.XLinkOut)
xout.setStreamName('rgb')
script.outputs['out'].link(rgbCam.inputControl)
rgbCam.still.link(xout.input)
device_info = dai.DeviceInfo("169.254.1.101")
with dai.Device(pipeline, device_info, maxUsbSpeed=dai.UsbSpeed.SUPER_PLUS) as device:
inQ = device.getInputQueue("in")
arr = ['left', 'right', 'rgb'] #, 'rgb_out']
# arr = ['rgb']
queues = {}
frames = {}
for name in arr:
queues[name] = device.getOutputQueue(name)
def trigger():
buffer = dai.Buffer()
buffer.setData([1])
inQ.send(buffer)
time.sleep(1)
trigger() # Initial trigger
t_trigger = time.monotonic()
while True:
for name in arr:
if queues[name].has():
temp = queues[name].get()
t_stamp = temp.getTimestamp().total_seconds()
diff = t_stamp - t_trigger
print(f"{name} time [s]: {t_stamp:0.3f}, diff {diff:0.3f}")
frames[name] = temp.getCvFrame()
for name, frame in frames.items():
cv2.imshow(name, frame)
key = cv2.waitKey(0)
if key == ord('q'):
break
elif key == ord('c'):
t_trigger = time.monotonic()
trigger()
# time.sleep(0.1)
ir: example of @jakaskerl
https://discuss.luxonis.com/d/2447-taking-still-images-in-the-dark-using-ir-flood-and-monocamera/7
rgb: <https://docs.luxonis.com/software/depthai/examples/script_camera_control/ >
I slightly changed the visualisation Instead of a cv2.waitKey(1), I replaced it with cv2.waitKey(0). Because after a trigger I only should get 1 frame. However, apparently, with cv2.waitKey set to 0. I always get an old frame. Further debugging showed that actually with one trigger it can get multiple frames see screenshot below (tested with cv2.waitKey(1) to go through the loop multiple times).
