Repository navigation
Expand file tree
/
Copy pathdeepstream_pose_estimation_app.py
More file actions
672 lines (524 loc) · 21.8 KB
/
Copy pathdeepstream_pose_estimation_app.py
File metadata and controls
672 lines (524 loc) · 21.8 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
import sys
import numpy as np
from typing import List, Tuple
import queue as queue_module
import math
import ctypes # Required for pointer operations
import pyds
from post_process import refine_peaks, paf_score_graph, find_peaks, assignment, connect_parts, topology
import pyds
import gi
import platform
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GLib
# Platform check (equivalent of #ifdef PLATFORM_TEGRA in C++)
# Jetson devices typically use the 'aarch64' architecture.
IS_TEGRA = platform.machine() == 'aarch64'
# consts
EPS = 1e-6
MAX_DISPLAY_LEN = 64
MUXER_OUTPUT_WIDHT = 1920
MUXER_OUTPUT_HEIGHT = 1080
MUXER_BATCH_TIMEOUT_SEC = 4000000
MAX_ELEMENTS_IN_DISPLAY_META = 16
M = 2
frame_number = 0
# Type aliases to match C++ template definitions
# Vec1D<T> equivalent
Vec1DInt = List[int]
Vec1DFloat = List[float]
# Vec2D<T> equivalent
Vec2DInt = List[List[int]]
Vec2DFloat = List[List[float]]
# Vec3D<T> equivalent
Vec3DInt = List[List[List[int]]]
Vec3DFloat = List[List[List[float]]]
def parse_objects_from_tensor_meta(tensor_meta) -> Tuple[Vec2DInt, Vec3DFloat]:
"""
Python port of the C++ parse_objects_from_tensor_meta function.
"""
# Parameters
threshold = 0.1
window_size = 5
max_num_parts = 20
num_integral_samples = 7
link_threshold = 0.1
max_num_objects = 100
# --- Layer 0: CMAP data access ---
# C++: tensor_meta->out_buf_ptrs_host[0]
layer_info_0 = pyds.get_nvds_LayerInfo(tensor_meta, 0)
cmap_dims = layer_info_0.inferDims
ptr_0 = pyds.get_ptr(layer_info_0.buffer)
# Cast the pointer to a float array
c_type_pointer_0 = ctypes.cast(ptr_0, ctypes.POINTER(ctypes.c_float))
# Create a NumPy array (without copying, as a memory view)
cmap_data = np.ctypeslib.as_array(c_type_pointer_0, shape=(cmap_dims.numElements,))
# --- Layer 1: PAF data access ---
# C++: tensor_meta->out_buf_ptrs_host[1]
layer_info_1 = pyds.get_nvds_LayerInfo(tensor_meta, 1)
paf_dims = layer_info_1.inferDims
ptr_1 = pyds.get_ptr(layer_info_1.buffer)
c_type_pointer_1 = ctypes.cast(ptr_1, ctypes.POINTER(ctypes.c_float))
paf_data = np.ctypeslib.as_array(c_type_pointer_1, shape=(paf_dims.numElements,))
# --- Algorithm flow ---
# 1. Finding peaks
# Note: the Python version returns counts and peaks (C++ took them by reference)
counts, peaks = find_peaks(cmap_data, cmap_dims, threshold, window_size, max_num_parts)
# 2. Non-Maximum Suppression
refined_peaks = refine_peaks(counts, peaks, cmap_data, cmap_dims, window_size)
# 3. Bipartite graph creation
# topology must be defined as a global variable
score_graph = paf_score_graph(paf_data, paf_dims, topology, counts, refined_peaks, num_integral_samples)
# 4. Assignment
connections = assignment(score_graph, topology, counts, link_threshold, max_num_parts)
# 5. Connect parts
objects = connect_parts(connections, topology, counts, max_num_objects)
return objects, refined_peaks
def create_display_meta(objects, normalized_peaks, frame_meta, frame_width, frame_height):
"""
Python/pyds port of the C++ create_display_meta function.
"""
import pyds # Can be imported inside the function or globally
K = len(topology)
# Length of the objects list
# count = len(objects) # Not needed with a Python for loop, but the logic is the same
# 1. Get the batch meta and the first display meta
bmeta = frame_meta.base_meta.batch_meta
dmeta = pyds.nvds_acquire_display_meta_from_pool(bmeta)
pyds.nvds_add_display_meta_to_frame(frame_meta, dmeta)
for obj in objects:
C = len(obj)
# --- PART 1: CIRCLES (Joints) ---
for j in range(C):
k = obj[j]
if k >= 0:
peak = normalized_peaks[j][k]
# Coordinate calculation (float * width -> int, as in C++)
# Note: the C++ code uses peak[1] as X and peak[0] as Y.
x = int(peak[1] * MUXER_OUTPUT_WIDHT)
y = int(peak[0] * MUXER_OUTPUT_HEIGHT)
# Meta limit check (usually 16 in DeepStream)
if dmeta.num_circles == MAX_ELEMENTS_IN_DISPLAY_META:
dmeta = pyds.nvds_acquire_display_meta_from_pool(bmeta)
pyds.nvds_add_display_meta_to_frame(frame_meta, dmeta)
# Set circle parameters
cparams = dmeta.circle_params[dmeta.num_circles]
cparams.xc = x
cparams.yc = y
cparams.radius = 8
# Color: {244, 67, 54, 1}
cparams.circle_color.red = 244
cparams.circle_color.green = 67
cparams.circle_color.blue = 54
cparams.circle_color.alpha = 1.0
cparams.has_bg_color = 1
# Background color: {0, 255, 0, 1}
cparams.bg_color.red = 0
cparams.bg_color.green = 255
cparams.bg_color.blue = 0
cparams.bg_color.alpha = 1.0
dmeta.num_circles += 1
# --- PART 2: LINES (Limbs) ---
for k in range(K):
c_a = topology[k][2]
c_b = topology[k][3]
if obj[c_a] >= 0 and obj[c_b] >= 0:
peak0 = normalized_peaks[c_a][obj[c_a]]
peak1 = normalized_peaks[c_b][obj[c_b]]
x0 = int(peak0[1] * MUXER_OUTPUT_WIDHT)
y0 = int(peak0[0] * MUXER_OUTPUT_HEIGHT)
x1 = int(peak1[1] * MUXER_OUTPUT_WIDHT)
y1 = int(peak1[0] * MUXER_OUTPUT_HEIGHT)
# Meta limit check
if dmeta.num_lines == MAX_ELEMENTS_IN_DISPLAY_META:
dmeta = pyds.nvds_acquire_display_meta_from_pool(bmeta)
pyds.nvds_add_display_meta_to_frame(frame_meta, dmeta)
# Set line parameters
lparams = dmeta.line_params[dmeta.num_lines]
lparams.x1 = x0
lparams.y1 = y0
lparams.x2 = x1
lparams.y2 = y1
lparams.line_width = 3
# Color: {0, 255, 0, 1}
lparams.line_color.red = 0
lparams.line_color.green = 255
lparams.line_color.blue = 0
lparams.line_color.alpha = 1.0
dmeta.num_lines += 1
def pgie_src_pad_buffer_probe(pad, info, u_data):
"""
Python/pyds port of the C++ pgie_src_pad_buffer_probe function.
Extracts the metadata coming from PGIE and updates the drawing parameters.
"""
gst_buffer = info.get_buffer()
if not gst_buffer:
print("Unable to get GstBuffer")
return Gst.PadProbeReturn.OK
# Get the batch meta via the buffer hash
batch_meta = pyds.gst_buffer_get_nvds_batch_meta(hash(gst_buffer))
# --- Iterate over the frame meta list ---
l_frame = batch_meta.frame_meta_list
while l_frame is not None:
try:
# Note: the cast is equivalent to (NvDsFrameMeta *)l_frame->data in C++
frame_meta = pyds.NvDsFrameMeta.cast(l_frame.data)
except StopIteration:
break
# --- 1. Frame user meta list (for frame-level tensor data) ---
l_user = frame_meta.frame_user_meta_list
while l_user is not None:
try:
user_meta = pyds.NvDsUserMeta.cast(l_user.data)
except StopIteration:
break
# if (user_meta->base_meta.meta_type == NVDSINFER_TENSOR_OUTPUT_META)
if user_meta.base_meta.meta_type == pyds.NVDSINFER_TENSOR_OUTPUT_META:
# Tensor Meta Cast
tensor_meta = pyds.NvDsInferTensorMeta.cast(user_meta.user_meta_data)
# tie(objects, normalized_peaks) = parse_objects_from_tensor_meta(...)
objects, normalized_peaks = parse_objects_from_tensor_meta(tensor_meta)
# create_display_meta(...)
create_display_meta(
objects,
normalized_peaks,
frame_meta,
frame_meta.source_frame_width,
frame_meta.source_frame_height
)
try:
l_user = l_user.next
except StopIteration:
break
# --- 2. Object meta list (for object-level tensor data) ---
l_obj = frame_meta.obj_meta_list
while l_obj is not None:
try:
obj_meta = pyds.NvDsObjectMeta.cast(l_obj.data)
except StopIteration:
break
# Object user meta list
l_user_obj = obj_meta.obj_user_meta_list
while l_user_obj is not None:
try:
user_meta_obj = pyds.NvDsUserMeta.cast(l_user_obj.data)
except StopIteration:
break
# if (user_meta->base_meta.meta_type == NVDSINFER_TENSOR_OUTPUT_META)
if user_meta_obj.base_meta.meta_type == pyds.NVDSINFER_TENSOR_OUTPUT_META:
tensor_meta_obj = pyds.NvDsInferTensorMeta.cast(user_meta_obj.user_meta_data)
objects, normalized_peaks = parse_objects_from_tensor_meta(tensor_meta_obj)
create_display_meta(
objects,
normalized_peaks,
frame_meta,
frame_meta.source_frame_width,
frame_meta.source_frame_height
)
try:
l_user_obj = l_user_obj.next
except StopIteration:
break
try:
l_obj = l_obj.next
except StopIteration:
break
try:
l_frame = l_frame.next
except StopIteration:
break
return Gst.PadProbeReturn.OK
def osd_sink_pad_buffer_probe(pad, info, u_data):
global frame_number # Use the global counter
gst_buffer = info.get_buffer()
if not gst_buffer:
print("Unable to get GstBuffer")
return Gst.PadProbeReturn.OK
# Get the batch meta via the buffer hash
batch_meta = pyds.gst_buffer_get_nvds_batch_meta(hash(gst_buffer))
l_frame = batch_meta.frame_meta_list
while l_frame is not None:
try:
frame_meta = pyds.NvDsFrameMeta.cast(l_frame.data)
except StopIteration:
break
# --- Object loop from the C++ code ---
# (The original code only performed the assignment; kept the same for fidelity)
l_obj = frame_meta.obj_meta_list
while l_obj is not None:
try:
# Only the cast is performed; the data is not used
obj_meta = pyds.NvDsObjectMeta.cast(l_obj.data)
except StopIteration:
break
try:
l_obj = l_obj.next
except StopIteration:
break
# --- Display meta operations ---
display_meta = pyds.nvds_acquire_display_meta_from_pool(batch_meta)
# Set text parameters
display_meta.num_labels = 1
txt_params = display_meta.text_params[0]
# Set the text: "Frame Number = %d"
txt_params.display_text = f"Frame Number = {frame_number}"
# Coordinates
txt_params.x_offset = 10
txt_params.y_offset = 12
# Font settings
txt_params.font_params.font_name = "Mono"
txt_params.font_params.font_size = 10
# Font color (white: 1.0, 1.0, 1.0, 1.0)
txt_params.font_params.font_color.red = 1.0
txt_params.font_params.font_color.green = 1.0
txt_params.font_params.font_color.blue = 1.0
txt_params.font_params.font_color.alpha = 1.0
# Background settings
txt_params.set_bg_clr = 1
# Background color (black: 0.0, 0.0, 0.0, 1.0)
txt_params.text_bg_clr.red = 0.0
txt_params.text_bg_clr.green = 0.0
txt_params.text_bg_clr.blue = 0.0
txt_params.text_bg_clr.alpha = 1.0
# Attach the prepared display meta to the frame
pyds.nvds_add_display_meta_to_frame(frame_meta, display_meta)
try:
l_frame = l_frame.next
except StopIteration:
break
# Increment the frame counter
frame_number += 1
return Gst.PadProbeReturn.OK
def bus_call(bus, msg, loop):
"""
Python port of the C++ bus_call function.
"""
t = msg.type
if t == Gst.MessageType.EOS:
sys.stdout.write("End of Stream\n")
loop.quit()
elif t == Gst.MessageType.ERROR:
err, debug = msg.parse_error()
# GST_OBJECT_NAME(msg->src) -> msg.src.get_name()
# g_printerr -> sys.stderr.write
sys.stderr.write(f"ERROR from element {msg.src.get_name()}: {err.message}\n")
if debug:
sys.stderr.write(f"Error details: {debug}\n")
# In Python there is no need to call g_free and g_error_free;
# the garbage collector handles it.
loop.quit()
return True
def link_element_to_tee_src_pad(tee, sinkelem):
"""
Requests a dynamic pad from the tee element and links it to the sink element.
"""
ret = False
tee_src_pad = None
sinkpad = None
# Instead of gst_element_request_pad(...) from the C code:
# In Python, 'get_request_pad' finds the template automatically.
tee_src_pad = tee.get_request_pad("src_%u")
if tee_src_pad is None:
sys.stderr.write("Failed to get src pad from tee\n")
return False
# Get the static pad from the sink element
sinkpad = sinkelem.get_static_pad("sink")
if sinkpad is None:
sys.stderr.write(f"Failed to get sink pad from '{sinkelem.get_name()}'\n")
return False
# Link
if tee_src_pad.link(sinkpad) != Gst.PadLinkReturn.OK:
sys.stderr.write(f"Failed to link '{tee.get_name()}' and '{sinkelem.get_name()}'\n")
return False
ret = True
return ret
# Constants (presumably #define'd in the C code; defined here)
MUXER_OUTPUT_WIDTH = 1920
MUXER_OUTPUT_HEIGHT = 1080
MUXER_BATCH_TIMEOUT_USEC = 4000000
def main():
# Initialize GStreamer
Gst.init(None)
transform = None # Tegra only
# Input argument check
if len(sys.argv) != 3:
sys.stderr.write(f"Usage: {sys.argv[0]} <filename> <output-path>\n")
return -1
# Create the main loop
loop = GLib.MainLoop()
# --- Element creation ---
# Pipeline
pipeline = Gst.Pipeline.new("deepstream-tensorrt-openpose-pipeline")
# Source
source = Gst.ElementFactory.make("filesrc", "file-source")
# Parser 1 & 2
h264parser = Gst.ElementFactory.make("h264parse", "h264-parser")
h264parser1 = Gst.ElementFactory.make("h264parse", "h264-parser1")
# Decoder (GPU-accelerated)
decoder = Gst.ElementFactory.make("nvv4l2decoder", "nvv4l2-decoder")
# Stream Muxer
streammux = Gst.ElementFactory.make("nvstreammux", "stream-muxer")
if not pipeline or not streammux:
sys.stderr.write("One element could not be created. Exiting.\n")
return -1
# Inference Engine (PGIE)
pgie = Gst.ElementFactory.make("nvinfer", "primary-nvinference-engine")
# Converter (NV12 -> RGBA)
nvvidconv = Gst.ElementFactory.make("nvvideoconvert", "nvvideo-converter")
# Queue and file output
queue = Gst.ElementFactory.make("queue", "queue")
filesink = Gst.ElementFactory.make("filesink", "filesink")
# Set the output file path
# C: strcat(output_path,"Pose_Estimation.mp4");
# String concatenation is safer in Python.
output_path_arg = sys.argv[2]
if not output_path_arg.endswith("/"):
# Simple path safety; the C code uses strcat directly, but do it the Python way
pass
final_output_path = output_path_arg + "Pose_Estimation.mp4"
filesink.set_property("location", final_output_path)
# Other elements
nvvideoconvert = Gst.ElementFactory.make("nvvideoconvert", "nvvideo-converter1")
tee = Gst.ElementFactory.make("tee", "TEE")
h264encoder = Gst.ElementFactory.make("nvv4l2h264enc", "video-encoder")
cap_filter = Gst.ElementFactory.make("capsfilter", "enc_caps_filter")
# Create caps
caps = Gst.Caps.from_string("video/x-raw(memory:NVMM), format=I420")
cap_filter.set_property("caps", caps)
qtmux = Gst.ElementFactory.make("qtmux", "muxer")
# OSD (On Screen Display)
nvosd = Gst.ElementFactory.make("nvdsosd", "nv-onscreendisplay")
# Sink and render elements
if IS_TEGRA:
transform = Gst.ElementFactory.make("nvegltransform", "nvegl-transform")
nvsink = Gst.ElementFactory.make("nveglglessink", "nvvideo-renderer")
sink = Gst.ElementFactory.make("fpsdisplaysink", "fps-display")
# Sink settings
# Note: the C code sets these but does not add the sink to the pipeline (commented out)
# However, it must be created because 'sink' is passed to the probe callback as user_data.
sink.set_property("text-overlay", False)
sink.set_property("video-sink", nvsink)
sink.set_property("sync", False)
# Element check
elements_list = [source, h264parser, decoder, pgie, nvvidconv, nvosd, sink,
cap_filter, tee, nvvideoconvert, h264encoder, filesink,
queue, qtmux, h264parser1]
if any(e is None for e in elements_list):
sys.stderr.write("One element could not be created. Exiting.\n")
return -1
if IS_TEGRA and transform is None:
sys.stderr.write("One tegra element could not be created. Exiting.\n")
return -1
# Source file setting
source.set_property("location", sys.argv[1])
# Streammux settings
streammux.set_property("width", MUXER_OUTPUT_WIDTH)
streammux.set_property("height", MUXER_OUTPUT_HEIGHT)
streammux.set_property("batch-size", 1)
streammux.set_property("batched-push-timeout", MUXER_BATCH_TIMEOUT_USEC)
# PGIE settings
pgie.set_property("output-tensor-meta", True)
pgie.set_property("config-file-path", "deepstream_pose_estimation_config.txt")
# Bus Handler
bus = pipeline.get_bus()
bus_watch_id = bus.add_watch(GLib.PRIORITY_DEFAULT, bus_call, loop)
# --- Add elements to the pipeline ---
# Logic follows the #ifdef and #else blocks in the C++ code:
# Common elements
pipeline.add(source)
pipeline.add(h264parser)
pipeline.add(decoder)
pipeline.add(streammux)
pipeline.add(pgie)
pipeline.add(nvvidconv)
pipeline.add(nvosd)
if IS_TEGRA:
pipeline.add(transform)
# The sink was commented out on Tegra, so it is not added.
# Common elements (continued)
pipeline.add(tee)
pipeline.add(nvvideoconvert)
pipeline.add(h264encoder)
pipeline.add(cap_filter)
pipeline.add(filesink)
pipeline.add(queue)
pipeline.add(h264parser1)
pipeline.add(qtmux)
# --- Linking ---
# 1. Source -> Parser -> Decoder
print("Linking Source -> Parser -> Decoder")
source.link(h264parser)
h264parser.link(decoder)
# 2. Decoder -> Streammux (via request pad)
sinkpad = streammux.get_request_pad("sink_0")
if not sinkpad:
sys.stderr.write("Streammux request sink pad failed. Exiting.\n")
return -1
srcpad = decoder.get_static_pad("src")
if not srcpad:
sys.stderr.write("Decoder request src pad failed. Exiting.\n")
return -1
if srcpad.link(sinkpad) != Gst.PadLinkReturn.OK:
sys.stderr.write("Failed to link decoder to stream muxer. Exiting.\n")
return -1
# 3. Streammux -> PGIE -> NVVidConv -> NVOSD -> ...
# Active block in the C++ code (filesink path):
# stream -> pgie -> nvvidconv -> nvosd -> tee
print("Linking Streammux chain...")
# Was transform used on Tegra? In the C code:
# Tegra: streammux, pgie, nvvidconv, nvosd, tee (transform is not in the chain here)
# else: streammux, pgie, nvvidconv, nvosd, tee
# Transform is only used when the display sink is enabled; here a file sink is used.
# The C code does add `transform` to the bin (on Tegra), but in the link chain it appears to be inside #if 0.
# Looking at the active #else block at the bottom:
# if (!gst_element_link_many(streammux, pgie, nvvidconv, nvosd, tee, NULL))
# The Tegra block is the same. Transform is not used in linking.
streammux.link(pgie)
pgie.link(nvvidconv)
nvvidconv.link(nvosd)
nvosd.link(tee)
# 4. Tee -> Queue -> ... -> FileSink
# Call link_element_to_tee_src_pad
if not link_element_to_tee_src_pad(tee, queue):
sys.stderr.write("Could not link tee to queue/nvvideoconvert\n")
return -1
# Chain after the queue
# queue -> nvvideoconvert -> cap_filter -> h264encoder -> h264parser1 -> qtmux -> filesink
queue.link(nvvideoconvert)
nvvideoconvert.link(cap_filter)
cap_filter.link(h264encoder)
h264encoder.link(h264parser1)
h264parser1.link(qtmux)
qtmux.link(filesink)
# --- Add probes ---
# PGIE Src Pad Probe
pgie_src_pad = pgie.get_static_pad("src")
if not pgie_src_pad:
sys.stdout.write("Unable to get pgie src pad\n")
else:
# Note: the sink pointer is passed as user_data.
# In Python, the sink object is passed.
pgie_src_pad.add_probe(Gst.PadProbeType.BUFFER, pgie_src_pad_buffer_probe, sink)
# OSD Sink Pad Probe
osd_sink_pad = nvosd.get_static_pad("sink")
if not osd_sink_pad:
sys.stdout.write("Unable to get sink pad\n")
else:
osd_sink_pad.add_probe(Gst.PadProbeType.BUFFER, osd_sink_pad_buffer_probe, sink)
# --- Run ---
sys.stdout.write(f"Now playing: {sys.argv[1]}\n")
pipeline.set_state(Gst.State.PLAYING)
sys.stdout.write("Running...\n")
try:
loop.run()
except:
pass
# --- Shutdown ---
sys.stdout.write("Returned, stopping playback\n")
pipeline.set_state(Gst.State.NULL)
sys.stdout.write("Deleting pipeline\n")
# In Python, unref is handled automatically by the GC, but
# calling quit on the GLib loop is good practice.
return 0
if __name__ == '__main__':
sys.exit(main())