facedetect: make updates on_change works as expected
[platform/upstream/gstreamer.git] / ext / opencv / gstfacedetect.c
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  * 
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-facedetect
49  *
50  * Performs face detection on videos and images.
51  *
52  * The image is scaled down multiple times using the GstFaceDetect::scale-factor
53  * until the size is &lt;= GstFaceDetect::min-size-width or 
54  * GstFaceDetect::min-size-height. 
55  *
56  * <refsect2>
57  * <title>Example launch line</title>
58  * |[
59  * gst-launch-0.10 autovideosrc ! decodebin2 ! colorspace ! facedetect ! videoconvert ! xvimagesink
60  * ]| Detect and show faces
61  * |[
62  * gst-launch-0.10 autovideosrc ! video/x-raw,width=320,height=240 ! videoconvert ! facedetect min-size-width=60 min-size-height=60 ! colorspace ! xvimagesink
63  * ]| Detect large faces on a smaller image 
64  *
65  * </refsect2>
66  */
67
68 /* FIXME: development version of OpenCV has CV_HAAR_FIND_BIGGEST_OBJECT which
69  * we might want to use if available
70  * see https://code.ros.org/svn/opencv/trunk/opencv/modules/objdetect/src/haar.cpp
71  */
72
73 #ifdef HAVE_CONFIG_H
74 #  include <config.h>
75 #endif
76
77 #include <gst/gst.h>
78 #include <gst/video/gstvideometa.h>
79
80 #include "gstopencvutils.h"
81 #include "gstfacedetect.h"
82
83 GST_DEBUG_CATEGORY_STATIC (gst_face_detect_debug);
84 #define GST_CAT_DEFAULT gst_face_detect_debug
85
86 #define HAAR_CASCADES_DIR OPENCV_PREFIX G_DIR_SEPARATOR_S "share" \
87     G_DIR_SEPARATOR_S "opencv" G_DIR_SEPARATOR_S "haarcascades" \
88     G_DIR_SEPARATOR_S
89 #define DEFAULT_FACE_PROFILE HAAR_CASCADES_DIR "haarcascade_frontalface_default.xml"
90 #define DEFAULT_NOSE_PROFILE HAAR_CASCADES_DIR "haarcascade_mcs_nose.xml"
91 #define DEFAULT_MOUTH_PROFILE HAAR_CASCADES_DIR "haarcascade_mcs_mouth.xml"
92 #define DEFAULT_EYES_PROFILE HAAR_CASCADES_DIR "haarcascade_mcs_eyepair_small.xml"
93 #define DEFAULT_SCALE_FACTOR 1.1
94 #define DEFAULT_FLAGS 0
95 #define DEFAULT_MIN_NEIGHBORS 3
96 #define DEFAULT_MIN_SIZE_WIDTH 0
97 #define DEFAULT_MIN_SIZE_HEIGHT 0
98
99 /* Filter signals and args */
100 enum
101 {
102   /* FILL ME */
103   LAST_SIGNAL
104 };
105
106 enum
107 {
108   PROP_0,
109   PROP_DISPLAY,
110   PROP_FACE_PROFILE,
111   PROP_NOSE_PROFILE,
112   PROP_MOUTH_PROFILE,
113   PROP_EYES_PROFILE,
114   PROP_SCALE_FACTOR,
115   PROP_MIN_NEIGHBORS,
116   PROP_FLAGS,
117   PROP_MIN_SIZE_WIDTH,
118   PROP_MIN_SIZE_HEIGHT,
119   PROP_UPDATES
120 };
121
122
123 /*
124  * GstOpencvFaceDetectFlags:
125  *
126  * Flags parameter to OpenCV's cvHaarDetectObjects function.
127  */
128 typedef enum
129 {
130   GST_OPENCV_FACE_DETECT_HAAR_DO_CANNY_PRUNING = (1 << 0)
131 } GstOpencvFaceDetectFlags;
132
133 #define GST_TYPE_OPENCV_FACE_DETECT_FLAGS (gst_opencv_face_detect_flags_get_type())
134
135 static void
136 register_gst_opencv_face_detect_flags (GType * id)
137 {
138   static const GFlagsValue values[] = {
139     {(guint) GST_OPENCV_FACE_DETECT_HAAR_DO_CANNY_PRUNING,
140         "Do Canny edge detection to discard some regions", "do-canny-pruning"},
141     {0, NULL, NULL}
142   };
143   *id = g_flags_register_static ("GstOpencvFaceDetectFlags", values);
144 }
145
146 static GType
147 gst_opencv_face_detect_flags_get_type (void)
148 {
149   static GType id;
150   static GOnce once = G_ONCE_INIT;
151
152   g_once (&once, (GThreadFunc) register_gst_opencv_face_detect_flags, &id);
153   return id;
154 }
155
156 #define GST_TYPE_FACE_DETECT_UPDATES (facedetect_update_get_type ())
157
158 static GType
159 facedetect_update_get_type (void)
160 {
161   static GType facedetect_update_type = 0;
162   static const GEnumValue facedetect_update[] = {
163     {GST_FACEDETECT_UPDATES_EVERY_FRAME, "Send update messages on every frame",
164         "every_frame"},
165     {GST_FACEDETECT_UPDATES_ON_CHANGE,
166           "Send update messages on change (face detected/not detected)",
167         "on_change"},
168     {GST_FACEDETECT_UPDATES_ON_FACE,
169           "Send update messages when a face is detected",
170         "on_face"},
171     {GST_FACEDETECT_UPDATES_NONE, "Send no messages update", "none"},
172     {0, NULL, NULL},
173   };
174
175   if (!facedetect_update_type) {
176     facedetect_update_type =
177         g_enum_register_static ("GstFaceDetectUpdates", facedetect_update);
178   }
179   return facedetect_update_type;
180 }
181
182 /* the capabilities of the inputs and outputs.
183  */
184 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
185     GST_PAD_SINK,
186     GST_PAD_ALWAYS,
187     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
188     );
189
190 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
191     GST_PAD_SRC,
192     GST_PAD_ALWAYS,
193     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
194     );
195
196 G_DEFINE_TYPE (GstFaceDetect, gst_face_detect, GST_TYPE_OPENCV_VIDEO_FILTER);
197
198 static void gst_face_detect_set_property (GObject * object, guint prop_id,
199     const GValue * value, GParamSpec * pspec);
200 static void gst_face_detect_get_property (GObject * object, guint prop_id,
201     GValue * value, GParamSpec * pspec);
202
203 static gboolean gst_face_detect_set_caps (GstOpencvVideoFilter * transform,
204     gint in_width, gint in_height, gint in_depth, gint in_channels,
205     gint out_width, gint out_height, gint out_depth, gint out_channels);
206 static GstFlowReturn gst_face_detect_transform_ip (GstOpencvVideoFilter * base,
207     GstBuffer * buf, IplImage * img);
208
209 static CvHaarClassifierCascade *gst_face_detect_load_profile (GstFaceDetect *
210     filter, gchar * profile);
211
212 /* Clean up */
213 static void
214 gst_face_detect_finalize (GObject * obj)
215 {
216   GstFaceDetect *filter = GST_FACE_DETECT (obj);
217
218   if (filter->cvGray)
219     cvReleaseImage (&filter->cvGray);
220   if (filter->cvStorage)
221     cvReleaseMemStorage (&filter->cvStorage);
222
223   g_free (filter->face_profile);
224   g_free (filter->nose_profile);
225   g_free (filter->mouth_profile);
226   g_free (filter->eyes_profile);
227
228   if (filter->cvFaceDetect)
229     cvReleaseHaarClassifierCascade (&filter->cvFaceDetect);
230   if (filter->cvNoseDetect)
231     cvReleaseHaarClassifierCascade (&filter->cvNoseDetect);
232   if (filter->cvMouthDetect)
233     cvReleaseHaarClassifierCascade (&filter->cvMouthDetect);
234   if (filter->cvEyesDetect)
235     cvReleaseHaarClassifierCascade (&filter->cvEyesDetect);
236
237   G_OBJECT_CLASS (gst_face_detect_parent_class)->finalize (obj);
238 }
239
240 /* initialize the facedetect's class */
241 static void
242 gst_face_detect_class_init (GstFaceDetectClass * klass)
243 {
244   GObjectClass *gobject_class;
245   GstOpencvVideoFilterClass *gstopencvbasefilter_class;
246
247   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
248   gobject_class = (GObjectClass *) klass;
249   gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
250
251   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_face_detect_finalize);
252   gobject_class->set_property = gst_face_detect_set_property;
253   gobject_class->get_property = gst_face_detect_get_property;
254
255   gstopencvbasefilter_class->cv_trans_ip_func = gst_face_detect_transform_ip;
256   gstopencvbasefilter_class->cv_set_caps = gst_face_detect_set_caps;
257
258   g_object_class_install_property (gobject_class, PROP_DISPLAY,
259       g_param_spec_boolean ("display", "Display",
260           "Sets whether the detected faces should be highlighted in the output",
261           TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
262
263   g_object_class_install_property (gobject_class, PROP_FACE_PROFILE,
264       g_param_spec_string ("profile", "Face profile",
265           "Location of Haar cascade file to use for face detection",
266           DEFAULT_FACE_PROFILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
267   g_object_class_install_property (gobject_class, PROP_NOSE_PROFILE,
268       g_param_spec_string ("nose-profile", "Nose profile",
269           "Location of Haar cascade file to use for nose detection",
270           DEFAULT_NOSE_PROFILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271   g_object_class_install_property (gobject_class, PROP_MOUTH_PROFILE,
272       g_param_spec_string ("mouth-profile", "Mouth profile",
273           "Location of Haar cascade file to use for mouth detection",
274           DEFAULT_MOUTH_PROFILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
275   g_object_class_install_property (gobject_class, PROP_EYES_PROFILE,
276       g_param_spec_string ("eyes-profile", "Eyes profile",
277           "Location of Haar cascade file to use for eye-pair detection",
278           DEFAULT_EYES_PROFILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
279
280   g_object_class_install_property (gobject_class, PROP_FLAGS,
281       g_param_spec_flags ("flags", "Flags", "Flags to cvHaarDetectObjects",
282           GST_TYPE_OPENCV_FACE_DETECT_FLAGS, DEFAULT_FLAGS,
283           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284   g_object_class_install_property (gobject_class, PROP_SCALE_FACTOR,
285       g_param_spec_double ("scale-factor", "Scale factor",
286           "Factor by which the frame is scaled after each object scan",
287           1.1, 10.0, DEFAULT_SCALE_FACTOR,
288           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289   g_object_class_install_property (gobject_class, PROP_MIN_NEIGHBORS,
290       g_param_spec_int ("min-neighbors", "Mininum neighbors",
291           "Minimum number (minus 1) of neighbor rectangles that makes up "
292           "an object", 0, G_MAXINT, DEFAULT_MIN_NEIGHBORS,
293           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
294   g_object_class_install_property (gobject_class, PROP_MIN_SIZE_WIDTH,
295       g_param_spec_int ("min-size-width", "Minimum face width",
296           "Minimum area width to be recognized as a face", 0, G_MAXINT,
297           DEFAULT_MIN_SIZE_WIDTH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
298   g_object_class_install_property (gobject_class, PROP_MIN_SIZE_HEIGHT,
299       g_param_spec_int ("min-size-height", "Minimum face height",
300           "Minimum area height to be recognized as a face", 0, G_MAXINT,
301           DEFAULT_MIN_SIZE_HEIGHT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
302   g_object_class_install_property (gobject_class, PROP_UPDATES,
303       g_param_spec_enum ("updates", "Updates",
304           "When send update bus messages, if at all",
305           GST_TYPE_FACE_DETECT_UPDATES, GST_FACEDETECT_UPDATES_EVERY_FRAME,
306           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307
308   gst_element_class_set_static_metadata (element_class,
309       "facedetect",
310       "Filter/Effect/Video",
311       "Performs face detection on videos and images, providing detected positions via bus messages",
312       "Michael Sheldon <mike@mikeasoft.com>");
313
314   gst_element_class_add_pad_template (element_class,
315       gst_static_pad_template_get (&src_factory));
316   gst_element_class_add_pad_template (element_class,
317       gst_static_pad_template_get (&sink_factory));
318 }
319
320 /* initialize the new element
321  * initialize instance structure
322  */
323 static void
324 gst_face_detect_init (GstFaceDetect * filter)
325 {
326   filter->face_profile = g_strdup (DEFAULT_FACE_PROFILE);
327   filter->nose_profile = g_strdup (DEFAULT_NOSE_PROFILE);
328   filter->mouth_profile = g_strdup (DEFAULT_MOUTH_PROFILE);
329   filter->eyes_profile = g_strdup (DEFAULT_EYES_PROFILE);
330   filter->display = TRUE;
331   filter->face_detected = FALSE;
332   filter->scale_factor = DEFAULT_SCALE_FACTOR;
333   filter->min_neighbors = DEFAULT_MIN_NEIGHBORS;
334   filter->flags = DEFAULT_FLAGS;
335   filter->min_size_width = DEFAULT_MIN_SIZE_WIDTH;
336   filter->min_size_height = DEFAULT_MIN_SIZE_HEIGHT;
337   filter->cvFaceDetect =
338       gst_face_detect_load_profile (filter, filter->face_profile);
339   filter->cvNoseDetect =
340       gst_face_detect_load_profile (filter, filter->nose_profile);
341   filter->cvMouthDetect =
342       gst_face_detect_load_profile (filter, filter->mouth_profile);
343   filter->cvEyesDetect =
344       gst_face_detect_load_profile (filter, filter->eyes_profile);
345
346   gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
347       TRUE);
348   filter->updates = GST_FACEDETECT_UPDATES_EVERY_FRAME;
349 }
350
351 static void
352 gst_face_detect_set_property (GObject * object, guint prop_id,
353     const GValue * value, GParamSpec * pspec)
354 {
355   GstFaceDetect *filter = GST_FACE_DETECT (object);
356
357   switch (prop_id) {
358     case PROP_FACE_PROFILE:
359       g_free (filter->face_profile);
360       if (filter->cvFaceDetect)
361         cvReleaseHaarClassifierCascade (&filter->cvFaceDetect);
362       filter->face_profile = g_value_dup_string (value);
363       filter->cvFaceDetect =
364           gst_face_detect_load_profile (filter, filter->face_profile);
365       break;
366     case PROP_NOSE_PROFILE:
367       g_free (filter->nose_profile);
368       if (filter->cvNoseDetect)
369         cvReleaseHaarClassifierCascade (&filter->cvNoseDetect);
370       filter->nose_profile = g_value_dup_string (value);
371       filter->cvNoseDetect =
372           gst_face_detect_load_profile (filter, filter->nose_profile);
373       break;
374     case PROP_MOUTH_PROFILE:
375       g_free (filter->mouth_profile);
376       if (filter->cvMouthDetect)
377         cvReleaseHaarClassifierCascade (&filter->cvMouthDetect);
378       filter->mouth_profile = g_value_dup_string (value);
379       filter->cvMouthDetect =
380           gst_face_detect_load_profile (filter, filter->mouth_profile);
381       break;
382     case PROP_EYES_PROFILE:
383       g_free (filter->eyes_profile);
384       if (filter->cvEyesDetect)
385         cvReleaseHaarClassifierCascade (&filter->cvEyesDetect);
386       filter->eyes_profile = g_value_dup_string (value);
387       filter->cvEyesDetect =
388           gst_face_detect_load_profile (filter, filter->eyes_profile);
389       break;
390     case PROP_DISPLAY:
391       filter->display = g_value_get_boolean (value);
392       break;
393     case PROP_SCALE_FACTOR:
394       filter->scale_factor = g_value_get_double (value);
395       break;
396     case PROP_MIN_NEIGHBORS:
397       filter->min_neighbors = g_value_get_int (value);
398       break;
399     case PROP_MIN_SIZE_WIDTH:
400       filter->min_size_width = g_value_get_int (value);
401       break;
402     case PROP_MIN_SIZE_HEIGHT:
403       filter->min_size_height = g_value_get_int (value);
404       break;
405     case PROP_FLAGS:
406       filter->flags = g_value_get_flags (value);
407       break;
408     case PROP_UPDATES:
409       filter->updates = g_value_get_enum (value);
410       break;
411     default:
412       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
413       break;
414   }
415 }
416
417 static void
418 gst_face_detect_get_property (GObject * object, guint prop_id,
419     GValue * value, GParamSpec * pspec)
420 {
421   GstFaceDetect *filter = GST_FACE_DETECT (object);
422
423   switch (prop_id) {
424     case PROP_FACE_PROFILE:
425       g_value_set_string (value, filter->face_profile);
426       break;
427     case PROP_NOSE_PROFILE:
428       g_value_set_string (value, filter->nose_profile);
429       break;
430     case PROP_MOUTH_PROFILE:
431       g_value_set_string (value, filter->mouth_profile);
432       break;
433     case PROP_EYES_PROFILE:
434       g_value_set_string (value, filter->eyes_profile);
435       break;
436     case PROP_DISPLAY:
437       g_value_set_boolean (value, filter->display);
438       break;
439     case PROP_SCALE_FACTOR:
440       g_value_set_double (value, filter->scale_factor);
441       break;
442     case PROP_MIN_NEIGHBORS:
443       g_value_set_int (value, filter->min_neighbors);
444       break;
445     case PROP_MIN_SIZE_WIDTH:
446       g_value_set_int (value, filter->min_size_width);
447       break;
448     case PROP_MIN_SIZE_HEIGHT:
449       g_value_set_int (value, filter->min_size_height);
450       break;
451     case PROP_FLAGS:
452       g_value_set_flags (value, filter->flags);
453       break;
454     case PROP_UPDATES:
455       g_value_set_enum (value, filter->updates);
456       break;
457     default:
458       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
459       break;
460   }
461 }
462
463 /* GstElement vmethod implementations */
464
465 /* this function handles the link with other elements */
466 static gboolean
467 gst_face_detect_set_caps (GstOpencvVideoFilter * transform, gint in_width,
468     gint in_height, gint in_depth, gint in_channels,
469     gint out_width, gint out_height, gint out_depth, gint out_channels)
470 {
471   GstFaceDetect *filter;
472
473   filter = GST_FACE_DETECT (transform);
474
475   if (filter->cvGray)
476     cvReleaseImage (&filter->cvGray);
477
478   filter->cvGray = cvCreateImage (cvSize (in_width, in_height), IPL_DEPTH_8U,
479       1);
480
481   if (!filter->cvStorage)
482     filter->cvStorage = cvCreateMemStorage (0);
483   else
484     cvClearMemStorage (filter->cvStorage);
485
486   return TRUE;
487 }
488
489 static GstMessage *
490 gst_face_detect_message_new (GstFaceDetect * filter, GstBuffer * buf)
491 {
492   GstBaseTransform *trans = GST_BASE_TRANSFORM_CAST (filter);
493   GstStructure *s;
494   GstClockTime running_time, stream_time;
495
496   running_time = gst_segment_to_running_time (&trans->segment, GST_FORMAT_TIME,
497       GST_BUFFER_TIMESTAMP (buf));
498   stream_time = gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
499       GST_BUFFER_TIMESTAMP (buf));
500
501   s = gst_structure_new ("facedetect",
502       "timestamp", G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (buf),
503       "stream-time", G_TYPE_UINT64, stream_time,
504       "running-time", G_TYPE_UINT64, running_time,
505       "duration", G_TYPE_UINT64, GST_BUFFER_DURATION (buf), NULL);
506
507   return gst_message_new_element (GST_OBJECT (filter), s);
508 }
509
510 static CvSeq *
511 gst_face_detect_run_detector (GstFaceDetect * filter,
512     CvHaarClassifierCascade * detector, gint min_size_width,
513     gint min_size_height)
514 {
515   return cvHaarDetectObjects (filter->cvGray, detector,
516       filter->cvStorage, filter->scale_factor, filter->min_neighbors,
517       filter->flags, cvSize (min_size_width, min_size_height)
518 #if (CV_MAJOR_VERSION >= 2) && (CV_MINOR_VERSION >= 2)
519       , cvSize (0, 0)
520 #endif
521       );
522 }
523
524 /* 
525  * Performs the face detection
526  */
527 static GstFlowReturn
528 gst_face_detect_transform_ip (GstOpencvVideoFilter * base, GstBuffer * buf,
529     IplImage * img)
530 {
531   GstFaceDetect *filter = GST_FACE_DETECT (base);
532
533   if (filter->cvFaceDetect) {
534     GstMessage *msg = NULL;
535     GstStructure *s;
536     GValue facelist = { 0 };
537     GValue facedata = { 0 };
538     CvSeq *faces;
539     CvSeq *mouth = NULL, *nose = NULL, *eyes = NULL;
540     gint i;
541     gboolean do_display = FALSE;
542     gboolean post_msg = FALSE;
543
544     if (filter->display) {
545       if (gst_buffer_is_writable (buf)) {
546         do_display = TRUE;
547       } else {
548         GST_LOG_OBJECT (filter, "Buffer is not writable, not drawing faces.");
549       }
550     }
551
552     cvCvtColor (img, filter->cvGray, CV_RGB2GRAY);
553     cvClearMemStorage (filter->cvStorage);
554
555     faces = gst_face_detect_run_detector (filter, filter->cvFaceDetect,
556         filter->min_size_width, filter->min_size_height);
557
558     switch (filter->updates) {
559       case GST_FACEDETECT_UPDATES_EVERY_FRAME:
560         post_msg = TRUE;
561         break;
562       case GST_FACEDETECT_UPDATES_ON_CHANGE:
563         if (faces && faces->total > 0) {
564           if (!filter->face_detected)
565             post_msg = TRUE;
566         } else {
567           if (filter->face_detected) {
568             post_msg = TRUE;
569           }
570         }
571         break;
572       case GST_FACEDETECT_UPDATES_ON_FACE:
573         if (faces && faces->total > 0) {
574           post_msg = TRUE;
575         } else {
576           post_msg = FALSE;
577         }
578         break;
579       case GST_FACEDETECT_UPDATES_NONE:
580         post_msg = FALSE;
581         break;
582       default:
583         post_msg = TRUE;
584         break;
585     }
586
587     filter->face_detected = faces ? faces->total > 0 : FALSE;
588
589     if (post_msg) {
590       msg = gst_face_detect_message_new (filter, buf);
591       g_value_init (&facelist, GST_TYPE_LIST);
592     }
593
594     for (i = 0; i < (faces ? faces->total : 0); i++) {
595       CvRect *r = (CvRect *) cvGetSeqElem (faces, i);
596       guint mw = filter->min_size_width / 8;
597       guint mh = filter->min_size_height / 8;
598       guint rnx = 0, rny = 0, rnw, rnh;
599       guint rmx = 0, rmy = 0, rmw, rmh;
600       guint rex = 0, rey = 0, rew, reh;
601       gboolean have_nose, have_mouth, have_eyes;
602
603       /* detect face features */
604
605       if (filter->cvNoseDetect) {
606         rnx = r->x + r->width / 4;
607         rny = r->y + r->height / 4;
608         rnw = r->width / 2;
609         rnh = r->height / 2;
610         cvSetImageROI (filter->cvGray, cvRect (rnx, rny, rnw, rnh));
611         nose =
612             gst_face_detect_run_detector (filter, filter->cvNoseDetect, mw, mh);
613         have_nose = (nose && nose->total);
614         cvResetImageROI (filter->cvGray);
615       } else {
616         have_nose = FALSE;
617       }
618
619       if (filter->cvMouthDetect) {
620         rmx = r->x;
621         rmy = r->y + r->height / 2;
622         rmw = r->width;
623         rmh = r->height / 2;
624         cvSetImageROI (filter->cvGray, cvRect (rmx, rmy, rmw, rmh));
625         mouth =
626             gst_face_detect_run_detector (filter, filter->cvMouthDetect, mw,
627             mh);
628         have_mouth = (mouth && mouth->total);
629         cvResetImageROI (filter->cvGray);
630       } else {
631         have_mouth = FALSE;
632       }
633
634       if (filter->cvEyesDetect) {
635         rex = r->x;
636         rey = r->y;
637         rew = r->width;
638         reh = r->height / 2;
639         cvSetImageROI (filter->cvGray, cvRect (rex, rey, rew, reh));
640         eyes =
641             gst_face_detect_run_detector (filter, filter->cvEyesDetect, mw, mh);
642         have_eyes = (eyes && eyes->total);
643         cvResetImageROI (filter->cvGray);
644       } else {
645         have_eyes = FALSE;
646       }
647
648       GST_LOG_OBJECT (filter,
649           "%2d/%2d: x,y = %4u,%4u: w.h = %4u,%4u : features(e,n,m) = %d,%d,%d",
650           i, faces->total, r->x, r->y, r->width, r->height,
651           have_eyes, have_nose, have_mouth);
652       if (post_msg) {
653         s = gst_structure_new ("face",
654             "x", G_TYPE_UINT, r->x,
655             "y", G_TYPE_UINT, r->y,
656             "width", G_TYPE_UINT, r->width,
657             "height", G_TYPE_UINT, r->height, NULL);
658         if (have_nose) {
659           CvRect *sr = (CvRect *) cvGetSeqElem (nose, 0);
660           GST_LOG_OBJECT (filter, "nose/%d: x,y = %4u,%4u: w.h = %4u,%4u",
661               nose->total, rnx + sr->x, rny + sr->y, sr->width, sr->height);
662           gst_structure_set (s,
663               "nose->x", G_TYPE_UINT, rnx + sr->x,
664               "nose->y", G_TYPE_UINT, rny + sr->y,
665               "nose->width", G_TYPE_UINT, sr->width,
666               "nose->height", G_TYPE_UINT, sr->height, NULL);
667         }
668         if (have_mouth) {
669           CvRect *sr = (CvRect *) cvGetSeqElem (mouth, 0);
670           GST_LOG_OBJECT (filter, "mouth/%d: x,y = %4u,%4u: w.h = %4u,%4u",
671               mouth->total, rmx + sr->x, rmy + sr->y, sr->width, sr->height);
672           gst_structure_set (s,
673               "mouth->x", G_TYPE_UINT, rmx + sr->x,
674               "mouth->y", G_TYPE_UINT, rmy + sr->y,
675               "mouth->width", G_TYPE_UINT, sr->width,
676               "mouth->height", G_TYPE_UINT, sr->height, NULL);
677         }
678         if (have_eyes) {
679           CvRect *sr = (CvRect *) cvGetSeqElem (eyes, 0);
680           GST_LOG_OBJECT (filter, "eyes/%d: x,y = %4u,%4u: w.h = %4u,%4u",
681               eyes->total, rex + sr->x, rey + sr->y, sr->width, sr->height);
682           gst_structure_set (s,
683               "eyes->x", G_TYPE_UINT, rex + sr->x,
684               "eyes->y", G_TYPE_UINT, rey + sr->y,
685               "eyes->width", G_TYPE_UINT, sr->width,
686               "eyes->height", G_TYPE_UINT, sr->height, NULL);
687         }
688
689         g_value_init (&facedata, GST_TYPE_STRUCTURE);
690         g_value_take_boxed (&facedata, s);
691         gst_value_list_append_value (&facelist, &facedata);
692         g_value_unset (&facedata);
693         s = NULL;
694       }
695
696       if (do_display) {
697         CvPoint center;
698         CvSize axes;
699         gdouble w, h;
700         gint cb = 255 - ((i & 3) << 7);
701         gint cg = 255 - ((i & 12) << 5);
702         gint cr = 255 - ((i & 48) << 3);
703
704         w = r->width / 2;
705         h = r->height / 2;
706         center.x = cvRound ((r->x + w));
707         center.y = cvRound ((r->y + h));
708         axes.width = w;
709         axes.height = h * 1.25; /* tweak for face form */
710         cvEllipse (img, center, axes, 0.0, 0.0, 360.0, CV_RGB (cr, cg, cb),
711             3, 8, 0);
712
713         if (have_nose) {
714           CvRect *sr = (CvRect *) cvGetSeqElem (nose, 0);
715
716           w = sr->width / 2;
717           h = sr->height / 2;
718           center.x = cvRound ((rnx + sr->x + w));
719           center.y = cvRound ((rny + sr->y + h));
720           axes.width = w;
721           axes.height = h * 1.25;       /* tweak for nose form */
722           cvEllipse (img, center, axes, 0.0, 0.0, 360.0, CV_RGB (cr, cg, cb),
723               1, 8, 0);
724         }
725         if (have_mouth) {
726           CvRect *sr = (CvRect *) cvGetSeqElem (mouth, 0);
727
728           w = sr->width / 2;
729           h = sr->height / 2;
730           center.x = cvRound ((rmx + sr->x + w));
731           center.y = cvRound ((rmy + sr->y + h));
732           axes.width = w * 1.5; /* tweak for mouth form */
733           axes.height = h;
734           cvEllipse (img, center, axes, 0.0, 0.0, 360.0, CV_RGB (cr, cg, cb),
735               1, 8, 0);
736         }
737         if (have_eyes) {
738           CvRect *sr = (CvRect *) cvGetSeqElem (eyes, 0);
739
740           w = sr->width / 2;
741           h = sr->height / 2;
742           center.x = cvRound ((rex + sr->x + w));
743           center.y = cvRound ((rey + sr->y + h));
744           axes.width = w * 1.5; /* tweak for eyes form */
745           axes.height = h;
746           cvEllipse (img, center, axes, 0.0, 0.0, 360.0, CV_RGB (cr, cg, cb),
747               1, 8, 0);
748         }
749       }
750       gst_buffer_add_video_region_of_interest_meta (buf, "face",
751           (guint) r->x, (guint) r->y, (guint) r->width, (guint) r->height);
752     }
753
754     if (post_msg) {
755       gst_structure_set_value ((GstStructure *) gst_message_get_structure (msg),
756           "faces", &facelist);
757       g_value_unset (&facelist);
758       gst_element_post_message (GST_ELEMENT (filter), msg);
759     }
760   }
761
762   return GST_FLOW_OK;
763 }
764
765
766 static CvHaarClassifierCascade *
767 gst_face_detect_load_profile (GstFaceDetect * filter, gchar * profile)
768 {
769   CvHaarClassifierCascade *cascade;
770
771   if (!(cascade = (CvHaarClassifierCascade *) cvLoad (profile, 0, 0, 0))) {
772     GST_WARNING_OBJECT (filter, "Couldn't load Haar classifier cascade: %s.",
773         profile);
774   }
775   return cascade;
776 }
777
778
779 /* entry point to initialize the plug-in
780  * initialize the plug-in itself
781  * register the element factories and other features
782  */
783 gboolean
784 gst_face_detect_plugin_init (GstPlugin * plugin)
785 {
786   /* debug category for fltering log messages */
787   GST_DEBUG_CATEGORY_INIT (gst_face_detect_debug, "facedetect",
788       0,
789       "Performs face detection on videos and images, providing detected positions via bus messages");
790
791   return gst_element_register (plugin, "facedetect", GST_RANK_NONE,
792       GST_TYPE_FACE_DETECT);
793 }