# SPDX-License-Identifier: MIT # Copyright (C) Intel Corporation # # Vehicle Entry/Exit Logging: DLStreamer pipeline sample. # See README.md for prerequisites and inputs. Run: python3 dlstreamer_sample.py import gi gi.require_version("Gst", "1.0") from gi.repository import Gst from gstgva import VideoFrame Gst.init([]) INPUT_VIDEO = "smart_parking_720p_30fps.mp4" VEHICLE_LABELS = {"car"} LINE_RATIO = 0.6 # 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=short-term-imageless ! queue ! " "identity name=probe ! " "gvawatermark displ-cfg=show-roi=car ! " "videoconvert ! video/x-raw,format=I420 ! " "openh264enc ! h264parse ! " "mp4mux ! filesink location=output_dlstreamer.mp4" ) pipeline = Gst.parse_launch(pipeline_str) prev_positions: dict[int, int] = {} entry_time: dict[int, float] = {} entered = 0 exited = 0 frame_height = 0 def fmt_time(seconds: float) -> str: """Format elapsed video time as MM:SS.mmm.""" minutes, secs = divmod(seconds, 60) return f"{int(minutes):02d}:{secs:06.3f}" def on_buffer(pad, info): global entered, exited, frame_height buf = info.get_buffer() caps = pad.get_current_caps() if caps and frame_height == 0: frame_height = caps.get_structure(0).get_value("height") or 720 line_y = int(frame_height * LINE_RATIO) t = buf.pts / Gst.SECOND if buf.pts != Gst.CLOCK_TIME_NONE else 0.0 frame = VideoFrame(buf, caps=caps) current: dict[int, int] = {} for region in frame.regions(): if region.label() not in VEHICLE_LABELS: continue rect = region.rect() cy = int(rect.y + rect.h / 2) tid = region.object_id() current[tid] = cy if tid in prev_positions: py = prev_positions[tid] if py < line_y <= cy: exited += 1 enter_t = entry_time.pop(tid, None) if enter_t is not None: print( f"EXIT track={tid:<3} entry={fmt_time(enter_t)} " f"exit={fmt_time(t)}", flush=True) else: print(f"EXIT track={tid:<3} exit={fmt_time(t)}", flush=True) elif py >= line_y > cy: entered += 1 entry_time[tid] = t print(f"ENTRY track={tid:<3} entry={fmt_time(t)}", flush=True) prev_positions.clear() prev_positions.update(current) return Gst.PadProbeReturn.OK probe = pipeline.get_by_name("probe") probe.get_static_pad("src").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) print(f"Total: entered={entered} exited={exited}", flush=True)