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 Stefan Sauer <ensonic@users.sf.net>
7 * Copyright (C) 2014 Robert Jobbagy <jobbagy.robert@gmail.com>
8 * Copyright (C) 2018 Nicola Murino <nicola.murino@gmail.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 *
28 * Alternatively, the contents of this file may be used under the
29 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
30 * which case the following provisions apply instead of the ones
31 * mentioned above:
32 *
33 * This library is free software; you can redistribute it and/or
34 * modify it under the terms of the GNU Library General Public
35 * License as published by the Free Software Foundation; either
36 * version 2 of the License, or (at your option) any later version.
37 *
38 * This library is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41 * Library General Public License for more details.
42 *
43 * You should have received a copy of the GNU Library General Public
44 * License along with this library; if not, write to the
45 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
46 * Boston, MA 02110-1301, USA.
47 */
48
49 /**
50 * SECTION:element-facedetect
51 *
52 * Performs face detection on videos and images.
53 * If you have high cpu load you need to use videoscale with capsfilter and reduce the video resolution.
54 *
55 * The image is scaled down multiple times using the GstFaceDetect::scale-factor
56 * until the size is <= GstFaceDetect::min-size-width or
57 * GstFaceDetect::min-size-height.
58 *
59 * ## Example launch line
60 *
61 * |[
62 * gst-launch-1.0 autovideosrc ! decodebin ! colorspace ! facedetect ! videoconvert ! xvimagesink
63 * ]| Detect and show faces
64 * |[
65 * gst-launch-1.0 autovideosrc ! video/x-raw,width=320,height=240 ! videoconvert ! facedetect min-size-width=60 min-size-height=60 ! colorspace ! xvimagesink
66 * ]| Detect large faces on a smaller image
67 *
68 */
69
70 /* FIXME: development version of OpenCV has CV_HAAR_FIND_BIGGEST_OBJECT which
71 * we might want to use if available
72 * see https://code.ros.org/svn/opencv/trunk/opencv/modules/objdetect/src/haar.cpp
73 */
74
75 #ifdef HAVE_CONFIG_H
76 # include <config.h>
77 #endif
78
79 #include <vector>
80
81 using namespace std;
82
83 #include "gstfacedetect.h"
84 #include <opencv2/imgproc.hpp>
85
86 GST_DEBUG_CATEGORY_STATIC (gst_face_detect_debug);
87 #define GST_CAT_DEFAULT gst_face_detect_debug
88
89 #define HAAR_CASCADES_DIR OPENCV_PREFIX G_DIR_SEPARATOR_S "share" \
90 G_DIR_SEPARATOR_S OPENCV_PATH_NAME G_DIR_SEPARATOR_S "haarcascades" \
91 G_DIR_SEPARATOR_S
92 #define DEFAULT_FACE_PROFILE HAAR_CASCADES_DIR "haarcascade_frontalface_default.xml"
93 #define DEFAULT_NOSE_PROFILE HAAR_CASCADES_DIR "haarcascade_mcs_nose.xml"
94 #define DEFAULT_MOUTH_PROFILE HAAR_CASCADES_DIR "haarcascade_mcs_mouth.xml"
95 #define DEFAULT_EYES_PROFILE HAAR_CASCADES_DIR "haarcascade_mcs_eyepair_small.xml"
96 #define DEFAULT_SCALE_FACTOR 1.25
97 #if (CV_MAJOR_VERSION >= 4)
98 #define DEFAULT_FLAGS CASCADE_DO_CANNY_PRUNING
99 #else
100 #define DEFAULT_FLAGS CV_HAAR_DO_CANNY_PRUNING
101 #endif
102 #define DEFAULT_MIN_NEIGHBORS 3
103 #define DEFAULT_MIN_SIZE_WIDTH 30
104 #define DEFAULT_MIN_SIZE_HEIGHT 30
105 #define DEFAULT_MIN_STDDEV 0
106
107 using namespace cv;
108 /* Filter signals and args */
109 enum
110 {
111 /* FILL ME */
112 LAST_SIGNAL
113 };
114
115 enum
116 {
117 PROP_0,
118 PROP_DISPLAY,
119 PROP_FACE_PROFILE,
120 PROP_NOSE_PROFILE,
121 PROP_MOUTH_PROFILE,
122 PROP_EYES_PROFILE,
123 PROP_SCALE_FACTOR,
124 PROP_MIN_NEIGHBORS,
125 PROP_FLAGS,
126 PROP_MIN_SIZE_WIDTH,
127 PROP_MIN_SIZE_HEIGHT,
128 PROP_UPDATES,
129 PROP_MIN_STDDEV
130 };
131
132
133 /*
134 * GstOpencvFaceDetectFlags:
135 *
136 * Flags parameter to OpenCV's cvHaarDetectObjects function.
137 */
138 typedef enum
139 {
140 GST_OPENCV_FACE_DETECT_HAAR_DO_CANNY_PRUNING = (1 << 0)
141 } GstOpencvFaceDetectFlags;
142
143 #define GST_TYPE_OPENCV_FACE_DETECT_FLAGS (gst_opencv_face_detect_flags_get_type())
144
145 inline void
structure_and_message(const vector<Rect> & rectangles,const gchar * name,guint rx,guint ry,GstFaceDetect * filter,GstStructure * s)146 structure_and_message (const vector < Rect > &rectangles, const gchar * name,
147 guint rx, guint ry, GstFaceDetect * filter, GstStructure * s)
148 {
149 Rect sr = rectangles[0];
150 gchar *nx = g_strconcat (name, "->x", NULL);
151 gchar *ny = g_strconcat (name, "->y", NULL);
152 gchar *nw = g_strconcat (name, "->width", NULL);
153 gchar *nh = g_strconcat (name, "->height", NULL);
154
155 GST_LOG_OBJECT (filter,
156 "%s/%" G_GSIZE_FORMAT ": x,y = %4u,%4u: w.h = %4u,%4u",
157 name, rectangles.size (), rx + sr.x, ry + sr.y, sr.width, sr.height);
158 gst_structure_set (s, nx, G_TYPE_UINT, rx + sr.x, ny, G_TYPE_UINT, ry + sr.y,
159 nw, G_TYPE_UINT, sr.width, nh, G_TYPE_UINT, sr.height, NULL);
160
161 g_free (nx);
162 g_free (ny);
163 g_free (nw);
164 g_free (nh);
165 }
166
167 static void
register_gst_opencv_face_detect_flags(GType * id)168 register_gst_opencv_face_detect_flags (GType * id)
169 {
170 static const GFlagsValue values[] = {
171 {(guint) GST_OPENCV_FACE_DETECT_HAAR_DO_CANNY_PRUNING,
172 "Do Canny edge detection to discard some regions", "do-canny-pruning"},
173 {0, NULL, NULL}
174 };
175 *id = g_flags_register_static ("GstOpencvFaceDetectFlags", values);
176 }
177
178 static GType
gst_opencv_face_detect_flags_get_type(void)179 gst_opencv_face_detect_flags_get_type (void)
180 {
181 static GType id;
182 static GOnce once = G_ONCE_INIT;
183
184 g_once (&once, (GThreadFunc) register_gst_opencv_face_detect_flags, &id);
185 return id;
186 }
187
188 #define GST_TYPE_FACE_DETECT_UPDATES (facedetect_update_get_type ())
189
190 static GType
facedetect_update_get_type(void)191 facedetect_update_get_type (void)
192 {
193 static GType facedetect_update_type = 0;
194 static const GEnumValue facedetect_update[] = {
195 {GST_FACEDETECT_UPDATES_EVERY_FRAME, "Send update messages on every frame",
196 "every_frame"},
197 {GST_FACEDETECT_UPDATES_ON_CHANGE,
198 "Send messages when a new face is detected or one is not anymore detected",
199 "on_change"},
200 {GST_FACEDETECT_UPDATES_ON_FACE,
201 "Send messages whenever a face is detected",
202 "on_face"},
203 {GST_FACEDETECT_UPDATES_NONE, "Send no messages update", "none"},
204 {0, NULL, NULL},
205 };
206
207 if (!facedetect_update_type) {
208 facedetect_update_type =
209 g_enum_register_static ("GstFaceDetectUpdates", facedetect_update);
210 }
211 return facedetect_update_type;
212 }
213
214 /* the capabilities of the inputs and outputs.
215 */
216 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
217 GST_PAD_SINK,
218 GST_PAD_ALWAYS,
219 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
220 );
221
222 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
223 GST_PAD_SRC,
224 GST_PAD_ALWAYS,
225 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
226 );
227
228 G_DEFINE_TYPE_WITH_CODE (GstFaceDetect, gst_face_detect,
229 GST_TYPE_OPENCV_VIDEO_FILTER,
230 GST_DEBUG_CATEGORY_INIT (gst_face_detect_debug, "facedetect", 0,
231 "Performs face detection on videos and images, providing detected positions via bus messages"););
232 GST_ELEMENT_REGISTER_DEFINE (facedetect, "facedetect", GST_RANK_NONE,
233 GST_TYPE_FACE_DETECT);
234
235 static void gst_face_detect_set_property (GObject * object, guint prop_id,
236 const GValue * value, GParamSpec * pspec);
237 static void gst_face_detect_get_property (GObject * object, guint prop_id,
238 GValue * value, GParamSpec * pspec);
239
240 static gboolean gst_face_detect_set_caps (GstOpencvVideoFilter * transform,
241 gint in_width, gint in_height, int in_cv_type,
242 gint out_width, gint out_height, int out_cv_type);
243 static GstFlowReturn gst_face_detect_transform_ip (GstOpencvVideoFilter * base,
244 GstBuffer * buf, Mat img);
245
246 static CascadeClassifier *gst_face_detect_load_profile (GstFaceDetect *
247 filter, gchar * profile);
248
249 /* Clean up */
250 static void
gst_face_detect_finalize(GObject * obj)251 gst_face_detect_finalize (GObject * obj)
252 {
253 GstFaceDetect *filter = GST_FACE_DETECT (obj);
254
255 filter->cvGray.release ();
256
257 g_free (filter->face_profile);
258 g_free (filter->nose_profile);
259 g_free (filter->mouth_profile);
260 g_free (filter->eyes_profile);
261
262 if (filter->cvFaceDetect)
263 delete (filter->cvFaceDetect);
264 if (filter->cvNoseDetect)
265 delete (filter->cvNoseDetect);
266 if (filter->cvMouthDetect)
267 delete (filter->cvMouthDetect);
268 if (filter->cvEyesDetect)
269 delete (filter->cvEyesDetect);
270
271 G_OBJECT_CLASS (gst_face_detect_parent_class)->finalize (obj);
272 }
273
274 /* initialize the facedetect's class */
275 static void
gst_face_detect_class_init(GstFaceDetectClass * klass)276 gst_face_detect_class_init (GstFaceDetectClass * klass)
277 {
278 GObjectClass *gobject_class;
279 GstOpencvVideoFilterClass *gstopencvbasefilter_class;
280
281 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
282 gobject_class = (GObjectClass *) klass;
283 gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
284
285 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_face_detect_finalize);
286 gobject_class->set_property = gst_face_detect_set_property;
287 gobject_class->get_property = gst_face_detect_get_property;
288
289 gstopencvbasefilter_class->cv_trans_ip_func = gst_face_detect_transform_ip;
290 gstopencvbasefilter_class->cv_set_caps = gst_face_detect_set_caps;
291
292 g_object_class_install_property (gobject_class, PROP_DISPLAY,
293 g_param_spec_boolean ("display", "Display",
294 "Sets whether the detected faces should be highlighted in the output",
295 TRUE, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
296
297 g_object_class_install_property (gobject_class, PROP_FACE_PROFILE,
298 g_param_spec_string ("profile", "Face profile",
299 "Location of Haar cascade file to use for face detection",
300 DEFAULT_FACE_PROFILE,
301 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
302 g_object_class_install_property (gobject_class, PROP_NOSE_PROFILE,
303 g_param_spec_string ("nose-profile", "Nose profile",
304 "Location of Haar cascade file to use for nose detection",
305 DEFAULT_NOSE_PROFILE,
306 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
307 g_object_class_install_property (gobject_class, PROP_MOUTH_PROFILE,
308 g_param_spec_string ("mouth-profile", "Mouth profile",
309 "Location of Haar cascade file to use for mouth detection",
310 DEFAULT_MOUTH_PROFILE,
311 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
312 g_object_class_install_property (gobject_class, PROP_EYES_PROFILE,
313 g_param_spec_string ("eyes-profile", "Eyes profile",
314 "Location of Haar cascade file to use for eye-pair detection",
315 DEFAULT_EYES_PROFILE,
316 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
317
318 g_object_class_install_property (gobject_class, PROP_FLAGS,
319 g_param_spec_flags ("flags", "Flags", "Flags to cvHaarDetectObjects",
320 GST_TYPE_OPENCV_FACE_DETECT_FLAGS, DEFAULT_FLAGS,
321 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
322 g_object_class_install_property (gobject_class, PROP_SCALE_FACTOR,
323 g_param_spec_double ("scale-factor", "Scale factor",
324 "Factor by which the frame is scaled after each object scan",
325 1.1, 10.0, DEFAULT_SCALE_FACTOR,
326 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
327 g_object_class_install_property (gobject_class, PROP_MIN_NEIGHBORS,
328 g_param_spec_int ("min-neighbors", "Minimum neighbors",
329 "Minimum number (minus 1) of neighbor rectangles that makes up "
330 "an object", 0, G_MAXINT, DEFAULT_MIN_NEIGHBORS,
331 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
332 g_object_class_install_property (gobject_class, PROP_MIN_SIZE_WIDTH,
333 g_param_spec_int ("min-size-width", "Minimum face width",
334 "Minimum area width to be recognized as a face", 0, G_MAXINT,
335 DEFAULT_MIN_SIZE_WIDTH,
336 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
337 g_object_class_install_property (gobject_class, PROP_MIN_SIZE_HEIGHT,
338 g_param_spec_int ("min-size-height", "Minimum face height",
339 "Minimum area height to be recognized as a face", 0, G_MAXINT,
340 DEFAULT_MIN_SIZE_HEIGHT,
341 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
342 g_object_class_install_property (gobject_class, PROP_UPDATES,
343 g_param_spec_enum ("updates", "Updates",
344 "When send update bus messages, if at all",
345 GST_TYPE_FACE_DETECT_UPDATES, GST_FACEDETECT_UPDATES_EVERY_FRAME,
346 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
347 g_object_class_install_property (gobject_class, PROP_MIN_STDDEV,
348 g_param_spec_int ("min-stddev", "Minimum image standard deviation",
349 "Minimum image average standard deviation: on images with standard "
350 "deviation lesser than this value facedetection will not be "
351 "performed. Setting this property help to save cpu and reduce "
352 "false positives not performing face detection on images with "
353 "little changes", 0, 255, DEFAULT_MIN_STDDEV,
354 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
355
356 gst_element_class_set_static_metadata (element_class,
357 "facedetect",
358 "Filter/Effect/Video",
359 "Performs face detection on videos and images, providing detected positions via bus messages",
360 "Michael Sheldon <mike@mikeasoft.com>");
361
362 gst_element_class_add_static_pad_template (element_class, &src_factory);
363 gst_element_class_add_static_pad_template (element_class, &sink_factory);
364
365 gst_type_mark_as_plugin_api (GST_TYPE_OPENCV_FACE_DETECT_FLAGS, (GstPluginAPIFlags) 0);
366 gst_type_mark_as_plugin_api (GST_TYPE_FACE_DETECT_UPDATES, (GstPluginAPIFlags) 0);
367 }
368
369 /* initialize the new element
370 * initialize instance structure
371 */
372 static void
gst_face_detect_init(GstFaceDetect * filter)373 gst_face_detect_init (GstFaceDetect * filter)
374 {
375 filter->face_profile = g_strdup (DEFAULT_FACE_PROFILE);
376 filter->nose_profile = g_strdup (DEFAULT_NOSE_PROFILE);
377 filter->mouth_profile = g_strdup (DEFAULT_MOUTH_PROFILE);
378 filter->eyes_profile = g_strdup (DEFAULT_EYES_PROFILE);
379 filter->display = TRUE;
380 filter->face_detected = FALSE;
381 filter->scale_factor = DEFAULT_SCALE_FACTOR;
382 filter->min_neighbors = DEFAULT_MIN_NEIGHBORS;
383 filter->flags = DEFAULT_FLAGS;
384 filter->min_size_width = DEFAULT_MIN_SIZE_WIDTH;
385 filter->min_size_height = DEFAULT_MIN_SIZE_HEIGHT;
386 filter->min_stddev = DEFAULT_MIN_STDDEV;
387 filter->cvFaceDetect =
388 gst_face_detect_load_profile (filter, filter->face_profile);
389 filter->cvNoseDetect =
390 gst_face_detect_load_profile (filter, filter->nose_profile);
391 filter->cvMouthDetect =
392 gst_face_detect_load_profile (filter, filter->mouth_profile);
393 filter->cvEyesDetect =
394 gst_face_detect_load_profile (filter, filter->eyes_profile);
395
396 gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
397 TRUE);
398 filter->updates = GST_FACEDETECT_UPDATES_EVERY_FRAME;
399 }
400
401 static void
gst_face_detect_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)402 gst_face_detect_set_property (GObject * object, guint prop_id,
403 const GValue * value, GParamSpec * pspec)
404 {
405 GstFaceDetect *filter = GST_FACE_DETECT (object);
406
407 switch (prop_id) {
408 case PROP_FACE_PROFILE:
409 g_free (filter->face_profile);
410 if (filter->cvFaceDetect)
411 delete (filter->cvFaceDetect);
412 filter->face_profile = g_value_dup_string (value);
413 filter->cvFaceDetect =
414 gst_face_detect_load_profile (filter, filter->face_profile);
415 break;
416 case PROP_NOSE_PROFILE:
417 g_free (filter->nose_profile);
418 if (filter->cvNoseDetect)
419 delete (filter->cvNoseDetect);
420 filter->nose_profile = g_value_dup_string (value);
421 filter->cvNoseDetect =
422 gst_face_detect_load_profile (filter, filter->nose_profile);
423 break;
424 case PROP_MOUTH_PROFILE:
425 g_free (filter->mouth_profile);
426 if (filter->cvMouthDetect)
427 delete (filter->cvMouthDetect);
428 filter->mouth_profile = g_value_dup_string (value);
429 filter->cvMouthDetect =
430 gst_face_detect_load_profile (filter, filter->mouth_profile);
431 break;
432 case PROP_EYES_PROFILE:
433 g_free (filter->eyes_profile);
434 if (filter->cvEyesDetect)
435 delete (filter->cvEyesDetect);
436 filter->eyes_profile = g_value_dup_string (value);
437 filter->cvEyesDetect =
438 gst_face_detect_load_profile (filter, filter->eyes_profile);
439 break;
440 case PROP_DISPLAY:
441 filter->display = g_value_get_boolean (value);
442 break;
443 case PROP_SCALE_FACTOR:
444 filter->scale_factor = g_value_get_double (value);
445 break;
446 case PROP_MIN_NEIGHBORS:
447 filter->min_neighbors = g_value_get_int (value);
448 break;
449 case PROP_MIN_SIZE_WIDTH:
450 filter->min_size_width = g_value_get_int (value);
451 break;
452 case PROP_MIN_SIZE_HEIGHT:
453 filter->min_size_height = g_value_get_int (value);
454 break;
455 case PROP_MIN_STDDEV:
456 filter->min_stddev = g_value_get_int (value);
457 break;
458 case PROP_FLAGS:
459 filter->flags = g_value_get_flags (value);
460 break;
461 case PROP_UPDATES:
462 filter->updates = g_value_get_enum (value);
463 break;
464 default:
465 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
466 break;
467 }
468 }
469
470 static void
gst_face_detect_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)471 gst_face_detect_get_property (GObject * object, guint prop_id,
472 GValue * value, GParamSpec * pspec)
473 {
474 GstFaceDetect *filter = GST_FACE_DETECT (object);
475
476 switch (prop_id) {
477 case PROP_FACE_PROFILE:
478 g_value_set_string (value, filter->face_profile);
479 break;
480 case PROP_NOSE_PROFILE:
481 g_value_set_string (value, filter->nose_profile);
482 break;
483 case PROP_MOUTH_PROFILE:
484 g_value_set_string (value, filter->mouth_profile);
485 break;
486 case PROP_EYES_PROFILE:
487 g_value_set_string (value, filter->eyes_profile);
488 break;
489 case PROP_DISPLAY:
490 g_value_set_boolean (value, filter->display);
491 break;
492 case PROP_SCALE_FACTOR:
493 g_value_set_double (value, filter->scale_factor);
494 break;
495 case PROP_MIN_NEIGHBORS:
496 g_value_set_int (value, filter->min_neighbors);
497 break;
498 case PROP_MIN_SIZE_WIDTH:
499 g_value_set_int (value, filter->min_size_width);
500 break;
501 case PROP_MIN_SIZE_HEIGHT:
502 g_value_set_int (value, filter->min_size_height);
503 break;
504 case PROP_MIN_STDDEV:
505 g_value_set_int (value, filter->min_stddev);
506 break;
507 case PROP_FLAGS:
508 g_value_set_flags (value, filter->flags);
509 break;
510 case PROP_UPDATES:
511 g_value_set_enum (value, filter->updates);
512 break;
513 default:
514 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
515 break;
516 }
517 }
518
519 /* GstElement vmethod implementations */
520
521 /* this function handles the link with other elements */
522 static gboolean
gst_face_detect_set_caps(GstOpencvVideoFilter * transform,gint in_width,gint in_height,int in_cv_type,gint out_width,gint out_height,int out_cv_type)523 gst_face_detect_set_caps (GstOpencvVideoFilter * transform, gint in_width,
524 gint in_height, int in_cv_type,
525 gint out_width, gint out_height, int out_cv_type)
526 {
527 GstFaceDetect *filter;
528
529 filter = GST_FACE_DETECT (transform);
530
531 filter->cvGray.create (Size (in_width, in_height), CV_8UC1);
532
533 return TRUE;
534 }
535
536 static GstMessage *
gst_face_detect_message_new(GstFaceDetect * filter,GstBuffer * buf)537 gst_face_detect_message_new (GstFaceDetect * filter, GstBuffer * buf)
538 {
539 GstBaseTransform *trans = GST_BASE_TRANSFORM_CAST (filter);
540 GstStructure *s;
541 GstClockTime running_time, stream_time;
542
543 running_time = gst_segment_to_running_time (&trans->segment, GST_FORMAT_TIME,
544 GST_BUFFER_TIMESTAMP (buf));
545 stream_time = gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
546 GST_BUFFER_TIMESTAMP (buf));
547
548 s = gst_structure_new ("facedetect",
549 "timestamp", G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (buf),
550 "stream-time", G_TYPE_UINT64, stream_time,
551 "running-time", G_TYPE_UINT64, running_time,
552 "duration", G_TYPE_UINT64, GST_BUFFER_DURATION (buf), NULL);
553
554 return gst_message_new_element (GST_OBJECT (filter), s);
555 }
556
557 static void
gst_face_detect_run_detector(GstFaceDetect * filter,CascadeClassifier * detector,gint min_size_width,gint min_size_height,Rect r,vector<Rect> & faces)558 gst_face_detect_run_detector (GstFaceDetect * filter,
559 CascadeClassifier * detector, gint min_size_width,
560 gint min_size_height, Rect r, vector < Rect > &faces)
561 {
562 double img_stddev = 0;
563 if (filter->min_stddev > 0) {
564 Scalar mean, stddev;
565 meanStdDev (filter->cvGray, mean, stddev);
566 img_stddev = stddev.val[0];
567 }
568 if (img_stddev >= filter->min_stddev) {
569 Mat roi (filter->cvGray, r);
570 detector->detectMultiScale (roi, faces, filter->scale_factor,
571 filter->min_neighbors, filter->flags, Size (min_size_width,
572 min_size_height), Size (0, 0));
573 } else {
574 GST_LOG_OBJECT (filter,
575 "Calculated stddev %f lesser than min_stddev %d, detection not performed",
576 img_stddev, filter->min_stddev);
577 }
578 }
579
580 /*
581 * Performs the face detection
582 */
583 static GstFlowReturn
gst_face_detect_transform_ip(GstOpencvVideoFilter * base,GstBuffer * buf,Mat img)584 gst_face_detect_transform_ip (GstOpencvVideoFilter * base, GstBuffer * buf,
585 Mat img)
586 {
587 GstFaceDetect *filter = GST_FACE_DETECT (base);
588
589 if (filter->cvFaceDetect) {
590 GstMessage *msg = NULL;
591 GstStructure *s;
592 GValue facelist = { 0 };
593 GValue facedata = { 0 };
594 vector < Rect > faces;
595 vector < Rect > mouth;
596 vector < Rect > nose;
597 vector < Rect > eyes;
598 gboolean post_msg = FALSE;
599
600 cvtColor (img, filter->cvGray, COLOR_RGB2GRAY);
601
602 gst_face_detect_run_detector (filter, filter->cvFaceDetect,
603 filter->min_size_width, filter->min_size_height,
604 Rect (0, 0,
605 filter->cvGray.size ().width, filter->cvGray.size ().height),
606 faces);
607
608 switch (filter->updates) {
609 case GST_FACEDETECT_UPDATES_EVERY_FRAME:
610 post_msg = TRUE;
611 break;
612 case GST_FACEDETECT_UPDATES_ON_CHANGE:
613 if (!faces.empty ()) {
614 if (!filter->face_detected)
615 post_msg = TRUE;
616 } else {
617 if (filter->face_detected) {
618 post_msg = TRUE;
619 }
620 }
621 break;
622 case GST_FACEDETECT_UPDATES_ON_FACE:
623 if (!faces.empty ()) {
624 post_msg = TRUE;
625 } else {
626 post_msg = FALSE;
627 }
628 break;
629 case GST_FACEDETECT_UPDATES_NONE:
630 post_msg = FALSE;
631 break;
632 default:
633 post_msg = TRUE;
634 break;
635 }
636
637 filter->face_detected = !faces.empty ()? TRUE : FALSE;
638
639 if (post_msg) {
640 msg = gst_face_detect_message_new (filter, buf);
641 g_value_init (&facelist, GST_TYPE_LIST);
642 }
643
644 for (unsigned int i = 0; i < faces.size (); ++i) {
645 Rect r = faces[i];
646 guint mw = filter->min_size_width / 8;
647 guint mh = filter->min_size_height / 8;
648 guint rnx = 0, rny = 0, rnw, rnh;
649 guint rmx = 0, rmy = 0, rmw, rmh;
650 guint rex = 0, rey = 0, rew, reh;
651 guint rhh = r.height / 2;
652 gboolean have_nose, have_mouth, have_eyes;
653
654 /* detect face features */
655
656 if (filter->cvNoseDetect) {
657 rnx = r.x + r.width / 4;
658 rny = r.y + r.height / 4;
659 rnw = r.width / 2;
660 rnh = rhh;
661 gst_face_detect_run_detector (filter, filter->cvNoseDetect, mw, mh,
662 Rect (rnx, rny, rnw, rnh), nose);
663 have_nose = !nose.empty ();
664 } else {
665 have_nose = FALSE;
666 }
667
668 if (filter->cvMouthDetect) {
669 rmx = r.x;
670 rmy = r.y + r.height / 2;
671 rmw = r.width;
672 rmh = rhh;
673 gst_face_detect_run_detector (filter, filter->cvMouthDetect, mw,
674 mh, Rect (rmx, rmy, rmw, rmh), mouth);
675 have_mouth = !mouth.empty ();
676 } else {
677 have_mouth = FALSE;
678 }
679
680 if (filter->cvEyesDetect) {
681 rex = r.x;
682 rey = r.y;
683 rew = r.width;
684 reh = rhh;
685 gst_face_detect_run_detector (filter, filter->cvEyesDetect, mw, mh,
686 Rect (rex, rey, rew, reh), eyes);
687 have_eyes = !eyes.empty ();
688 } else {
689 have_eyes = FALSE;
690 }
691
692 GST_LOG_OBJECT (filter,
693 "%2d/%2" G_GSIZE_FORMAT
694 ": x,y = %4u,%4u: w.h = %4u,%4u : features(e,n,m) = %d,%d,%d", i,
695 faces.size (), r.x, r.y, r.width, r.height, have_eyes, have_nose,
696 have_mouth);
697 if (post_msg) {
698 s = gst_structure_new ("face",
699 "x", G_TYPE_UINT, r.x,
700 "y", G_TYPE_UINT, r.y,
701 "width", G_TYPE_UINT, r.width,
702 "height", G_TYPE_UINT, r.height, NULL);
703 if (have_nose)
704 structure_and_message (nose, "nose", rnx, rny, filter, s);
705 if (have_mouth)
706 structure_and_message (mouth, "mouth", rmx, rmy, filter, s);
707 if (have_eyes)
708 structure_and_message (eyes, "eyes", rex, rey, filter, s);
709
710 g_value_init (&facedata, GST_TYPE_STRUCTURE);
711 g_value_take_boxed (&facedata, s);
712 gst_value_list_append_value (&facelist, &facedata);
713 g_value_unset (&facedata);
714 s = NULL;
715 }
716
717 if (filter->display) {
718 Point center;
719 Size axes;
720 gdouble w, h;
721 gint cb = 255 - ((i & 3) << 7);
722 gint cg = 255 - ((i & 12) << 5);
723 gint cr = 255 - ((i & 48) << 3);
724
725 w = r.width / 2;
726 h = r.height / 2;
727 center.x = cvRound ((r.x + w));
728 center.y = cvRound ((r.y + h));
729 axes.width = w;
730 axes.height = h * 1.25; /* tweak for face form */
731 ellipse (img, center, axes, 0, 0, 360, Scalar (cr, cg, cb), 3, 8, 0);
732
733 if (have_nose) {
734 Rect sr = nose[0];
735
736 w = sr.width / 2;
737 h = sr.height / 2;
738 center.x = cvRound ((rnx + sr.x + w));
739 center.y = cvRound ((rny + sr.y + h));
740 axes.width = w;
741 axes.height = h * 1.25; /* tweak for nose form */
742 ellipse (img, center, axes, 0, 0, 360, Scalar (cr, cg, cb), 1, 8, 0);
743 }
744 if (have_mouth) {
745 Rect sr = mouth[0];
746
747 w = sr.width / 2;
748 h = sr.height / 2;
749 center.x = cvRound ((rmx + sr.x + w));
750 center.y = cvRound ((rmy + sr.y + h));
751 axes.width = w * 1.5; /* tweak for mouth form */
752 axes.height = h;
753 ellipse (img, center, axes, 0, 0, 360, Scalar (cr, cg, cb), 1, 8, 0);
754 }
755 if (have_eyes) {
756 Rect sr = eyes[0];
757
758 w = sr.width / 2;
759 h = sr.height / 2;
760 center.x = cvRound ((rex + sr.x + w));
761 center.y = cvRound ((rey + sr.y + h));
762 axes.width = w * 1.5; /* tweak for eyes form */
763 axes.height = h;
764 ellipse (img, center, axes, 0, 0, 360, Scalar (cr, cg, cb), 1, 8, 0);
765 }
766 }
767 gst_buffer_add_video_region_of_interest_meta (buf, "face",
768 (guint) r.x, (guint) r.y, (guint) r.width, (guint) r.height);
769 }
770
771 if (post_msg) {
772 gst_structure_set_value ((GstStructure *) gst_message_get_structure (msg),
773 "faces", &facelist);
774 g_value_unset (&facelist);
775 gst_element_post_message (GST_ELEMENT (filter), msg);
776 }
777 }
778
779 return GST_FLOW_OK;
780 }
781
782
783 static CascadeClassifier *
gst_face_detect_load_profile(GstFaceDetect * filter,gchar * profile)784 gst_face_detect_load_profile (GstFaceDetect * filter, gchar * profile)
785 {
786 CascadeClassifier *cascade;
787
788 cascade = new CascadeClassifier (profile);
789 if (cascade->empty ()) {
790 GST_ERROR_OBJECT (filter, "Invalid profile file: %s", profile);
791 delete cascade;
792 return NULL;
793 }
794
795 return cascade;
796 }
797