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>
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
25 * Alternatively, the contents of this file may be used under the
26 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
27 * which case the following provisions apply instead of the ones
30 * This library is free software; you can redistribute it and/or
31 * modify it under the terms of the GNU Library General Public
32 * License as published by the Free Software Foundation; either
33 * version 2 of the License, or (at your option) any later version.
35 * This library is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
38 * Library General Public License for more details.
40 * You should have received a copy of the GNU Library General Public
41 * License along with this library; if not, write to the
42 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
43 * Boston, MA 02111-1307, USA.
47 * SECTION:element-faceblur
49 * Blurs faces in images and videos.
52 * <title>Example launch line</title>
54 * gst-launch-0.10 videotestsrc ! decodebin ! ffmpegcolorspace ! faceblur ! ffmpegcolorspace ! xvimagesink
65 #include "gstopencvutils.h"
66 #include "gstfaceblur.h"
68 GST_DEBUG_CATEGORY_STATIC (gst_face_blur_debug);
69 #define GST_CAT_DEFAULT gst_face_blur_debug
71 #define DEFAULT_PROFILE "/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml"
73 /* Filter signals and args */
86 /* the capabilities of the inputs and outputs.
88 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
91 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
94 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
97 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
100 GST_BOILERPLATE (GstFaceBlur, gst_face_blur, GstElement, GST_TYPE_ELEMENT);
102 static void gst_face_blur_set_property (GObject * object, guint prop_id,
103 const GValue * value, GParamSpec * pspec);
104 static void gst_face_blur_get_property (GObject * object, guint prop_id,
105 GValue * value, GParamSpec * pspec);
107 static gboolean gst_face_blur_set_caps (GstPad * pad, GstCaps * caps);
108 static GstFlowReturn gst_face_blur_chain (GstPad * pad, GstBuffer * buf);
110 static void gst_face_blur_load_profile (GstFaceBlur * filter);
114 gst_face_blur_finalize (GObject * obj)
116 GstFaceBlur *filter = GST_FACE_BLUR (obj);
118 if (filter->cvImage) {
119 cvReleaseImage (&filter->cvImage);
120 cvReleaseImage (&filter->cvGray);
123 g_free (filter->profile);
125 G_OBJECT_CLASS (parent_class)->finalize (obj);
129 /* GObject vmethod implementations */
131 gst_face_blur_base_init (gpointer gclass)
133 GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
135 gst_element_class_set_details_simple (element_class,
137 "Filter/Effect/Video",
138 "Blurs faces in images and videos",
139 "Michael Sheldon <mike@mikeasoft.com>");
141 gst_element_class_add_static_pad_template (element_class, &src_factory);
142 gst_element_class_add_static_pad_template (element_class, &sink_factory);
145 /* initialize the faceblur's class */
147 gst_face_blur_class_init (GstFaceBlurClass * klass)
149 GObjectClass *gobject_class;
151 gobject_class = (GObjectClass *) klass;
152 parent_class = g_type_class_peek_parent (klass);
154 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_face_blur_finalize);
155 gobject_class->set_property = gst_face_blur_set_property;
156 gobject_class->get_property = gst_face_blur_get_property;
158 g_object_class_install_property (gobject_class, PROP_PROFILE,
159 g_param_spec_string ("profile", "Profile",
160 "Location of Haar cascade file to use for face blurion",
161 DEFAULT_PROFILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
164 /* initialize the new element
165 * instantiate pads and add them to element
166 * set pad calback functions
167 * initialize instance structure
170 gst_face_blur_init (GstFaceBlur * filter, GstFaceBlurClass * gclass)
172 filter->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
173 gst_pad_set_setcaps_function (filter->sinkpad,
174 GST_DEBUG_FUNCPTR (gst_face_blur_set_caps));
175 gst_pad_set_getcaps_function (filter->sinkpad,
176 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
177 gst_pad_set_chain_function (filter->sinkpad,
178 GST_DEBUG_FUNCPTR (gst_face_blur_chain));
180 filter->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
181 gst_pad_set_getcaps_function (filter->srcpad,
182 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
184 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
185 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
186 filter->profile = g_strdup (DEFAULT_PROFILE);
187 gst_face_blur_load_profile (filter);
191 gst_face_blur_set_property (GObject * object, guint prop_id,
192 const GValue * value, GParamSpec * pspec)
194 GstFaceBlur *filter = GST_FACE_BLUR (object);
198 g_free (filter->profile);
199 filter->profile = g_value_dup_string (value);
200 gst_face_blur_load_profile (filter);
203 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209 gst_face_blur_get_property (GObject * object, guint prop_id,
210 GValue * value, GParamSpec * pspec)
212 GstFaceBlur *filter = GST_FACE_BLUR (object);
216 g_value_set_string (value, filter->profile);
219 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224 /* GstElement vmethod implementations */
226 /* this function handles the link with other elements */
228 gst_face_blur_set_caps (GstPad * pad, GstCaps * caps)
233 GstStructure *structure;
235 filter = GST_FACE_BLUR (gst_pad_get_parent (pad));
236 structure = gst_caps_get_structure (caps, 0);
237 gst_structure_get_int (structure, "width", &width);
238 gst_structure_get_int (structure, "height", &height);
240 filter->cvImage = cvCreateImage (cvSize (width, height), IPL_DEPTH_8U, 3);
241 filter->cvGray = cvCreateImage (cvSize (width, height), IPL_DEPTH_8U, 1);
242 filter->cvStorage = cvCreateMemStorage (0);
244 otherpad = (pad == filter->srcpad) ? filter->sinkpad : filter->srcpad;
245 gst_object_unref (filter);
247 return gst_pad_set_caps (otherpad, caps);
251 * this function does the actual processing
254 gst_face_blur_chain (GstPad * pad, GstBuffer * buf)
260 filter = GST_FACE_BLUR (GST_OBJECT_PARENT (pad));
262 filter->cvImage->imageData = (char *) GST_BUFFER_DATA (buf);
264 cvCvtColor (filter->cvImage, filter->cvGray, CV_RGB2GRAY);
265 cvClearMemStorage (filter->cvStorage);
267 if (filter->cvCascade) {
269 cvHaarDetectObjects (filter->cvGray, filter->cvCascade,
270 filter->cvStorage, 1.1, 2, 0, cvSize (30, 30)
271 #if (CV_MAJOR_VERSION >= 2) && (CV_MINOR_VERSION >= 2)
276 if (faces && faces->total > 0) {
277 buf = gst_buffer_make_writable (buf);
279 for (i = 0; i < (faces ? faces->total : 0); i++) {
280 CvRect *r = (CvRect *) cvGetSeqElem (faces, i);
281 cvSetImageROI (filter->cvImage, *r);
282 cvSmooth (filter->cvImage, filter->cvImage, CV_BLUR, 11, 11, 0, 0);
283 cvSmooth (filter->cvImage, filter->cvImage, CV_GAUSSIAN, 11, 11, 0, 0);
284 cvResetImageROI (filter->cvImage);
288 /* these filters operate in place, so we push the same buffer */
290 return gst_pad_push (filter->srcpad, buf);
295 gst_face_blur_load_profile (GstFaceBlur * filter)
298 (CvHaarClassifierCascade *) cvLoad (filter->profile, 0, 0, 0);
299 if (!filter->cvCascade) {
300 GST_WARNING ("Couldn't load Haar classifier cascade: %s.", filter->profile);
305 /* entry point to initialize the plug-in
306 * initialize the plug-in itself
307 * register the element factories and other features
310 gst_face_blur_plugin_init (GstPlugin * plugin)
312 /* debug category for filtering log messages */
313 GST_DEBUG_CATEGORY_INIT (gst_face_blur_debug, "faceblur",
314 0, "Blurs faces in images and videos");
316 return gst_element_register (plugin, "faceblur", GST_RANK_NONE,