File size: 6,071 Bytes
953285e | 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | # SPDX-License-Identifier: MIT
# Copyright (C) Intel Corporation
#
# Running Detection: DLStreamer pipeline sample.
# See README.md for prerequisites and inputs. Run: python3 dlstreamer_sample.py
import subprocess
from collections import defaultdict
import numpy as np
import gi
gi.require_version("Gst", "1.0")
gi.require_version("GstAnalytics", "1.0")
gi.require_version("GstVideo", "1.0")
from gi.repository import Gst, GLib, GstAnalytics, GstVideo
Gst.init([])
# Import cv2 after Gst.init to avoid GStreamer re-initialization conflicts.
import cv2
INPUT_VIDEO = "running.mp4"
RUNNING_SPEED = 250.0 # pixels/second
MIN_RUN_FRAMES = 3 # sustained frames above the threshold before flagging
ALERT_HOLD_SECONDS = 2.0 # latch the alert banner to keep it from flickering
# Select the model: "yolo26n" (default) or "detr-resnet-50".
MODEL = "yolo26n"
# For CPU: change device=GPU to device=CPU.
# For NPU: change device=GPU to device=NPU (batch-size=1, nireq=4 recommended).
# gvawatermark draws only person ROIs (displ-cfg=show-roi=person) so boxes for
# other COCO classes are not rendered.
pipeline_str = (
f"filesrc location={INPUT_VIDEO} ! decodebin3 ! "
"videoconvert ! "
f"gvadetect model={MODEL}_openvino_model/{MODEL}.xml "
"device=GPU "
"threshold=0.4 ! queue ! "
"gvatrack tracking-type=short-term-imageless ! queue ! "
"gvawatermark displ-cfg=show-roi=person ! "
"videoconvert ! video/x-raw,format=BGR ! "
"appsink name=sink emit-signals=false sync=false"
)
pipeline = Gst.parse_launch(pipeline_str)
appsink = pipeline.get_by_name("sink")
pipeline.set_state(Gst.State.PLAYING)
proc = None
prev_center: dict[int, tuple[int, int]] = {}
run_frames: dict[int, int] = defaultdict(int)
flagged: set[int] = set()
prev_pts = None
alert_hold = 0
alert_hold_frames = 40 # updated from the real framerate on the first frame
frame_idx = 0
while True:
sample = appsink.emit("pull-sample")
if sample is None:
break
buf = sample.get_buffer()
caps = sample.get_caps()
struct = caps.get_structure(0)
width = struct.get_value("width")
height = struct.get_value("height")
frame_idx += 1
# Start ffmpeg encoder on the first frame.
if proc is None:
ok, fps_num, fps_den = struct.get_fraction("framerate")
fps = fps_num / fps_den if ok and fps_den > 0 else 25.0
alert_hold_frames = max(1, int(ALERT_HOLD_SECONDS * fps))
proc = subprocess.Popen(
["ffmpeg", "-y", "-f", "rawvideo", "-pix_fmt", "bgr24",
"-s", f"{width}x{height}", "-r", str(fps),
"-i", "pipe:0", "-c:v", "libx264", "-pix_fmt", "yuv420p",
"-movflags", "+faststart", "output_dlstreamer.mp4"],
stdin=subprocess.PIPE, stderr=subprocess.DEVNULL,
)
# Elapsed time since the previous frame from buffer timestamps.
pts = buf.pts / Gst.SECOND if buf.pts != Gst.CLOCK_TIME_NONE else frame_idx / fps
dt = (pts - prev_pts) if (prev_pts is not None and pts > prev_pts) else 1.0 / fps
prev_pts = pts
# Read detection / tracking metadata via GstAnalytics.
rmeta = GstAnalytics.buffer_get_analytics_relation_meta(buf)
regions = []
if rmeta is not None:
od_entries = []
trk_map = {} # metadata_id -> tracking_id
idx = 1
while True:
ok_od, od = rmeta.get_od_mtd(idx)
ok_trk, trk = rmeta.get_tracking_mtd(idx)
if not ok_od and not ok_trk:
break
if ok_od:
label = GLib.quark_to_string(od.get_obj_type())
_, x, y, w, h, _ = od.get_location()
od_entries.append((idx, label, int(x + w / 2), int(y + h / 2)))
if ok_trk:
ok2, tid, _, _, _ = trk.get_info()
if ok2:
trk_map[idx] = tid
idx += 1
for od_id, label, cx, cy in od_entries:
if label != "person":
continue
tid = 0
for trk_meta_id, tracking_id in trk_map.items():
if rmeta.get_relation(od_id, trk_meta_id) != GstAnalytics.RelTypes.NONE:
tid = tracking_id
break
regions.append((tid, cx, cy))
# Map buffer read-only and copy pixels to a writable numpy array.
success, map_info = buf.map(Gst.MapFlags.READ)
if not success:
continue
video_info = GstVideo.VideoInfo.new_from_caps(caps)
arr = np.ndarray((height, width, 3), dtype=np.uint8,
buffer=map_info.data, strides=(video_info.stride[0], 3, 1)).copy()
buf.unmap(map_info)
frame_running = False
for tid, cx, cy in regions:
px, py = prev_center.get(tid, (cx, cy))
speed = np.hypot(cx - px, cy - py) / dt if dt > 0 else 0.0
prev_center[tid] = (cx, cy)
run_frames[tid] = run_frames[tid] + 1 if speed >= RUNNING_SPEED else 0
if run_frames[tid] >= MIN_RUN_FRAMES:
frame_running = True
cv2.putText(arr, f"RUNNING {int(speed)}px/s", (cx - 40, max(cy - 20, 12)),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
if tid not in flagged:
flagged.add(tid)
print(f"RUNNING id={tid} speed={int(speed)}px/s frame={frame_idx}", flush=True)
# Latch the alert so the banner reflects a sustained state, not a single
# transient frame: once running is seen it stays on for alert_hold_frames.
alert_hold = alert_hold_frames if frame_running else max(0, alert_hold - 1)
alert_on = alert_hold > 0
banner = "RUNNING DETECTED" if alert_on else "NO RUNNING DETECTED"
banner_color = (0, 0, 255) if alert_on else (0, 180, 0)
cv2.rectangle(arr, (0, 0), (width, 40), (0, 0, 0), -1)
cv2.putText(arr, banner, (10, 28),
cv2.FONT_HERSHEY_SIMPLEX, 0.9, banner_color, 2)
proc.stdin.write(arr.tobytes())
pipeline.set_state(Gst.State.NULL)
if proc:
proc.stdin.close()
proc.wait()
print("Wrote output_dlstreamer.mp4", flush=True)
|