-
Notifications
You must be signed in to change notification settings - Fork 66
/
ode_persistence_trigger_fill_tracked_objects.py
306 lines (267 loc) · 12.8 KB
/
ode_persistence_trigger_fill_tracked_objects.py
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
################################################################################
# The MIT License
#
# Copyright (c) 2021-2023, Prominence AI, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
################################################################################
################################################################################
#
# This example demonstrates the use of three ODE Persistence Triggers to trigger on
# all tracked Objects - as identified by an IOU Tracker - that persist accross consecutive
# frames for a specifid period of time. Each trigger specifies a range of minimum and
# maximum times of persistence.
# Trigger 1: 0 - 3 seconds - action = fill object with opaque green color
# Trigger 2: 3 - 6 seconds - action = fill object with opaque yellow color
# Trigger 3: 6 - 0 seconds - action = fill object with opaque red color
# This will have the effect of coloring an object by its time in view
#
# The example uses a basic inference Pipeline consisting of:
# - A URI Source
# - Primary GST Inference Engine (PGIE)
# - IOU Tracker
# - On-Screen Display
# - Window Sink
#
################################################################################
#!/usr/bin/env python
import sys
from dsl import *
uri_h265 = "/opt/nvidia/deepstream/deepstream/samples/streams/sample_1080p_h265.mp4"
# Filespecs for the Primary GIE
primary_infer_config_file = \
'/opt/nvidia/deepstream/deepstream/samples/configs/deepstream-app/config_infer_primary.txt'
primary_model_engine_file = \
'/opt/nvidia/deepstream/deepstream/samples/models/Primary_Detector/resnet18_trafficcamnet.etlt_b8_gpu0_int8.engine'
# Filespec for the IOU Tracker config file
iou_tracker_config_file = \
'/opt/nvidia/deepstream/deepstream/samples/configs/deepstream-app/config_tracker_IOU.yml'
PGIE_CLASS_ID_VEHICLE = 0
PGIE_CLASS_ID_BICYCLE = 1
PGIE_CLASS_ID_PERSON = 2
PGIE_CLASS_ID_ROADSIGN = 3
WINDOW_WIDTH = DSL_1K_HD_WIDTH // 2
WINDOW_HEIGHT = DSL_1K_HD_HEIGHT // 2
##
# Function to be called on XWindow KeyRelease event
##
def xwindow_key_event_handler(key_string, client_data):
print('key released = ', key_string)
if key_string.upper() == 'P':
dsl_pipeline_pause('pipeline')
elif key_string.upper() == 'R':
dsl_pipeline_play('pipeline')
elif key_string.upper() == 'Q' or key_string == '' or key_string == '':
dsl_pipeline_stop('pipeline')
dsl_main_loop_quit()
##
# Function to be called on XWindow Delete event
##
def xwindow_delete_event_handler(client_data):
print('delete window event')
dsl_pipeline_stop('pipeline')
dsl_main_loop_quit()
##
# Function to be called on End-of-Stream (EOS) event
##
def eos_event_listener(client_data):
print('Pipeline EOS event')
dsl_pipeline_stop('pipeline')
dsl_main_loop_quit()
##
# Function to be called on every change of Pipeline state
##
def state_change_listener(old_state, new_state, client_data):
print('previous state = ', old_state, ', new state = ', new_state)
if new_state == DSL_STATE_PLAYING:
dsl_pipeline_dump_to_dot('pipeline', "state-playing")
def main(args):
# Since we're not using args, we can Let DSL initialize GST on first call
while True:
#```````````````````````````````````````````````````````````````````````````````````
# Create a Format Label Action to remove the Object Label from view
# Note: the label can be disabled with the OSD API as well.
retval = dsl_ode_action_label_format_new('remove-label',
font=None, has_bg_color=False, bg_color=None)
if retval != DSL_RETURN_SUCCESS:
break
# Create a Format Bounding Box Action to remove the box border from view
retval = dsl_ode_action_bbox_format_new('remove-border', border_width=0,
border_color=None, has_bg_color=False, bg_color=None)
if retval != DSL_RETURN_SUCCESS:
break
# Create an Any-Class Occurrence Trigger for our remove label and bbox actions
retval = dsl_ode_trigger_occurrence_new('every-occurrence-trigger', source='uri-source-1',
class_id=DSL_ODE_ANY_CLASS, limit=DSL_ODE_TRIGGER_LIMIT_NONE)
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_ode_trigger_action_add_many('every-occurrence-trigger',
actions=['remove-label', 'remove-border', None])
if retval != DSL_RETURN_SUCCESS:
break
#```````````````````````````````````````````````````````````````````````````````````
# Create three new RGBA fill colors to fill the bounding boxes of new objects
retval = dsl_display_type_rgba_color_custom_new('opaque-green', red=0.0, green=1.0, blue=0.0, alpha=0.3)
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_display_type_rgba_color_custom_new('opaque-yellow', red=1.0, green=1.0, blue=0.0, alpha=0.3)
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_display_type_rgba_color_custom_new('opaque-red', red=1.0, green=0.0, blue=0.0, alpha=0.3)
if retval != DSL_RETURN_SUCCESS:
break
#```````````````````````````````````````````````````````````````````````````````````
# Create three new Actions to fill the bounding boxes, one for each Persistence Trigger
retval = dsl_ode_action_bbox_format_new('fill-opaque-green',
border_width = 0,
border_color = None,
has_bg_color = True,
bg_color = 'opaque-green')
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_ode_action_bbox_format_new('fill-opaque-yellow',
border_width = 0,
border_color = None,
has_bg_color = True,
bg_color = 'opaque-yellow')
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_ode_action_bbox_format_new('fill-opaque-red',
border_width = 0,
border_color = None,
has_bg_color = True,
bg_color = 'opaque-red')
if retval != DSL_RETURN_SUCCESS:
break
#```````````````````````````````````````````````````````````````````````````````````
# Create the three persistence triggers for the PERSON class, each with their unique range
# Set the minimum hight critera - we only care about people that are near the Camera
retval = dsl_ode_trigger_persistence_new('minimum-persitence-trigger', source='uri-source-1',
class_id=PGIE_CLASS_ID_PERSON, limit=DSL_ODE_TRIGGER_LIMIT_NONE, minimum=0, maximum=2)
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_ode_trigger_dimensions_min_set('minimum-persitence-trigger',
min_width=0, min_height=100)
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_ode_trigger_persistence_new('medium-persitence-trigger', source='uri-source-1',
class_id=PGIE_CLASS_ID_PERSON, limit=DSL_ODE_TRIGGER_LIMIT_NONE, minimum=2, maximum=4)
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_ode_trigger_dimensions_min_set('medium-persitence-trigger',
min_width=0, min_height=100)
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_ode_trigger_persistence_new('maximum-persitence-trigger', source='uri-source-1',
class_id=PGIE_CLASS_ID_PERSON, limit=DSL_ODE_TRIGGER_LIMIT_NONE, minimum=4, maximum=0)
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_ode_trigger_dimensions_min_set('maximum-persitence-trigger',
min_width=0, min_height=100)
if retval != DSL_RETURN_SUCCESS:
break
#```````````````````````````````````````````````````````````````````````````````````
# Next, we add our Actions to our Triggers
retval = dsl_ode_trigger_action_add('minimum-persitence-trigger', action='fill-opaque-green')
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_ode_trigger_action_add('medium-persitence-trigger', action='fill-opaque-yellow')
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_ode_trigger_action_add('maximum-persitence-trigger', action='fill-opaque-red')
if retval != DSL_RETURN_SUCCESS:
break
#```````````````````````````````````````````````````````````````````````````````````
# New ODE Handler to handle all ODE Triggers
retval = dsl_pph_ode_new('ode-handler')
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_pph_ode_trigger_add_many('ode-handler', triggers=[
'every-occurrence-trigger',
'minimum-persitence-trigger',
'medium-persitence-trigger',
'maximum-persitence-trigger',
None])
if retval != DSL_RETURN_SUCCESS:
break
####################################################################################
#
# Create the remaining Pipeline components
# New URI File Source using the filespec defined above
retval = dsl_source_uri_new('uri-source-1', uri_h265, False, False, 0)
if retval != DSL_RETURN_SUCCESS:
break
# New Primary GIE using the filespecs above with interval = 0
retval = dsl_infer_gie_primary_new('primary-gie',
primary_infer_config_file, primary_model_engine_file, 1)
if retval != DSL_RETURN_SUCCESS:
break
# New IOU Tracker, setting operational width and hieght
retval = dsl_tracker_new('iou-tracker', iou_tracker_config_file, 480, 272)
if retval != DSL_RETURN_SUCCESS:
break
# New OSD with text, clock and bbox display all enabled.
retval = dsl_osd_new('on-screen-display',
text_enabled=True, clock_enabled=True, bbox_enabled=True, mask_enabled=False)
if retval != DSL_RETURN_SUCCESS:
break
# Add our ODE Pad Probe Handler to the Sink pad of the OSD
retval = dsl_osd_pph_add('on-screen-display',
handler='ode-handler', pad=DSL_PAD_SINK)
if retval != DSL_RETURN_SUCCESS:
break
# New Window Sink, 0 x/y offsets and dimensions
retval = dsl_sink_window_egl_new('egl-sink', 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)
if retval != DSL_RETURN_SUCCESS:
break
# Add the XWindow event handler functions defined above to the Window Sink
retval = dsl_sink_window_key_event_handler_add('egl-sink',
xwindow_key_event_handler, None)
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_sink_window_delete_event_handler_add('egl-sink',
xwindow_delete_event_handler, None)
if retval != DSL_RETURN_SUCCESS:
break
# Add all the components to our pipeline
retval = dsl_pipeline_new_component_add_many('pipeline',
['uri-source-1', 'primary-gie', 'iou-tracker',
'on-screen-display', 'egl-sink', None])
if retval != DSL_RETURN_SUCCESS:
break
## Add the listener callback functions defined above
retval = dsl_pipeline_state_change_listener_add('pipeline', state_change_listener, None)
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_pipeline_eos_listener_add('pipeline', eos_event_listener, None)
if retval != DSL_RETURN_SUCCESS:
break
# Play the pipeline
retval = dsl_pipeline_play('pipeline')
if retval != DSL_RETURN_SUCCESS:
break
dsl_main_loop_run()
retval = DSL_RETURN_SUCCESS
break
# Print out the final result
print(dsl_return_value_to_string(retval))
# Cleanup all DSL/GST resources
dsl_delete_all()
if __name__ == '__main__':
sys.exit(main(sys.argv))