Custom Trigger¶
This example shows how to set custom trigger condition in DepthAI SDK. The trigger condition returns a boolean value if the condition is met. In this case the trigger will start a recording of disparity stream when all depth values are below 1 meter.
Note
Visualization in current example is done with blocking behavor. This means that the program will halt at oak.start()
until the window is closed.
This is done to keep the example simple. For more advanced usage, see Blocking behavior section.
Setup¶
Please run the install script to download all required dependencies. Please note that this script must be ran from git context, so you have to download the depthai repository first and then run the script
git clone https://github.com/luxonis/depthai.git
cd depthai/
python3 install_requirements.py
For additional information, please follow our installation guide.
Pipeline¶
Source Code¶
Also available on GitHub.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import numpy as np from depthai_sdk import OakCamera from depthai_sdk.trigger_action import Trigger from depthai_sdk.trigger_action.actions import RecordAction def my_condition(packet) -> bool: """ Returns true if all depth values are within 1m range. """ frame = packet.frame required_range = 1000 # mm --> 5m frame = frame[frame > 0] # remove invalid depth values frame = frame[(frame > np.percentile(frame, 1)) & (frame < np.percentile(frame, 99))] min_depth = np.min(frame) max_depth = np.max(frame) if min_depth < required_range < max_depth: return True return False with OakCamera() as oak: color = oak.create_camera('color', fps=30) stereo = oak.create_stereo('800p') stereo.config_stereo(align=color) trigger = Trigger(input=stereo.out.depth, condition=my_condition, cooldown=30) action = RecordAction( inputs=[stereo.out.disparity], dir_path='./recordings/', duration_before_trigger=5, duration_after_trigger=5 ) oak.trigger_action(trigger=trigger, action=action) oak.start(blocking=True) |