Views
No views yet
| Property | Value |
|---|---|
| Category | Object Detection + Tracking + Zone Analytics (GstAnalytics) |
| Base Model | YOLO26 (Ultralytics) |
| Source Framework | PyTorch (Ultralytics) |
| Supported Precisions | FP32, FP16, INT8 (mixed-precision) |
| Inference Engine | OpenVINO |
| Hardware | CPU, GPU, NPU |
| Detected Class | person (COCO class 0) |
yolo26n, yolo26s, yolo26m, yolo26l, yolo26x.
Smaller variants (yolo26n, yolo26s) are recommended for high-FPS edge deployment.1python3 -m venv .venv --system-site-packages
2source .venv/bin/activateNote: The--system-site-packagesflag is required so the virtual environment can access the system-installed OpenVINO and DLStreamer Python packages.
1chmod +x export_and_quantize.sh
2./export_and_quantize.sh1./export_and_quantize.sh yolo26n FP32 # full-precision
2./export_and_quantize.sh yolo26n INT8 # quantized
3./export_and_quantize.sh yolo26s # larger variant, default FP16yolo26n with any variant (yolo26s, yolo26m, yolo26l, yolo26x).
The second argument selects the precision (FP32, FP16, INT8); the default is FP16.openvino, ultralytics, opencv-python; adds nncf for INT8).worker-zone-detection.mp4) into the current directory.yolo26n_openvino_model/ -- FP32 or FP16 OpenVINO IR model directory.yolo26n_perimeter_int8.xml / yolo26n_perimeter_int8.bin -- INT8 quantized model (only when INT8 is selected).| Precision | CPU | GPU | NPU |
|---|---|---|---|
| FP32 | Yes | Yes | No |
| FP16 | Yes | Yes | Yes |
| INT8 | Yes | Yes | Yes |
Note: The INT8 calibration uses frames from the bundled sample video. For production accuracy, replace it with a representative set of frames from the target deployment site.
RESTRICTED_ZONE polygon, which traces the yellow and black safety tape in the sample video's 1920x1080 pixel space: the tape runs as a diagonal boundary from (1577, 0) to (434, 1079), and the polygon covers the keep-out side of that line.
To adapt the perimeter to a different camera, edit the RESTRICTED_ZONE points.
YOLO26 is end-to-end (NMS-free), so no manual non-maximum suppression is needed.
Breaching people are drawn with a red box and a BREACH label; the annotated result is written to output_openvino.mp4.
Change the device string to run on CPU, GPU, or NPU.1import cv2
2import numpy as np
3import openvino as ov
4
5PERSON_CLASS_ID = 0
6CONF_THRESHOLD = 0.4
7INPUT_SIZE = 640
8INPUT_VIDEO = "worker-zone-detection.mp4"
9
10# Restricted zone traced from the yellow-and-black safety tape in the sample
11# video (1920x1080). The tape runs diagonally from (1577, 0) to (434, 1079);
12# this polygon covers the keep-out side of that boundary. Edit these points to
13# retrace the tape for a different camera.
14RESTRICTED_ZONE = np.array([[0, 0], [1577, 0], [434, 1079], [0, 1080]], dtype=np.int32)
15
16core = ov.Core()
17model = core.read_model("yolo26n_openvino_model/yolo26n.xml")
18
19# Change device to "GPU" or "NPU" to run on integrated GPU or NPU.
20compiled = core.compile_model(model, "CPU")
21
22cap = cv2.VideoCapture(INPUT_VIDEO)
23fps = cap.get(cv2.CAP_PROP_FPS) or 25.0
24w0 = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
25h0 = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
26
27zone = RESTRICTED_ZONE
28
29writer = cv2.VideoWriter(
30 "output_openvino.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w0, h0)
31)
32
33sx, sy = w0 / INPUT_SIZE, h0 / INPUT_SIZE
34frame_idx = 0
35while True:
36 ok, frame = cap.read()
37 if not ok:
38 break
39 frame_idx += 1
40
41 blob = cv2.resize(frame, (INPUT_SIZE, INPUT_SIZE))
42 blob = cv2.cvtColor(blob, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
43 blob = blob.transpose(2, 0, 1)[np.newaxis, ...] # NCHW
44
45 # YOLO26 end-to-end output: [1, 300, 6] = [x1, y1, x2, y2, confidence, class_id]
46 output = compiled([blob])[compiled.output(0)][0]
47 mask = (output[:, 4] >= CONF_THRESHOLD) & (output[:, 5].astype(int) == PERSON_CLASS_ID)
48
49 # Draw the translucent restricted zone first, then the person boxes on top.
50 overlay = frame.copy()
51 cv2.fillPoly(overlay, [zone], (0, 0, 255))
52 cv2.addWeighted(overlay, 0.25, frame, 0.75, 0, frame)
53 cv2.polylines(frame, [zone], True, (0, 255, 255), 2)
54
55 breaches = 0
56 for det in output[mask]:
57 x1, y1 = int(det[0] * sx), int(det[1] * sy)
58 x2, y2 = int(det[2] * sx), int(det[3] * sy)
59 center = (int((x1 + x2) / 2), int((y1 + y2) / 2))
60 inside = cv2.pointPolygonTest(zone, center, False) >= 0
61 color = (0, 0, 255) if inside else (0, 255, 0)
62 cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
63 if inside:
64 breaches += 1
65 cv2.putText(
66 frame, "BREACH", (x1, max(y1 - 6, 12)),
67 cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2,
68 )
69
70 cv2.putText(
71 frame, f"Breaches: {breaches}", (10, 30),
72 cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2,
73 )
74 if breaches:
75 print(f"frame {frame_idx}: perimeter breach - {breaches} person(s) inside zone", flush=True)
76 writer.write(frame)
77
78cap.release()
79writer.release()
80print("Saved: output_openvino.mp4")"CPU" -- default, works on all Intel platforms."GPU" -- Intel integrated or discrete GPU."NPU" -- Intel NPU (different throughput profile; validate with benchmark_app -d NPU).
output_openvino.mp4 shows the restricted perimeter shaded in red, a green box around each person outside the zone, and a red BREACH box around anyone inside it.gvadetect, tracks each person with gvatrack, and uses DLStreamer's gvaanalytics element to test membership in the restricted zone.
The zone is the same RESTRICTED_ZONE polygon used by the OpenVINO sample, passed to gvaanalytics as a JSON zone, so no polygon math is required in the code.
gvaanalytics attaches GstAnalyticsZoneMtd to every tracked person whose center falls inside the polygon.
The pipeline ends in an appsink; for each frame a callback reads the analytics metadata, shades the restricted zone, draws a green box around people outside it and a red BREACH box around anyone inside, and writes the annotated result to output_dlstreamer.mp4.
A perimeter-breach event is printed the first time each tracked person enters the zone.Notes on running this sample:
Use the FP16 IR (yolo26n_openvino_model/yolo26n.xml). On DLStreamer 2026.1,gvadetectcannot auto-derive a YOLO post-processor from the INT8 model produced by the bundled script. To use the INT8 model, supply a matchingmodel-procJSON. Class names are read automatically from the model's embeddedmetadata.yamlby DLStreamer 2026.0+ -- no externallabels-fileis required. ExportPYTHONPATHso the DLStreamer Python module is importable:bash1source /opt/intel/openvino_2026/setupvars.sh 2source /opt/intel/dlstreamer/scripts/setup_dls_env.sh 3export PYTHONPATH=/opt/intel/dlstreamer/python:\ 4/opt/intel/dlstreamer/gstreamer/lib/python3/dist-packages:${PYTHONPATH:-}
1import json
2import sys
3import gi
4
5gi.require_version("Gst", "1.0")
6gi.require_version("GstApp", "1.0")
7gi.require_version("GstAnalytics", "1.0")
8gi.require_version("DLStreamerMeta", "1.0")
9from gi.repository import Gst, GLib, GstApp, GstAnalytics, DLStreamerMeta
10
11Gst.init([])
12
13# Register DLStreamerMeta types so GstAnalytics iteration can handle them.
14_ov = sys.modules["gi.overrides.GstAnalytics"]
15_ov.__mtd_types__[DLStreamerMeta.ZoneMtd.get_mtd_type()] = DLStreamerMeta.relation_meta_get_zone_mtd
16
17# Import OpenCV after Gst.init to avoid a GStreamer re-initialization conflict.
18import cv2
19import numpy as np
20
21MODEL = "yolo26n_openvino_model/yolo26n.xml"
22VIDEO = "worker-zone-detection.mp4"
23DEVICE = "GPU" # change to "CPU" or "NPU" as needed
24
25# Restricted zone traced from the yellow-and-black safety tape in the sample
26# video (1920x1080), matching the OpenVINO sample. Edit these points to retrace
27# the tape for a different camera.
28RESTRICTED_ZONE = np.array([[0, 0], [1577, 0], [434, 1079], [0, 1080]], dtype=np.int32)
29ZONE_JSON = json.dumps([{
30 "id": "restricted_zone",
31 "type": "polygon",
32 "points": [{"x": int(x), "y": int(y)} for x, y in RESTRICTED_ZONE],
33}])
34
35pipeline = Gst.parse_launch(
36 f"filesrc location={VIDEO} ! decodebin3 ! videoconvert ! "
37 f"gvadetect model={MODEL} device={DEVICE} threshold=0.4 ! queue ! "
38 f"gvatrack tracking-type=short-term-imageless ! queue ! "
39 f"gvaanalytics name=analytics ! queue ! "
40 f"videoconvert ! video/x-raw,format=BGR ! "
41 f"appsink name=sink emit-signals=true max-buffers=4 drop=false sync=false"
42)
43pipeline.get_by_name("analytics").set_property("zones", ZONE_JSON)
44
45writer = {"w": None}
46# Track IDs that have already triggered a breach event, so each intruder is
47# reported only once.
48flagged = set()
49
50
51def on_sample(appsink):
52 sample = appsink.emit("pull-sample")
53 if sample is None:
54 return Gst.FlowReturn.OK
55 buf = sample.get_buffer()
56 caps = sample.get_caps().get_structure(0)
57 w = caps.get_value("width")
58 h = caps.get_value("height")
59 ok_fr, fr_n, fr_d = caps.get_fraction("framerate")
60 fps = (fr_n / fr_d) if (ok_fr and fr_d) else 30.0
61
62 ok, minfo = buf.map(Gst.MapFlags.READ)
63 if not ok:
64 return Gst.FlowReturn.OK
65 frame = np.ndarray((h, w, 3), buffer=minfo.data, dtype=np.uint8).copy()
66 buf.unmap(minfo)
67
68 now = buf.pts / Gst.SECOND if buf.pts != Gst.CLOCK_TIME_NONE else 0.0
69
70 # Shade the restricted zone and outline the tape boundary.
71 overlay = frame.copy()
72 cv2.fillPoly(overlay, [RESTRICTED_ZONE], (0, 0, 255))
73 cv2.addWeighted(overlay, 0.25, frame, 0.75, 0, frame)
74 cv2.polylines(frame, [RESTRICTED_ZONE], True, (0, 255, 255), 2)
75
76 breaches = 0
77 rmeta = GstAnalytics.buffer_get_analytics_relation_meta(buf)
78 if rmeta:
79 for od in rmeta.iter_on_type(GstAnalytics.ODMtd):
80 if GLib.quark_to_string(od.get_obj_type()) != "person":
81 continue
82 _, x, y, bw, bh, _ = od.get_location()
83
84 # Find the tracking ID via the direct relation.
85 track_id = None
86 for trk in od.iter_direct_related(GstAnalytics.RelTypes.RELATE_TO, GstAnalytics.TrackingMtd):
87 success, tid, *_ = trk.get_info()
88 if success:
89 track_id = tid
90 break
91
92 # gvaanalytics attaches a ZoneMtd relation when the person is in the zone.
93 in_zone = any(
94 True for _ in od.iter_direct_related(GstAnalytics.RelTypes.RELATE_TO, DLStreamerMeta.ZoneMtd)
95 )
96 color = (0, 0, 255) if in_zone else (0, 255, 0)
97 cv2.rectangle(frame, (int(x), int(y)), (int(x + bw), int(y + bh)), color, 3)
98 label = f"id {track_id}" if track_id is not None else "person"
99 if in_zone:
100 breaches += 1
101 cv2.putText(frame, f"BREACH {label}", (int(x), max(int(y) - 8, 14)),
102 cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
103 if track_id is not None and track_id not in flagged:
104 flagged.add(track_id)
105 print(f"PERIMETER BREACH id={track_id} t={now:.1f}s "
106 f"entered restricted zone at ({int(x + bw / 2)},{int(y + bh)})", flush=True)
107 else:
108 cv2.putText(frame, label, (int(x), max(int(y) - 8, 14)),
109 cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
110
111 cv2.putText(frame, f"Breaches: {breaches}", (10, 40),
112 cv2.FONT_HERSHEY_SIMPLEX, 1.1, (0, 0, 255), 3)
113 if writer["w"] is None:
114 writer["w"] = cv2.VideoWriter(
115 "output_dlstreamer.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h)
116 )
117 writer["w"].write(frame)
118 return Gst.FlowReturn.OK
119
120
121pipeline.get_by_name("sink").connect("new-sample", on_sample)
122pipeline.set_state(Gst.State.PLAYING)
123pipeline.get_bus().timed_pop_filtered(Gst.CLOCK_TIME_NONE, Gst.MessageType.EOS | Gst.MessageType.ERROR)
124pipeline.set_state(Gst.State.NULL)
125if writer["w"] is not None:
126 writer["w"].release()1PERIMETER BREACH id=2 t=3.8s entered restricted zone at (799,1067)
2PERIMETER BREACH id=8 t=13.7s entered restricted zone at (789,1069)
3...output_dlstreamer.mp4.
It shows the restricted zone shaded in red with the tape boundary outlined, a green box around each person outside the zone, and a red BREACH box around anyone inside it -- matching the OpenVINO output.
DEVICE = "GPU" -- default in the sample code.DEVICE = "CPU" -- change DEVICE = "GPU" to DEVICE = "CPU".DEVICE = "NPU" -- change DEVICE = "GPU" to DEVICE = "NPU" for the Intel NPU.