Commit d6c7f882 authored by Vivek R's avatar Vivek R

opencv: add cvtracker plugin

This adds an object tracker plugin with MedianFlow implementation.
parent 93f7b123
This commit is part of merge request !1321. Comments created here will be created in the context of that merge request.
/*
* GStreamer
* Copyright (C) 2020 Vivek R <123vivekr@gmail.com>
*
* 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.
*
* Alternatively, the contents of this file may be used under the
* GNU Lesser General Public License Version 2.1 (the "LGPL"), in
* which case the following provisions apply instead of the ones
* mentioned above:
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:element-cvtracker
*
* Performs object tracking on videos and stores it in video buffer metadata.
*
* ## Example launch line
*
* ```
* gst-launch-1.0 v4l2src ! videoconvert ! cvtracker box-x=50 box-y=50 box-wdith=50 box-height=50 ! videoconvert ! xvimagesink
* ```
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "gstcvtracker.h"
GST_DEBUG_CATEGORY_STATIC (gst_cvtracker_debug);
#define GST_CAT_DEFAULT gst_cvtracker_debug
#define DEFAULT_PROP_INITIAL_X 50
#define DEFAULT_PROP_INITIAL_Y 50
#define DEFAULT_PROP_INITIAL_WIDTH 50
#define DEFAULT_PROP_INITIAL_HEIGHT 50
enum
{
PROP_0,
PROP_INITIAL_X,
PROP_INITIAL_Y,
PROP_INITIAL_WIDTH,
PROP_INITIAL_HEIGHT,
};
static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
);
static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
);
G_DEFINE_TYPE (GstCVTracker, gst_cvtracker, GST_TYPE_OPENCV_VIDEO_FILTER);
static void gst_cvtracker_set_property (GObject * object,
guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_cvtracker_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec);
static GstFlowReturn gst_cvtracker_transform_ip (GstOpencvVideoFilter
* filter, GstBuffer * buf, cv::Mat img);
static void
gst_cvtracker_finalize (GObject * obj)
{
GstCVTracker *filter = GST_OPENCV_TRACKER (obj);
filter->tracker.release ();
filter->roi.release ();
G_OBJECT_CLASS (gst_cvtracker_parent_class)->finalize (obj);
}
static void
gst_cvtracker_class_init (GstCVTrackerClass * klass)
{
GObjectClass *gobject_class;
GstOpencvVideoFilterClass *gstopencvbasefilter_class;
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
gobject_class = (GObjectClass *) klass;
gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_cvtracker_finalize);
gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
gstopencvbasefilter_class->cv_trans_ip_func = gst_cvtracker_transform_ip;
gobject_class->set_property = gst_cvtracker_set_property;
gobject_class->get_property = gst_cvtracker_get_property;
g_object_class_install_property (gobject_class, PROP_INITIAL_X,
g_param_spec_double ("object-initial-x", "Initial X coordinate",
"Track object box's initial X coordinate", 0, G_MAXDOUBLE,
DEFAULT_PROP_INITIAL_X,
(GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property (gobject_class, PROP_INITIAL_Y,
g_param_spec_double ("object-initial-y", "Initial Y coordinate",
"Track object box's initial Y coordinate", 0, G_MAXDOUBLE,
DEFAULT_PROP_INITIAL_Y,
(GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property (gobject_class, PROP_INITIAL_WIDTH,
g_param_spec_double ("object-initial-width", "Object Initial Width",
"Track object box's initial width", 0, G_MAXDOUBLE,
DEFAULT_PROP_INITIAL_WIDTH,
(GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property (gobject_class, PROP_INITIAL_HEIGHT,
g_param_spec_double ("object-initial-height", "Object Initial Height",
"Track object box's initial height", 0, G_MAXDOUBLE,
DEFAULT_PROP_INITIAL_HEIGHT,
(GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
gst_element_class_set_static_metadata (element_class,
"cvtracker",
"Filter/Effect/Video",
"Performs object tracking on videos and stores it in video buffer metadata.",
"Vivek R <123vivekr@gmail.com>");
gst_element_class_add_static_pad_template (element_class, &src_factory);
gst_element_class_add_static_pad_template (element_class, &sink_factory);
}
static void
gst_cvtracker_init (GstCVTracker * filter)
{
filter->x = DEFAULT_PROP_INITIAL_X;
filter->y = DEFAULT_PROP_INITIAL_Y;
filter->width = DEFAULT_PROP_INITIAL_WIDTH;
filter->height = DEFAULT_PROP_INITIAL_HEIGHT;
filter->tracker = cv::TrackerMedianFlow::create ();
filter->post_debug_info = TRUE;
gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
TRUE);
}
static void
gst_cvtracker_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstCVTracker *filter = GST_OPENCV_TRACKER (object);
switch (prop_id) {
case PROP_INITIAL_X:
filter->x = g_value_get_double (value);
break;
case PROP_INITIAL_Y:
filter->y = g_value_get_double (value);
break;
case PROP_INITIAL_WIDTH:
filter->width = g_value_get_double (value);
break;
case PROP_INITIAL_HEIGHT:
filter->height = g_value_get_double (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_cvtracker_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstCVTracker *filter = GST_OPENCV_TRACKER (object);
switch (prop_id) {
case PROP_INITIAL_X:
g_value_set_double (value, filter->x);
break;
case PROP_INITIAL_Y:
g_value_set_double (value, filter->y);
break;
case PROP_INITIAL_WIDTH:
g_value_set_double (value, filter->width);
break;
case PROP_INITIAL_HEIGHT:
g_value_set_double (value, filter->height);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static GstFlowReturn
gst_cvtracker_transform_ip (GstOpencvVideoFilter * base,
GstBuffer * buf, cv::Mat img)
{
GstCVTracker *filter = GST_OPENCV_TRACKER (base);
GstStructure *s;
GstMessage *msg;
if (filter->roi.empty ()) {
filter->roi = new (cv::Rect2d);
filter->roi->x = filter->x;
filter->roi->y = filter->y;
filter->roi->width = filter->width;
filter->roi->height = filter->height;
filter->tracker->init (img, *filter->roi);
} else if (filter->tracker->update (img, *filter->roi)) {
s = gst_structure_new ("object",
"x", G_TYPE_DOUBLE, filter->roi->x,
"y", G_TYPE_DOUBLE, filter->roi->y,
"width", G_TYPE_DOUBLE, filter->roi->width,
"height", G_TYPE_DOUBLE, filter->roi->height, NULL);
msg = gst_message_new_element (GST_OBJECT (filter), s);
gst_buffer_add_video_region_of_interest_meta (buf, "object",
filter->roi->x, filter->roi->y, filter->roi->width,
filter->roi->height);
gst_element_post_message (GST_ELEMENT (filter), msg);
if (!(filter->post_debug_info))
filter->post_debug_info = TRUE;
} else if (filter->post_debug_info) {
GST_DEBUG_OBJECT (filter, "tracker lost");
filter->post_debug_info = FALSE;
}
return GST_FLOW_OK;
}
gboolean
gst_cvtracker_plugin_init (GstPlugin * plugin)
{
GST_DEBUG_CATEGORY_INIT (gst_cvtracker_debug, "cvtracker", 0,
"Performs object tracking on videos and stores it in video buffer metadata");
return gst_element_register (plugin, "cvtracker", GST_RANK_NONE,
GST_TYPE_OPENCV_TRACKER);
}
/*
* GStreamer
* Copyright (C) 2020 Vivek R <123vivekr@gmail.com>
*
* 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.
*
* Alternatively, the contents of this file may be used under the
* GNU Lesser General Public License Version 2.1 (the "LGPL"), in
* which case the following provisions apply instead of the ones
* mentioned above:
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_CVTRACKER_H__
#define __GST_CVTRACKER_H__
#include <gst/video/gstvideometa.h>
#include <gst/opencv/gstopencvvideofilter.h>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/tracking.hpp>
G_BEGIN_DECLS
/* #defines don't like whitespacey bits */
#define GST_TYPE_OPENCV_TRACKER \
(gst_cvtracker_get_type())
#define GST_OPENCV_TRACKER(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_OPENCV_TRACKER,GstCVTracker))
#define GST_OPENCV_TRACKER_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_OPENCV_TRACKER,GstCVTrackerClass))
#define GST_IS_OPENCV_TRACKER(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_OPENCV_TRACKER))
#define GST_IS_OPENCV_TRACKER_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_OPENCV_TRACKER))
typedef struct _GstCVTracker GstCVTracker;
typedef struct _GstCVTrackerClass GstCVTrackerClass;
struct _GstCVTracker
{
GstOpencvVideoFilter element;
gdouble x;
gdouble y;
gdouble width;
gdouble height;
gboolean post_debug_info;
cv::Ptr<cv::Tracker> tracker;
cv::Ptr<cv::Rect2d> roi;
};
struct _GstCVTrackerClass
{
GstOpencvVideoFilterClass parent_class;
};
GType gst_cvtracker_get_type (void);
gboolean gst_cvtracker_plugin_init (GstPlugin * plugin);
G_END_DECLS
#endif /* __GST_CVTRACKER_H__ */
......@@ -44,6 +44,7 @@
#include "gstdewarp.h"
#include "gstcameracalibrate.h"
#include "gstcameraundistort.h"
#include "gstcvtracker.h"
static gboolean
plugin_init (GstPlugin * plugin)
......@@ -111,6 +112,9 @@ plugin_init (GstPlugin * plugin)
if (!gst_camera_undistort_plugin_init (plugin))
return FALSE;
if (!gst_cvtracker_plugin_init (plugin))
return FALSE;
return TRUE;
}
......
......@@ -29,7 +29,8 @@ gstopencv_sources = [
'camerautils.cpp',
'cameraevent.cpp',
'gstcameracalibrate.cpp',
'gstcameraundistort.cpp'
'gstcameraundistort.cpp',
'gstcvtracker.cpp'
]
libopencv_headers = [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment