running-detection / dlstreamer_sample.py
vagheshpatel's picture
Sync running-detection from metro-analytics-catalog
953285e verified
Raw History Blame Contribute Delete
6.07 kB
# 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)