-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathpython-app-template.py
More file actions
185 lines (146 loc) · 6.28 KB
/
python-app-template.py
File metadata and controls
185 lines (146 loc) · 6.28 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# ==============================================================================
# Copyright (C) 2026 Intel Corporation
#
# SPDX-License-Identifier: MIT
# ==============================================================================
"""
DLStreamer <APPLICATION_NAME> pipeline.
Pipeline:
filesrc → decodebin3 →
gvadetect → gvafpscounter → gvawatermark →
gvametaconvert → gvametapublish (JSON Lines) →
videoconvert → vah264enc → h264parse → mp4mux → filesink
Supports file, HTTP URL, and RTSP IP camera inputs.
"""
import argparse
import os
import signal
import subprocess
import sys
import urllib.request
from pathlib import Path
import gi
gi.require_version("Gst", "1.0")
# Prevent GStreamer from forking gst-plugin-scanner (a C subprocess that cannot
# resolve Python symbols). Scanning in-process lets libgstpython.so find the
# Python runtime that is already loaded.
os.environ.setdefault("GST_REGISTRY_FORK", "no")
from gi.repository import Gst # pylint: disable=no-name-in-module, wrong-import-position
SCRIPT_DIR = Path(__file__).resolve().parent
MODELS_DIR = SCRIPT_DIR / "models"
VIDEOS_DIR = SCRIPT_DIR / "videos"
RESULTS_DIR = SCRIPT_DIR / "results"
DEFAULT_VIDEO_URL = "<VIDEO_URL>"
# ── helpers ──────────────────────────────────────────────────────────────────
def parse_args():
p = argparse.ArgumentParser(description="DLStreamer <APPLICATION_NAME>")
p.add_argument(
"--input",
default=DEFAULT_VIDEO_URL,
help="Video file path, HTTP URL, or rtsp:// URI",
)
p.add_argument("--device", default="GPU", help="Inference device (default: GPU)")
p.add_argument("--output-video", default=str(RESULTS_DIR / "output.mp4"))
p.add_argument("--output-json", default=str(RESULTS_DIR / "results.jsonl"))
p.add_argument("--threshold", type=float, default=0.5, help="Detection threshold")
return p.parse_args()
def prepare_input(source: str) -> str:
"""Download video if HTTP URL; pass through for RTSP or local file."""
if source.startswith("rtsp://"):
return source
if source.startswith(("http://", "https://")):
VIDEOS_DIR.mkdir(parents=True, exist_ok=True)
name = source.rstrip("/").split("/")[-1]
local = VIDEOS_DIR / name
if not local.exists():
print(f"Downloading video: {source}")
subprocess.run([
"curl", "-L", "-o", str(local),
"-H", "Referer: https://www.pexels.com/",
"-H", "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36",
source,
], check=True, timeout=300)
print(f"Saved to: {local}")
return str(local)
if not os.path.isfile(source):
sys.stderr.write(f"Error: file not found: {source}\n")
sys.exit(1)
return os.path.abspath(source)
def find_model(pattern: str, label: str) -> str:
"""Glob for a model .xml inside MODELS_DIR."""
hits = sorted(MODELS_DIR.glob(pattern))
if not hits:
sys.stderr.write(f"Error: {label} model not found. Run: python3 export_models.py\n")
sys.exit(1)
return str(hits[0])
def check_device(requested: str, label: str) -> str:
"""Check device availability with fallback chain: NPU → GPU → CPU."""
if requested == "NPU" and not os.path.exists("/dev/accel/accel0"):
print(f"Warning: NPU not available for {label}, falling back to GPU")
requested = "GPU"
if requested == "GPU" and not os.path.exists("/dev/dri/renderD128"):
print(f"Warning: GPU not available for {label}, falling back to CPU")
requested = "CPU"
return requested
def build_source(src: str) -> str:
"""Build GStreamer source element string for file or RTSP."""
if src.startswith("rtsp://"):
return f"rtspsrc location={src} latency=100"
return f'filesrc location="{src}"'
def run_pipeline(pipeline):
"""Event loop with SIGINT → EOS for graceful RTSP shutdown."""
def _sigint(signum, frame):
pipeline.send_event(Gst.Event.new_eos())
prev = signal.signal(signal.SIGINT, _sigint)
bus = pipeline.get_bus()
pipeline.set_state(Gst.State.PLAYING)
try:
while True:
msg = bus.timed_pop_filtered(
100 * Gst.MSECOND,
Gst.MessageType.ERROR | Gst.MessageType.EOS,
)
if msg is None:
continue
if msg.type == Gst.MessageType.ERROR:
err, dbg = msg.parse_error()
print(f"Error from {msg.src.get_name()}: {err.message}\nDebug: {dbg}")
break
if msg.type == Gst.MessageType.EOS:
print("Pipeline complete.")
break
finally:
signal.signal(signal.SIGINT, prev)
pipeline.set_state(Gst.State.NULL)
# ── main ─────────────────────────────────────────────────────────────────────
def main():
args = parse_args()
# Prepare input
input_src = prepare_input(args.input)
# Locate models (adjust glob patterns for your models)
model_xml = find_model("**/*.xml", "detection")
# Output dirs
Path(args.output_video).parent.mkdir(parents=True, exist_ok=True)
Path(args.output_json).parent.mkdir(parents=True, exist_ok=True)
# Device fallback
device = check_device(args.device, "inference")
# Build and run pipeline
Gst.init(None)
source_el = build_source(input_src)
pipe = (
f"{source_el} ! decodebin3 ! "
f'gvadetect model="{model_xml}" device={device} '
f"batch-size=4 threshold={args.threshold} ! queue ! "
f"gvafpscounter ! gvawatermark ! "
f"gvametaconvert ! "
f'gvametapublish file-format=json-lines file-path="{args.output_json}" ! '
f"videoconvert ! vah264enc ! h264parse ! mp4mux ! "
f'filesink location="{args.output_video}"'
)
print(f"\nPipeline:\n{pipe}\n")
pipeline = Gst.parse_launch(pipe)
run_pipeline(pipeline)
print(f"\nOutput video: {args.output_video}")
print(f"Output JSON: {args.output_json}")
if __name__ == "__main__":
main()