# SPDX-License-Identifier: MIT # Copyright (C) Intel Corporation # # Perimeter Breach Detection: DLStreamer pipeline sample. # See README.md for prerequisites and inputs. Run: python3 dlstreamer_sample.py import json import sys import gi gi.require_version("Gst", "1.0") gi.require_version("GstApp", "1.0") gi.require_version("GstAnalytics", "1.0") gi.require_version("DLStreamerMeta", "1.0") from gi.repository import Gst, GLib, GstApp, GstAnalytics, DLStreamerMeta Gst.init([]) # Register DLStreamerMeta types so GstAnalytics iteration can handle them. _ov = sys.modules["gi.overrides.GstAnalytics"] _ov.__mtd_types__[DLStreamerMeta.ZoneMtd.get_mtd_type()] = DLStreamerMeta.relation_meta_get_zone_mtd # Import OpenCV after Gst.init to avoid a GStreamer re-initialization conflict. import cv2 import numpy as np # Select the model: "yolo26n" (default) or "detr-resnet-50". MODEL = "yolo26n" MODEL_PATH = f"{MODEL}_openvino_model/{MODEL}.xml" VIDEO = "worker-zone-detection.mp4" DEVICE = "GPU" # change to "CPU" or "NPU" as needed # Restricted zone traced from the yellow-and-black safety tape in the sample # video (1920x1080), matching the OpenVINO sample. Edit these points to retrace # the tape for a different camera. RESTRICTED_ZONE = np.array([[0, 0], [1577, 0], [434, 1079], [0, 1080]], dtype=np.int32) ZONE_JSON = json.dumps([{ "id": "restricted_zone", "type": "polygon", "points": [{"x": int(x), "y": int(y)} for x, y in RESTRICTED_ZONE], }]) pipeline = Gst.parse_launch( f"filesrc location={VIDEO} ! decodebin3 ! videoconvert ! " f"gvadetect model={MODEL_PATH} device={DEVICE} threshold=0.4 ! queue ! " f"gvatrack tracking-type=short-term-imageless ! queue ! " f"gvaanalytics name=analytics ! queue ! " f"videoconvert ! video/x-raw,format=BGR ! " f"appsink name=sink emit-signals=true max-buffers=4 drop=false sync=false" ) pipeline.get_by_name("analytics").set_property("zones", ZONE_JSON) writer = {"w": None} # Track IDs that have already triggered a breach event, so each intruder is # reported only once. flagged = set() def on_sample(appsink): sample = appsink.emit("pull-sample") if sample is None: return Gst.FlowReturn.OK buf = sample.get_buffer() caps = sample.get_caps().get_structure(0) w = caps.get_value("width") h = caps.get_value("height") ok_fr, fr_n, fr_d = caps.get_fraction("framerate") fps = (fr_n / fr_d) if (ok_fr and fr_d) else 30.0 ok, minfo = buf.map(Gst.MapFlags.READ) if not ok: return Gst.FlowReturn.OK frame = np.ndarray((h, w, 3), buffer=minfo.data, dtype=np.uint8).copy() buf.unmap(minfo) now = buf.pts / Gst.SECOND if buf.pts != Gst.CLOCK_TIME_NONE else 0.0 # Shade the restricted zone and outline the tape boundary. overlay = frame.copy() cv2.fillPoly(overlay, [RESTRICTED_ZONE], (0, 0, 255)) cv2.addWeighted(overlay, 0.25, frame, 0.75, 0, frame) cv2.polylines(frame, [RESTRICTED_ZONE], True, (0, 255, 255), 2) breaches = 0 rmeta = GstAnalytics.buffer_get_analytics_relation_meta(buf) if rmeta: for od in rmeta.iter_on_type(GstAnalytics.ODMtd): if GLib.quark_to_string(od.get_obj_type()) != "person": continue _, x, y, bw, bh, _ = od.get_location() # Find the tracking ID via the direct relation. track_id = None for trk in od.iter_direct_related(GstAnalytics.RelTypes.RELATE_TO, GstAnalytics.TrackingMtd): success, tid, *_ = trk.get_info() if success: track_id = tid break # gvaanalytics attaches a ZoneMtd relation when the person is in the zone. in_zone = any( True for _ in od.iter_direct_related(GstAnalytics.RelTypes.RELATE_TO, DLStreamerMeta.ZoneMtd) ) color = (0, 0, 255) if in_zone else (0, 255, 0) cv2.rectangle(frame, (int(x), int(y)), (int(x + bw), int(y + bh)), color, 3) label = f"id {track_id}" if track_id is not None else "person" if in_zone: breaches += 1 cv2.putText(frame, f"BREACH {label}", (int(x), max(int(y) - 8, 14)), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) if track_id is not None and track_id not in flagged: flagged.add(track_id) print(f"PERIMETER BREACH id={track_id} t={now:.1f}s " f"entered restricted zone at ({int(x + bw / 2)},{int(y + bh)})", flush=True) else: cv2.putText(frame, label, (int(x), max(int(y) - 8, 14)), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) cv2.putText(frame, f"Breaches: {breaches}", (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.1, (0, 0, 255), 3) if writer["w"] is None: writer["w"] = cv2.VideoWriter( "output_dlstreamer.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h) ) writer["w"].write(frame) return Gst.FlowReturn.OK pipeline.get_by_name("sink").connect("new-sample", on_sample) pipeline.set_state(Gst.State.PLAYING) pipeline.get_bus().timed_pop_filtered(Gst.CLOCK_TIME_NONE, Gst.MessageType.EOS | Gst.MessageType.ERROR) pipeline.set_state(Gst.State.NULL) if writer["w"] is not None: writer["w"].release()