Stream vehicles

Stream every vehicle that leaves a traffic lane zone with the Python client. Install the blickfeld_qb2 library, save the script below as stream_vehicles.py and run it with the hostname of the sensor and the secret of an application key created in the WebGUI:

pip install -U blickfeld_qb2
python3 stream_vehicles.py qb2-<serial number> <application key>
stream_vehicles.py
import sys

import blickfeld_qb2
from blickfeld_qb2.betterproto import which_one_of

TlsVehicle = blickfeld_qb2.percept_pipeline.config.ObjectClassTlsVehicle


def vehicle_class(vehicle):
    predicted = vehicle.model_classification.predicted_class.tls_vehicle
    name = TlsVehicle(predicted).name.removeprefix("TLS_VEHICLE_")
    return name.replace("_", " ").capitalize()


def describe(vehicle):
    speed = abs(vehicle.velocity) * 3.6
    size = vehicle.dimensions
    return f"{vehicle_class(vehicle)}: {speed:.0f} km/h, {size.x:.1f} x {size.y:.1f} x {size.z:.1f} m"


def stream_vehicles(host, key=None):
    token = blickfeld_qb2.TokenFactory(application_key_secret=key) if key else None

    with blickfeld_qb2.Channel(host, token=token) as channel:
        events = blickfeld_qb2.percept_processing.services.Event(channel)

        for response in events.stream():
            kind, event = which_one_of(response.event, "type")
            if kind == "traffic_vehicle_detected":
                print(describe(event.vehicle))


if __name__ == "__main__":
    stream_vehicles(*sys.argv[1:3])

Output (example):

Motorbike: 7 km/h, 0.4 x 1.7 x 1.7 m
Car: 17 km/h, 1.9 x 3.6 x 1.5 m