• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
3  * Copyright (C) 2005 Thomas Vander Stichele <thomas@apestaart.org>
4  * Copyright (C) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
5  * Copyright (C) 2008 Michael Sheldon <mike@mikeasoft.com>
6  * Copyright (C) 2011 Robert Jobbagy <jobbagy.robert@gmail.com>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  *
26  * Alternatively, the contents of this file may be used under the
27  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
28  * which case the following provisions apply instead of the ones
29  * mentioned above:
30  *
31  * This library is free software; you can redistribute it and/or
32  * modify it under the terms of the GNU Library General Public
33  * License as published by the Free Software Foundation; either
34  * version 2 of the License, or (at your option) any later version.
35  *
36  * This library is distributed in the hope that it will be useful,
37  * but WITHOUT ANY WARRANTY; without even the implied warranty of
38  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
39  * Library General Public License for more details.
40  *
41  * You should have received a copy of the GNU Library General Public
42  * License along with this library; if not, write to the
43  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
44  * Boston, MA 02110-1301, USA.
45  */
46 
47 /**
48  * SECTION:element-faceblur
49  *
50  * Blurs faces in images and videos.
51  *
52  * ## Example launch line
53  *
54  * |[
55  * gst-launch-1.0 autovideosrc ! videoconvert ! faceblur ! videoconvert ! autovideosink
56  * ]|
57  */
58 
59 #ifdef HAVE_CONFIG_H
60 #  include <config.h>
61 #endif
62 
63 #include <vector>
64 
65 #include "gstfaceblur.h"
66 #include <opencv2/imgproc.hpp>
67 
68 GST_DEBUG_CATEGORY_STATIC (gst_face_blur_debug);
69 #define GST_CAT_DEFAULT gst_face_blur_debug
70 
71 #define DEFAULT_PROFILE OPENCV_PREFIX G_DIR_SEPARATOR_S "share" \
72     G_DIR_SEPARATOR_S OPENCV_PATH_NAME G_DIR_SEPARATOR_S "haarcascades" \
73     G_DIR_SEPARATOR_S "haarcascade_frontalface_default.xml"
74 #define DEFAULT_SCALE_FACTOR 1.25
75 #if (CV_MAJOR_VERSION >= 4)
76 #define DEFAULT_FLAGS CASCADE_DO_CANNY_PRUNING
77 #else
78 #define DEFAULT_FLAGS CV_HAAR_DO_CANNY_PRUNING
79 #endif
80 #define DEFAULT_MIN_NEIGHBORS 3
81 #define DEFAULT_MIN_SIZE_WIDTH 30
82 #define DEFAULT_MIN_SIZE_HEIGHT 30
83 
84 using namespace cv;
85 using namespace std;
86 enum
87 {
88   PROP_0,
89   PROP_PROFILE,
90   PROP_SCALE_FACTOR,
91   PROP_MIN_NEIGHBORS,
92   PROP_FLAGS,
93   PROP_MIN_SIZE_WIDTH,
94   PROP_MIN_SIZE_HEIGHT
95 };
96 
97 /**
98  * GstOpencvFaceDetectFlags:
99  * @GST_CAMERABIN_FLAG_SOURCE_RESIZE: enable video crop and scale
100  *   after capture
101  *
102  * Flags parameter to OpenCV's cvHaarDetectObjects function.
103  */
104 typedef enum
105 {
106   GST_OPENCV_FACE_BLUR_HAAR_DO_CANNY_PRUNING = (1 << 0)
107 } GstOpencvFaceBlurFlags;
108 
109 #define GST_TYPE_OPENCV_FACE_BLUR_FLAGS (gst_opencv_face_blur_flags_get_type())
110 
111 static void
register_gst_opencv_face_blur_flags(GType * id)112 register_gst_opencv_face_blur_flags (GType * id)
113 {
114   static const GFlagsValue values[] = {
115     {(guint) GST_OPENCV_FACE_BLUR_HAAR_DO_CANNY_PRUNING,
116         "Do Canny edge detection to discard some regions", "do-canny-pruning"},
117     {0, NULL, NULL}
118   };
119   *id = g_flags_register_static ("GstOpencvFaceBlurFlags", values);
120 }
121 
122 static GType
gst_opencv_face_blur_flags_get_type(void)123 gst_opencv_face_blur_flags_get_type (void)
124 {
125   static GType id;
126   static GOnce once = G_ONCE_INIT;
127 
128   g_once (&once, (GThreadFunc) register_gst_opencv_face_blur_flags, &id);
129   return id;
130 }
131 
132 /* the capabilities of the inputs and outputs.
133  */
134 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
135     GST_PAD_SINK,
136     GST_PAD_ALWAYS,
137     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
138     );
139 
140 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
141     GST_PAD_SRC,
142     GST_PAD_ALWAYS,
143     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
144     );
145 
146 G_DEFINE_TYPE_WITH_CODE (GstFaceBlur, gst_face_blur,
147     GST_TYPE_OPENCV_VIDEO_FILTER, GST_DEBUG_CATEGORY_INIT (gst_face_blur_debug,
148         "faceblur", 0, "Blurs faces in images and videos"););
149 GST_ELEMENT_REGISTER_DEFINE (faceblur, "faceblur", GST_RANK_NONE,
150     GST_TYPE_FACE_BLUR);
151 
152 static void gst_face_blur_set_property (GObject * object, guint prop_id,
153     const GValue * value, GParamSpec * pspec);
154 static void gst_face_blur_get_property (GObject * object, guint prop_id,
155     GValue * value, GParamSpec * pspec);
156 
157 
158 static gboolean gst_face_blur_set_caps (GstOpencvVideoFilter * transform,
159     gint in_width, gint in_height, int in_cv_type,
160     gint out_width, gint out_height, int out_cv_type);
161 static GstFlowReturn gst_face_blur_transform_ip (GstOpencvVideoFilter *
162     transform, GstBuffer * buffer, Mat img);
163 
164 static CascadeClassifier *gst_face_blur_load_profile (GstFaceBlur *
165     filter, gchar * profile);
166 
167 /* Clean up */
168 static void
gst_face_blur_finalize(GObject * obj)169 gst_face_blur_finalize (GObject * obj)
170 {
171   GstFaceBlur *filter = GST_FACE_BLUR (obj);
172 
173   filter->cvGray.release ();
174 
175   if (filter->cvCascade)
176     delete filter->cvCascade;
177 
178   g_free (filter->profile);
179 
180   G_OBJECT_CLASS (gst_face_blur_parent_class)->finalize (obj);
181 }
182 
183 
184 /* initialize the faceblur's class */
185 static void
gst_face_blur_class_init(GstFaceBlurClass * klass)186 gst_face_blur_class_init (GstFaceBlurClass * klass)
187 {
188   GObjectClass *gobject_class;
189   GstOpencvVideoFilterClass *gstopencvbasefilter_class;
190 
191   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
192   gobject_class = (GObjectClass *) klass;
193   gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
194 
195   gstopencvbasefilter_class->cv_trans_ip_func = gst_face_blur_transform_ip;
196   gstopencvbasefilter_class->cv_set_caps = gst_face_blur_set_caps;
197 
198   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_face_blur_finalize);
199   gobject_class->set_property = gst_face_blur_set_property;
200   gobject_class->get_property = gst_face_blur_get_property;
201 
202   g_object_class_install_property (gobject_class, PROP_PROFILE,
203       g_param_spec_string ("profile", "Profile",
204           "Location of Haar cascade file to use for face blurion",
205           DEFAULT_PROFILE,
206           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
207   g_object_class_install_property (gobject_class, PROP_FLAGS,
208       g_param_spec_flags ("flags", "Flags", "Flags to cvHaarDetectObjects",
209           GST_TYPE_OPENCV_FACE_BLUR_FLAGS, DEFAULT_FLAGS,
210           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
211   g_object_class_install_property (gobject_class, PROP_SCALE_FACTOR,
212       g_param_spec_double ("scale-factor", "Scale factor",
213           "Factor by which the windows is scaled after each scan", 1.1, 10.0,
214           DEFAULT_SCALE_FACTOR,
215           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
216   g_object_class_install_property (gobject_class, PROP_MIN_NEIGHBORS,
217       g_param_spec_int ("min-neighbors", "Minimum neighbors",
218           "Minimum number (minus 1) of neighbor rectangles that makes up "
219           "an object", 0, G_MAXINT, DEFAULT_MIN_NEIGHBORS,
220           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
221   g_object_class_install_property (gobject_class, PROP_MIN_SIZE_WIDTH,
222       g_param_spec_int ("min-size-width", "Minimum size width",
223           "Minimum window width size", 0, G_MAXINT, DEFAULT_MIN_SIZE_WIDTH,
224           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
225   g_object_class_install_property (gobject_class, PROP_MIN_SIZE_HEIGHT,
226       g_param_spec_int ("min-size-height", "Minimum size height",
227           "Minimum window height size", 0, G_MAXINT, DEFAULT_MIN_SIZE_HEIGHT,
228           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
229 
230   gst_element_class_set_static_metadata (element_class,
231       "faceblur",
232       "Filter/Effect/Video",
233       "Blurs faces in images and videos",
234       "Michael Sheldon <mike@mikeasoft.com>,Robert Jobbagy <jobbagy.robert@gmail.com>");
235 
236   gst_element_class_add_static_pad_template (element_class, &src_factory);
237   gst_element_class_add_static_pad_template (element_class, &sink_factory);
238 
239   gst_type_mark_as_plugin_api (GST_TYPE_OPENCV_FACE_BLUR_FLAGS, (GstPluginAPIFlags) 0);
240 }
241 
242 /* initialize the new element
243  * instantiate pads and add them to element
244  * set pad callback functions
245  * initialize instance structure
246  */
247 static void
gst_face_blur_init(GstFaceBlur * filter)248 gst_face_blur_init (GstFaceBlur * filter)
249 {
250   filter->profile = g_strdup (DEFAULT_PROFILE);
251   filter->cvCascade = gst_face_blur_load_profile (filter, filter->profile);
252   filter->sent_profile_load_failed_msg = FALSE;
253   filter->scale_factor = DEFAULT_SCALE_FACTOR;
254   filter->min_neighbors = DEFAULT_MIN_NEIGHBORS;
255   filter->flags = DEFAULT_FLAGS;
256   filter->min_size_width = DEFAULT_MIN_SIZE_WIDTH;
257   filter->min_size_height = DEFAULT_MIN_SIZE_HEIGHT;
258 
259   gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
260       TRUE);
261 }
262 
263 static void
gst_face_blur_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)264 gst_face_blur_set_property (GObject * object, guint prop_id,
265     const GValue * value, GParamSpec * pspec)
266 {
267   GstFaceBlur *filter = GST_FACE_BLUR (object);
268 
269   switch (prop_id) {
270     case PROP_PROFILE:
271       g_free (filter->profile);
272       if (filter->cvCascade)
273         delete filter->cvCascade;
274       filter->profile = g_value_dup_string (value);
275       filter->cvCascade = gst_face_blur_load_profile (filter, filter->profile);
276       filter->sent_profile_load_failed_msg = FALSE;
277       break;
278     case PROP_SCALE_FACTOR:
279       filter->scale_factor = g_value_get_double (value);
280       break;
281     case PROP_MIN_NEIGHBORS:
282       filter->min_neighbors = g_value_get_int (value);
283       break;
284     case PROP_MIN_SIZE_WIDTH:
285       filter->min_size_width = g_value_get_int (value);
286       break;
287     case PROP_MIN_SIZE_HEIGHT:
288       filter->min_size_height = g_value_get_int (value);
289       break;
290     case PROP_FLAGS:
291       filter->flags = g_value_get_flags (value);
292       break;
293     default:
294       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295       break;
296   }
297 }
298 
299 static void
gst_face_blur_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)300 gst_face_blur_get_property (GObject * object, guint prop_id,
301     GValue * value, GParamSpec * pspec)
302 {
303   GstFaceBlur *filter = GST_FACE_BLUR (object);
304 
305   switch (prop_id) {
306     case PROP_PROFILE:
307       g_value_set_string (value, filter->profile);
308       break;
309     case PROP_SCALE_FACTOR:
310       g_value_set_double (value, filter->scale_factor);
311       break;
312     case PROP_MIN_NEIGHBORS:
313       g_value_set_int (value, filter->min_neighbors);
314       break;
315     case PROP_MIN_SIZE_WIDTH:
316       g_value_set_int (value, filter->min_size_width);
317       break;
318     case PROP_MIN_SIZE_HEIGHT:
319       g_value_set_int (value, filter->min_size_height);
320       break;
321     case PROP_FLAGS:
322       g_value_set_flags (value, filter->flags);
323       break;
324     default:
325       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
326       break;
327   }
328 }
329 
330 static gboolean
gst_face_blur_set_caps(GstOpencvVideoFilter * transform,gint in_width,gint in_height,int in_cv_type,gint out_width,gint out_height,int out_cv_type)331 gst_face_blur_set_caps (GstOpencvVideoFilter * transform,
332     gint in_width, gint in_height, int in_cv_type,
333     gint out_width, gint out_height, int out_cv_type)
334 {
335   GstFaceBlur *filter = GST_FACE_BLUR (transform);
336 
337   filter->cvGray.create (Size (in_width, in_height), CV_8UC1);
338 
339   return TRUE;
340 }
341 
342 static GstFlowReturn
gst_face_blur_transform_ip(GstOpencvVideoFilter * transform,GstBuffer * buffer,Mat img)343 gst_face_blur_transform_ip (GstOpencvVideoFilter * transform,
344     GstBuffer * buffer, Mat img)
345 {
346   GstFaceBlur *filter = GST_FACE_BLUR (transform);
347   vector < Rect > faces;
348   unsigned int i;
349 
350   if (!filter->cvCascade) {
351     if (filter->profile != NULL
352         && filter->sent_profile_load_failed_msg == FALSE) {
353       GST_ELEMENT_WARNING (filter, RESOURCE, NOT_FOUND,
354           ("Profile %s is missing.", filter->profile),
355           ("missing faceblur profile file %s", filter->profile));
356       filter->sent_profile_load_failed_msg = TRUE;
357     }
358     return GST_FLOW_OK;
359   }
360 
361   cvtColor (img, filter->cvGray, COLOR_RGB2GRAY);
362 
363   filter->cvCascade->detectMultiScale (filter->cvGray, faces,
364       filter->scale_factor, filter->min_neighbors, filter->flags,
365       Size (filter->min_size_width, filter->min_size_height), Size (0, 0));
366 
367   if (!faces.empty ()) {
368 
369     for (i = 0; i < faces.size (); ++i) {
370       Rect *r = &faces[i];
371       Mat roi (img, Rect (r->x, r->y, r->width, r->height));
372       blur (roi, roi, Size (11, 11));
373       GaussianBlur (roi, roi, Size (11, 11), 0, 0);
374     }
375   }
376 
377   return GST_FLOW_OK;
378 }
379 
380 static CascadeClassifier *
gst_face_blur_load_profile(GstFaceBlur * filter,gchar * profile)381 gst_face_blur_load_profile (GstFaceBlur * filter, gchar * profile)
382 {
383   CascadeClassifier *cascade;
384 
385   cascade = new CascadeClassifier (profile);
386   if (cascade->empty ()) {
387     GST_ERROR_OBJECT (filter, "Invalid profile file: %s", profile);
388     delete cascade;
389     return NULL;
390   }
391   return cascade;
392 }
393