File size: 5,389 Bytes
64f1534
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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()