Stream events and alarms

The Event service in blickfeld_qb2.percept_processing.services queries and streams the events of the zones. An intrusion event of a security zone carries the zone identifier, the timestamps of the start and end of the intrusion and the properties of the intruding objects.

The examples use the Python library and sign in with an application key. Replace qb2-xxxxxxxxx with the hostname or IP address of your sensor and application-key-for-qb2-xxxxxxxxx with the secret of your key.

Stream the alarms of all security zones

Stream the alarms of all security zones with the Python client. Install the blickfeld_qb2 library, save the script below as stream_alarms.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_alarms.py qb2-<serial number> <application key>
stream_alarms.py
import sys

import blickfeld_qb2
from blickfeld_qb2.betterproto import which_one_of


def describe(kind, event):
    if kind == "security_zone_intrusion_started":
        return f"Alarm in {event.state.generator_name}"
    if kind == "security_zone_intrusion_ended":
        return f"Alarm ended after {event.duration:.1f} s"
    return None


def stream_alarms(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():
            line = describe(*which_one_of(response.event, "type"))
            if line:
                print(line)


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

Output when a person walks through a zone (example):

Alarm in Pre-Alarm
Alarm ended after 13.0 s

The examples below show the Event service step by step.

Stream events

This example prints each event.

import blickfeld_qb2


def stream_security_events() -> None:
    # Create token factory using an application key
    token_factory = blickfeld_qb2.TokenFactory(application_key_secret="application-key-for-qb2-xxxxxxxxx")
    # Open a secure connection to the sensor
    with blickfeld_qb2.Channel("qb2-xxxxxxxxx", token=token_factory) as channel:
        # Use Event service
        service = blickfeld_qb2.percept_processing.services.Event(channel)
        # Iterate over events stream and print them
        for response in service.stream():
            print("Event:", response.event)


stream_security_events()

Output when a person enters a zone, the first two events, one field per line and long lists cut with …​:

Event: Event(
    timestamp=1794557816515962216,
    security_intrusion_started=EventSecurityIntrusionStarted()
)
Event: Event(
    timestamp=1794557816515962216,
    security_zone_intrusion_started=EventSecurityZoneIntrusionStarted(
        zone_uuid='6dde4732-d2d2-4981-bee6-6864d7669a75',
        state=State(
            generator_name='Pre-Alarm',
            object_based_security=StateObjectBasedSecurity(
                alarm=True,
                objects={
                    '59': DetectedObject(
                        pose=Pose(
                            position=Vector3(x=-1.2360198497772217, y=7.185153961181641, z=1.0974624156951904)
                        ),
                        shape=Shape(
                            pose=Pose(
                                position=Vector3(x=-1.2197115421295166, y=7.104532241821289, z=1.0974624156951904)
                            ),
                            box=ShapeBox(
                                dimensions=Vector3(x=0.32164937257766724, y=0.3377991318702698, z=1.2932387590408325)
                            )
                        ),
                        properties=DetectedObjectProperties(size=2, num_points=220, average_intensity=3884, bounding_box_2_d_surface=0.43685492873191833, track_length=5.475871562957764, median_reflectivity=4146),
                        point_cloud=Frame(
                            binary=FrameBinary(
                                length=220,
                                cartesian=array(
                                    [
                                        [-1.1444434 ,  7.0532727 ,  1.2344986 ],
                                        [-1.1692843 ,  7.128196  ,  1.5870416 ],
                                        [-1.3347117 ,  7.079878  ,  1.000879  ],
                                        ...
                                    ],
                                    dtype=float32
                                ),
                                photon_count=array(
                                    [5381, 5142, 4758, ...],
                                    dtype=uint16
                                ),
                                direction_id=array(
                                    [393, 50, 2640, ...],
                                    dtype=uint32
                                )
                            )
                        ),
                        timestamp=1794557808515962216,
                        path=[
                            Pose(
                                position=Vector3(x=-4.121706008911133, y=1.7800681591033936, z=0.4839831292629242)
                            ),
                            Pose(
                                position=Vector3(x=-3.488027572631836, y=3.0714902877807617, z=1.079064130783081)
                            ),
                            Pose(
                                position=Vector3(x=-2.86340594291687, y=4.3938727378845215, z=1.0780154466629028)
                            ),
                            ...
                        ],
                        visible_to_sensors=['ABC123XYZ'],
                        intruding=Flag(state=True, since_timestamp=1794557808515962216),
                        intruder=Flag(state=True, since_timestamp=1794557808515962216),
                        lifetime=8000000000,
                        velocity=Vector3(x=0.3014988899230957, y=0.27406570315361023),
                        in_zone_uuids=['6dde4732-d2d2-4981-bee6-6864d7669a75']
                    )
                },
                intrusion=Flag(state=True, since_timestamp=1794557816515962216),
                intrusion_confidence=1.0,
                tampering=Flag(since_timestamp=1794557807015962216, since_duration=9500000000)
            ),
            coverage=1.0,
            coverage_observed_area=24.43000030517578
        ),
        disqualification=Disqualification()
    )
)

Handle zone intrusion details

This example prints the UUID of the zone and the position of each object when an intrusion starts:

import blickfeld_qb2


def stream_zone_intrusion_details() -> None:
    # Create token factory using an application key
    token_factory = blickfeld_qb2.TokenFactory(
      application_key_secret="application-key-for-qb2-xxxxxxxxx"
    )
    # Open a secure connection to the sensor
    with blickfeld_qb2.Channel("qb2-xxxxxxxxx", token=token_factory) as channel:
        # Use Event service
        service = blickfeld_qb2.percept_processing.services.Event(channel)
        # Iterate over events stream
        for response in service.stream():
            event = response.event
            # Condition for checking start of the intrusion of any zone
            if event.security_zone_intrusion_started:
                zone_event = event.security_zone_intrusion_started
                print(f"Zone: {zone_event.zone_uuid}")
                print("Objects involved:")
                # Iterate over objects in intrusion zone and print their position
                for obj_id, obj in zone_event.state.object_based_security.objects.items():
                    pos = obj.pose.position
                    print(f" - ID {obj_id}: (x={pos.x:.2f}, y={pos.y:.2f}, z={pos.z:.2f})")


stream_zone_intrusion_details()

Output:

Zone: 6dde4732-d2d2-4981-bee6-6864d7669a75
Objects involved:
 - ID 60: (x=-1.24, y=7.19, z=1.10)

Save events to a file

This example writes each event as one line of JSON into the file events.log, as soon as it arrives, until you press Ctrl+C.

import blickfeld_qb2


def log_events_to_file(filepath="events.log") -> None:
    # Create token factory using an application key
    token_factory = blickfeld_qb2.TokenFactory(application_key_secret="application-key-for-qb2-xxxxxxxxx")
    # Open a secure connection to the sensor
    with blickfeld_qb2.Channel("qb2-xxxxxxxxx", token=token_factory) as channel:
        service = blickfeld_qb2.percept_processing.services.Event(channel)
        # Line buffered: each event is in the file as soon as it is written
        with open(filepath, "w", buffering=1) as f:
            for response in service.stream():
                # Write each event as one line of JSON
                f.write(f"{response.event.to_json()}\n")


log_events_to_file()