1 /*
2 * GStreamer
3 * Copyright (C) 2020 Vivek R <123vivekr@gmail.com>
4 * Copyright (C) 2021 Cesar Fabian Orccon Chipana <cfoch.fabian@gmail.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Alternatively, the contents of this file may be used under the
25 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
26 * which case the following provisions apply instead of the ones
27 * mentioned above:
28 *
29 * This library is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU Library General Public
31 * License as published by the Free Software Foundation; either
32 * version 2 of the License, or (at your option) any later version.
33 *
34 * This library is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37 * Library General Public License for more details.
38 *
39 * You should have received a copy of the GNU Library General Public
40 * License along with this library; if not, write to the
41 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
42 * Boston, MA 02110-1301, USA.
43 */
44
45 /**
46 * SECTION:element-cvtracker
47 *
48 * Performs object tracking on videos and stores it in video buffer metadata.
49 *
50 * ## Example launch line
51 *
52 * ```
53 * gst-launch-1.0 v4l2src ! videoconvert ! cvtracker box-x=50 box-y=50 box-wdith=50 box-height=50 ! videoconvert ! xvimagesink
54 * ```
55 *
56 * Since: 1.20
57 */
58
59 #ifdef HAVE_CONFIG_H
60 #include <config.h>
61 #endif
62
63 #include "gstcvtracker.h"
64
65 GST_DEBUG_CATEGORY_STATIC (gst_cvtracker_debug);
66 #define GST_CAT_DEFAULT gst_cvtracker_debug
67
68 #define DEFAULT_PROP_INITIAL_X 50
69 #define DEFAULT_PROP_INITIAL_Y 50
70 #define DEFAULT_PROP_INITIAL_WIDTH 50
71 #define DEFAULT_PROP_INITIAL_HEIGHT 50
72
73 enum
74 {
75 PROP_0,
76 PROP_INITIAL_X,
77 PROP_INITIAL_Y,
78 PROP_INITIAL_WIDTH,
79 PROP_INITIAL_HEIGHT,
80 PROP_ALGORITHM,
81 PROP_DRAW,
82 };
83
84 #define GST_OPENCV_TRACKER_ALGORITHM (tracker_algorithm_get_type ())
85
86 /**
87 * GstOpenCVTrackerAlgorithm:
88 *
89 * Since: 1.20
90 */
91 static GType
tracker_algorithm_get_type(void)92 tracker_algorithm_get_type (void)
93 {
94 static GType algorithm = 0;
95 static const GEnumValue algorithms[] = {
96 {GST_OPENCV_TRACKER_ALGORITHM_BOOSTING, "the Boosting tracker", "Boosting"},
97 {GST_OPENCV_TRACKER_ALGORITHM_CSRT, "the CSRT tracker", "CSRT"},
98 {GST_OPENCV_TRACKER_ALGORITHM_KCF,
99 "the KCF (Kernelized Correlation Filter) tracker",
100 "KCF"},
101 {GST_OPENCV_TRACKER_ALGORITHM_MEDIANFLOW, "the Median Flow tracker",
102 "MedianFlow"},
103 {GST_OPENCV_TRACKER_ALGORITHM_MIL, "the MIL tracker", "MIL"},
104 {GST_OPENCV_TRACKER_ALGORITHM_MOSSE,
105 "the MOSSE (Minimum Output Sum of Squared Error) tracker", "MOSSE"},
106 {GST_OPENCV_TRACKER_ALGORITHM_TLD,
107 "the TLD (Tracking, learning and detection) tracker",
108 "TLD"},
109 {0, NULL, NULL},
110 };
111
112 if (!algorithm) {
113 algorithm =
114 g_enum_register_static ("GstOpenCVTrackerAlgorithm", algorithms);
115 }
116 return algorithm;
117 }
118
119 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
120 GST_PAD_SINK,
121 GST_PAD_ALWAYS,
122 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
123 );
124
125 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
126 GST_PAD_SRC,
127 GST_PAD_ALWAYS,
128 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
129 );
130
131 G_DEFINE_TYPE_WITH_CODE (GstCVTracker, gst_cvtracker,
132 GST_TYPE_OPENCV_VIDEO_FILTER,
133 GST_DEBUG_CATEGORY_INIT (gst_cvtracker_debug, "cvtracker", 0,
134 "Performs object tracking on videos and stores it in video buffer "
135 "metadata"));
136 GST_ELEMENT_REGISTER_DEFINE (cvtracker, "cvtracker", GST_RANK_NONE,
137 GST_TYPE_OPENCV_TRACKER);
138
139 static void gst_cvtracker_set_property (GObject * object,
140 guint prop_id, const GValue * value, GParamSpec * pspec);
141 static void gst_cvtracker_get_property (GObject * object,
142 guint prop_id, GValue * value, GParamSpec * pspec);
143
144 static GstFlowReturn gst_cvtracker_transform_ip (GstOpencvVideoFilter
145 * filter, GstBuffer * buf, cv::Mat img);
146
147 static void
gst_cvtracker_finalize(GObject * obj)148 gst_cvtracker_finalize (GObject * obj)
149 {
150 GstCVTracker *filter = GST_OPENCV_TRACKER (obj);
151
152 filter->tracker.release ();
153 filter->roi.release ();
154
155 G_OBJECT_CLASS (gst_cvtracker_parent_class)->finalize (obj);
156 }
157
158 static void
gst_cvtracker_class_init(GstCVTrackerClass * klass)159 gst_cvtracker_class_init (GstCVTrackerClass * klass)
160 {
161 GObjectClass *gobject_class;
162 GstOpencvVideoFilterClass *gstopencvbasefilter_class;
163 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
164
165 gobject_class = (GObjectClass *) klass;
166 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_cvtracker_finalize);
167 gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
168
169 gstopencvbasefilter_class->cv_trans_ip_func = gst_cvtracker_transform_ip;
170
171 gobject_class->set_property = gst_cvtracker_set_property;
172 gobject_class->get_property = gst_cvtracker_get_property;
173
174 /*
175 * Tracker API in versions older than OpenCV 4.5.1 worked with a ROI based
176 * on Rect<double>. However newer versions use Rect<int>. Running the same
177 * tracker type on different versions may lead to round up errors.
178 * To avoid inconsistencies from the GStreamer side depending on the OpenCV
179 * version, use integer properties independently on the OpenCV.
180 **/
181 g_object_class_install_property (gobject_class, PROP_INITIAL_X,
182 g_param_spec_uint ("object-initial-x", "Initial X coordinate",
183 "Track object box's initial X coordinate", 0, G_MAXUINT,
184 DEFAULT_PROP_INITIAL_X,
185 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
186
187 g_object_class_install_property (gobject_class, PROP_INITIAL_Y,
188 g_param_spec_uint ("object-initial-y", "Initial Y coordinate",
189 "Track object box's initial Y coordinate", 0, G_MAXUINT,
190 DEFAULT_PROP_INITIAL_Y,
191 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
192
193 g_object_class_install_property (gobject_class, PROP_INITIAL_WIDTH,
194 g_param_spec_uint ("object-initial-width", "Object Initial Width",
195 "Track object box's initial width", 0, G_MAXUINT,
196 DEFAULT_PROP_INITIAL_WIDTH,
197 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
198
199 g_object_class_install_property (gobject_class, PROP_INITIAL_HEIGHT,
200 g_param_spec_uint ("object-initial-height", "Object Initial Height",
201 "Track object box's initial height", 0, G_MAXUINT,
202 DEFAULT_PROP_INITIAL_HEIGHT,
203 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
204
205 g_object_class_install_property (gobject_class, PROP_ALGORITHM,
206 g_param_spec_enum ("algorithm", "Algorithm",
207 "Algorithm for tracking objects", GST_OPENCV_TRACKER_ALGORITHM,
208 GST_OPENCV_TRACKER_ALGORITHM_MEDIANFLOW,
209 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
210
211 g_object_class_install_property (gobject_class, PROP_DRAW,
212 g_param_spec_boolean ("draw-rect", "Display",
213 "Draw rectangle around tracked object",
214 TRUE, (GParamFlags) G_PARAM_READWRITE));
215
216 gst_element_class_set_static_metadata (element_class,
217 "cvtracker",
218 "Filter/Effect/Video",
219 "Performs object tracking on videos and stores it in video buffer metadata.",
220 "Vivek R <123vivekr@gmail.com>");
221
222 gst_element_class_add_static_pad_template (element_class, &src_factory);
223 gst_element_class_add_static_pad_template (element_class, &sink_factory);
224
225 gst_type_mark_as_plugin_api (GST_OPENCV_TRACKER_ALGORITHM,
226 (GstPluginAPIFlags) 0);
227 }
228
229 static void
gst_cvtracker_init(GstCVTracker * filter)230 gst_cvtracker_init (GstCVTracker * filter)
231 {
232 filter->x = DEFAULT_PROP_INITIAL_X;
233 filter->y = DEFAULT_PROP_INITIAL_Y;
234 filter->width = DEFAULT_PROP_INITIAL_WIDTH;
235 filter->height = DEFAULT_PROP_INITIAL_HEIGHT;
236 #if GST_OPENCV_CHECK_VERSION(4, 5, 1)
237 filter->tracker = cv::legacy::upgradeTrackingAPI(
238 cv::legacy::TrackerMedianFlow::create());
239 #else
240 filter->tracker = cv::TrackerMedianFlow::create();
241 #endif
242 filter->draw = TRUE;
243 filter->post_debug_info = TRUE;
244
245 gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
246 TRUE);
247 filter->algorithm = GST_OPENCV_TRACKER_ALGORITHM_MEDIANFLOW;
248 }
249
250 static void
gst_cvtracker_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)251 gst_cvtracker_set_property (GObject * object, guint prop_id,
252 const GValue * value, GParamSpec * pspec)
253 {
254 GstCVTracker *filter = GST_OPENCV_TRACKER (object);
255
256 switch (prop_id) {
257 case PROP_INITIAL_X:
258 filter->x = g_value_get_uint (value);
259 break;
260 case PROP_INITIAL_Y:
261 filter->y = g_value_get_uint (value);
262 break;
263 case PROP_INITIAL_WIDTH:
264 filter->width = g_value_get_uint (value);
265 break;
266 case PROP_INITIAL_HEIGHT:
267 filter->height = g_value_get_uint (value);
268 break;
269 case PROP_ALGORITHM:
270 filter->algorithm = g_value_get_enum (value);
271 break;
272 case PROP_DRAW:
273 filter->draw = g_value_get_boolean (value);
274 break;
275 default:
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
277 break;
278 }
279 }
280
281 static void
create_cvtracker(GstCVTracker * filter)282 create_cvtracker (GstCVTracker * filter)
283 {
284 switch (filter->algorithm) {
285 case GST_OPENCV_TRACKER_ALGORITHM_BOOSTING:
286 #if GST_OPENCV_CHECK_VERSION(4, 5, 1)
287 filter->tracker = cv::legacy::upgradeTrackingAPI(
288 cv::legacy::TrackerBoosting::create());
289 #else
290 filter->tracker = cv::TrackerBoosting::create();
291 #endif
292 break;
293 case GST_OPENCV_TRACKER_ALGORITHM_CSRT:
294 filter->tracker = cv::TrackerCSRT::create ();
295 break;
296 case GST_OPENCV_TRACKER_ALGORITHM_KCF:
297 filter->tracker = cv::TrackerKCF::create ();
298 break;
299 case GST_OPENCV_TRACKER_ALGORITHM_MEDIANFLOW:
300 #if GST_OPENCV_CHECK_VERSION(4, 5, 1)
301 filter->tracker = cv::legacy::upgradeTrackingAPI(
302 cv::legacy::TrackerMedianFlow::create());
303 #else
304 filter->tracker = cv::TrackerMedianFlow::create();
305 #endif
306 break;
307 case GST_OPENCV_TRACKER_ALGORITHM_MIL:
308 filter->tracker = cv::TrackerMIL::create ();
309 break;
310 case GST_OPENCV_TRACKER_ALGORITHM_MOSSE:
311 #if GST_OPENCV_CHECK_VERSION(4, 5, 1)
312 filter->tracker = cv::legacy::upgradeTrackingAPI(
313 cv::legacy::TrackerMOSSE::create());
314 #else
315 filter->tracker = cv::TrackerMOSSE::create ();
316 #endif
317 break;
318 case GST_OPENCV_TRACKER_ALGORITHM_TLD:
319 #if GST_OPENCV_CHECK_VERSION(4, 5, 1)
320 filter->tracker = cv::legacy::upgradeTrackingAPI(
321 cv::legacy::TrackerTLD::create());
322 #else
323 filter->tracker = cv::TrackerTLD::create();
324 #endif
325 break;
326 }
327 }
328
329 static void
gst_cvtracker_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)330 gst_cvtracker_get_property (GObject * object, guint prop_id,
331 GValue * value, GParamSpec * pspec)
332 {
333 GstCVTracker *filter = GST_OPENCV_TRACKER (object);
334
335 switch (prop_id) {
336 case PROP_INITIAL_X:
337 g_value_set_uint (value, filter->x);
338 break;
339 case PROP_INITIAL_Y:
340 g_value_set_uint (value, filter->y);
341 break;
342 case PROP_INITIAL_WIDTH:
343 g_value_set_uint (value, filter->width);
344 break;
345 case PROP_INITIAL_HEIGHT:
346 g_value_set_uint (value, filter->height);
347 break;
348 case PROP_ALGORITHM:
349 g_value_set_enum (value, filter->algorithm);
350 break;
351 case PROP_DRAW:
352 g_value_set_boolean (value, filter->draw);
353 break;
354 default:
355 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
356 break;
357 }
358 }
359
360 static GstFlowReturn
gst_cvtracker_transform_ip(GstOpencvVideoFilter * base,GstBuffer * buf,cv::Mat img)361 gst_cvtracker_transform_ip (GstOpencvVideoFilter * base,
362 GstBuffer * buf, cv::Mat img)
363 {
364 GstCVTracker *filter = GST_OPENCV_TRACKER (base);
365 GstStructure *s;
366 GstMessage *msg;
367
368 if (filter->roi.empty ()) {
369 #if GST_OPENCV_CHECK_VERSION(4, 5, 1)
370 filter->roi = new (cv::Rect);
371 #else
372 filter->roi = new (cv::Rect2d);
373 #endif
374 filter->roi->x = filter->x;
375 filter->roi->y = filter->y;
376 filter->roi->width = filter->width;
377 filter->roi->height = filter->height;
378 create_cvtracker (filter);
379 filter->tracker->init (img, *filter->roi);
380 } else if (filter->tracker->update (img, *filter->roi)) {
381 #if !GST_OPENCV_CHECK_VERSION(4, 5, 1)
382 /* Round values to avoid inconsistencies depending on the OpenCV version. */
383 filter->roi->x = cvRound (filter->roi->x);
384 filter->roi->y = cvRound (filter->roi->y);
385 filter->roi->width = cvRound (filter->roi->width);
386 filter->roi->height = cvRound (filter->roi->height);
387 #endif
388 s = gst_structure_new ("object",
389 "x", G_TYPE_UINT, (guint) filter->roi->x,
390 "y", G_TYPE_UINT, (guint) filter->roi->y,
391 "width", G_TYPE_UINT, (guint) filter->roi->width,
392 "height", G_TYPE_UINT, (guint) filter->roi->height, NULL);
393 msg = gst_message_new_element (GST_OBJECT (filter), s);
394 gst_buffer_add_video_region_of_interest_meta (buf, "object",
395 filter->roi->x, filter->roi->y, filter->roi->width,
396 filter->roi->height);
397 gst_element_post_message (GST_ELEMENT (filter), msg);
398 if (filter->draw)
399 cv::rectangle (img, *filter->roi, cv::Scalar (255, 0, 0), 2, 1);
400 if (!(filter->post_debug_info))
401 filter->post_debug_info = TRUE;
402 } else if (filter->post_debug_info) {
403 GST_DEBUG_OBJECT (filter, "tracker lost");
404 filter->post_debug_info = FALSE;
405 }
406
407 return GST_FLOW_OK;
408 }
409