Download dlstreamer_sample.py from Intel/crowd-analysis: direct link, hf CLI and curl.
- Browser
- Download file 3.6 kB
-
https://huggingface.co/Intel/crowd-analysis/resolve/main/dlstreamer_sample.py
- Command line
-
hf download hf://Intel/crowd-analysis/dlstreamer_sample.py
-
curl -L -o dlstreamer_sample.py https://huggingface.co/Intel/crowd-analysis/resolve/main/dlstreamer_sample.py
3.6 kB
| # SPDX-License-Identifier: MIT | |
| # Copyright (C) Intel Corporation | |
| # | |
| # Crowd Analysis: DLStreamer pipeline sample. | |
| # See README.md for prerequisites and inputs. Run: python3 dlstreamer_sample.py | |
| import gi | |
| gi.require_version("Gst", "1.0") | |
| gi.require_version("GstAnalytics", "1.0") | |
| from gi.repository import Gst, GLib, GstAnalytics | |
| Gst.init([]) | |
| INPUT_VIDEO = "test_video.mp4" | |
| # Crowd-density thresholds (person count per frame). | |
| DENSITY_LOW_MAX = 10 # fewer than 10 -> LOW | |
| DENSITY_MEDIUM_MAX = 25 # 10-25 -> MEDIUM, more than 25 -> HIGH | |
| def density_level(count): | |
| if count < DENSITY_LOW_MAX: | |
| return "LOW" | |
| if count <= DENSITY_MEDIUM_MAX: | |
| return "MEDIUM" | |
| return "HIGH" | |
| # 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). | |
| 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=zero-term-imageless ! queue ! " | |
| "gvawatermark displ-cfg=show-roi=person ! " | |
| "videoconvert ! video/x-raw,format=I420 ! " | |
| "openh264enc ! h264parse ! " | |
| "mp4mux ! filesink name=sink location=output_dlstreamer.mp4" | |
| ) | |
| pipeline = Gst.parse_launch(pipeline_str) | |
| sink = pipeline.get_by_name("sink") | |
| sink_pad = sink.get_static_pad("sink") | |
| prev_centroid = {} # track_id -> (cx, cy) | |
| def on_buffer(pad, info): | |
| buf = info.get_buffer() | |
| rmeta = GstAnalytics.buffer_get_analytics_relation_meta(buf) | |
| if rmeta is None: | |
| return Gst.PadProbeReturn.OK | |
| # OD and tracking metadata share one id space and can be interleaved | |
| # (id=1 -> ODMtd, id=2 -> TrackingMtd, ...), so scan every id and stop only | |
| # after several consecutive misses. | |
| ods, tracks = [], [] | |
| idx, misses = 1, 0 | |
| while misses < 20: | |
| ok_od, od = rmeta.get_od_mtd(idx) | |
| ok_trk, trk = rmeta.get_tracking_mtd(idx) | |
| if ok_od: | |
| ods.append(od) | |
| misses = 0 | |
| elif ok_trk: | |
| tracks.append(trk) | |
| misses = 0 | |
| else: | |
| misses += 1 | |
| idx += 1 | |
| count, moves = 0, [] | |
| for od in ods: | |
| if GLib.quark_to_string(od.get_obj_type()) != "person": | |
| continue | |
| count += 1 | |
| _, x, y, w, h, _ = od.get_location() | |
| cx, cy = x + w / 2.0, y + h / 2.0 | |
| for trk in tracks: | |
| if rmeta.get_relation(od.id, trk.id) == GstAnalytics.RelTypes.NONE: | |
| continue | |
| ok_trk, track_id, _, _, _ = trk.get_info() | |
| if not ok_trk: | |
| continue | |
| if track_id in prev_centroid: | |
| pcx, pcy = prev_centroid[track_id] | |
| moves.append((cx - pcx, cy - pcy)) | |
| prev_centroid[track_id] = (cx, cy) | |
| break | |
| if count: | |
| level = density_level(count) | |
| if moves: | |
| mdx = sum(m[0] for m in moves) / len(moves) | |
| mdy = sum(m[1] for m in moves) / len(moves) | |
| print(f"Crowd: {count} ({level}) flow dx={mdx:+.1f} dy={mdy:+.1f}", | |
| flush=True) | |
| else: | |
| print(f"Crowd: {count} ({level})", flush=True) | |
| return Gst.PadProbeReturn.OK | |
| sink_pad.add_probe(Gst.PadProbeType.BUFFER, on_buffer) | |
| pipeline.set_state(Gst.State.PLAYING) | |
| bus = pipeline.get_bus() | |
| bus.timed_pop_filtered( | |
| Gst.CLOCK_TIME_NONE, | |
| Gst.MessageType.EOS | Gst.MessageType.ERROR, | |
| ) | |
| pipeline.set_state(Gst.State.NULL) | |