Speed Calculation Preview

This example showcases the use of callback function inside the visualizer to log speed and draw tracking information.

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.

Demo

Speed Calculation Demo

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

Pipeline graph

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
import cv2

from depthai_sdk import OakCamera
from depthai_sdk.classes.packets import TrackerPacket


def callback(packet: TrackerPacket):
    for obj_id, tracklets in packet.tracklets.items():
        if len(tracklets) != 0:
            tracklet = tracklets[-1]
        if tracklet.speed is not None:
            print(f'Speed for object {obj_id}: {tracklet.speed:.02f} m/s, {tracklet.speed_kmph:.02f} km/h, {tracklet.speed_mph:.02f} mph')

    frame = packet.visualizer.draw(packet.decode())
    cv2.imshow('Speed estimation', frame)


with OakCamera() as oak:
    color = oak.create_camera('color')
    stereo = oak.create_stereo('800p')
    stereo.config_stereo(subpixel=False, lr_check=True)

    nn = oak.create_nn('face-detection-retail-0004', color, spatial=stereo, tracker=True)
    nn.config_tracker(calculate_speed=True)

    visualizer = oak.visualize(nn.out.tracker, callback=callback, fps=True)
    visualizer.tracking(show_speed=True).text(auto_scale=True)

    oak.start(blocking=True)

Got questions?

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