Fawkes API  Fawkes Development Version
faces.cpp
1 
2 /***************************************************************************
3  * faces.cpp - Faces classifier based on OpenCV
4  *
5  * Created: Mon Dec 10 15:47:11 2007
6  * Copyright 2005-2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <fvclassifiers/faces.h>
25 
26 #include <core/exception.h>
27 #include <core/exceptions/software.h>
28 #include <fvutils/color/colorspaces.h>
29 #include <fvutils/color/conversions.h>
30 #include <fvutils/adapters/iplimage.h>
31 
32 #include <cstddef>
33 #include <opencv/cv.h>
34 
35 namespace firevision {
36 #if 0 /* just to make Emacs auto-indent happy */
37 }
38 #endif
39 
40 /** @class FacesClassifier <fvclassifiers/faces.h>
41  * Faces classifier.
42  * This class provides a classifier that uses OpenCV to detect images in the given
43  * image. The faces are reported back as regions of interest. Each ROI is considered
44  * to contain a face.
45  *
46  * This code is based on the OpenCV example provided and works with the Haar cascade
47  * files that come with OpenCV. The code is based on investigations by Stefan Schiffer.
48  *
49  * @author Tim Niemueller
50  */
51 
52 /** Constructor.
53  * @param haarcascade_file Haar cascade file to use
54  * @param pixel_width width of images that will be processed
55  * @param pixel_height height of images that will be processed
56  * @param image Optional image that is used by the classifier. If this image is NULL
57  * an internal IplImage is created and the buffer converted. If you need the buffer
58  * anyway pass a pointer to this image to do the conversion only once. In that case
59  * the classifier assume that the image has already been converted!
60  * @param haar_scale_factor Haar scale factor
61  * @param min_neighbours minimum neighbours
62  * @param flags flags, can only be CV_HAAR_DO_CANNY_PRUNING at the moment.
63  */
64 FacesClassifier::FacesClassifier(const char *haarcascade_file,
65  unsigned int pixel_width, unsigned int pixel_height,
66  IplImage *image,
67  float haar_scale_factor, int min_neighbours, int flags)
68  : Classifier("FacesClassifier")
69 {
70  __haar_scale_factor = haar_scale_factor;
71  __min_neighbours = min_neighbours;
72  __flags = flags;
73 
74  __cascade = (CvHaarClassifierCascade *) cvLoad(haarcascade_file);
75  if ( ! __cascade ) {
76  throw fawkes::Exception("Could not load Haar casca via OpenCV");
77  }
78 
79  __storage = cvCreateMemStorage(0);
80  if ( ! __storage ) {
81  cvReleaseHaarClassifierCascade(&__cascade);
82  throw fawkes::Exception("Could not initialize OpenCV memory");
83  }
84 
85  if ( image ) {
86  __image = image;
87  __own_image = false;
88  } else {
89  __image = cvCreateImage(cvSize(pixel_width, pixel_height), IPL_DEPTH_8U, 3);
90  __own_image = true;
91  }
92 }
93 
94 
95 /** Destructor. */
97 {
98  cvReleaseHaarClassifierCascade(&__cascade);
99  cvReleaseMemStorage(&__storage);
100  if ( __own_image ) {
101  cvReleaseImage(&__image);
102  }
103 }
104 
105 
106 std::list< ROI > *
108 {
109  std::list< ROI > *rv = new std::list< ROI >();
110 
111  if ( __own_image ) {
113  }
114 
115  CvSeq *face_seq = cvHaarDetectObjects(__image, __cascade, __storage,
116  __haar_scale_factor, __min_neighbours, __flags);
117 
118  for ( int i = 0; i < face_seq->total; ++i) {
119  CvAvgComp el = *(CvAvgComp*)cvGetSeqElem(face_seq, i);
120  ROI r(el.rect.x, el.rect.y, el.rect.width, el.rect.height, _width, _height);
121  r.num_hint_points = el.rect.width * el.rect.height;
122  rv->push_back(r);
123  }
124 
125  // sort, smallest first, we define num_hint_points as area enclosed by the ROI
126  rv->sort();
127 
128  return rv;
129 }
130 
131 } // end namespace firevision
unsigned int _width
Width in pixels of _src buffer.
Definition: classifier.h:54
Region of interest.
Definition: roi.h:58
unsigned int _height
Height in pixels of _src buffer.
Definition: classifier.h:56
FacesClassifier(const char *haarcascade_file, unsigned int pixel_width, unsigned int pixel_height, IplImage *image=0, float haar_scale_factor=1.1, int min_neighbours=3, int flags=0)
Constructor.
Definition: faces.cpp:64
Base class for exceptions in Fawkes.
Definition: exception.h:36
static void convert_image_bgr(unsigned char *buffer, IplImage *image)
Convert image from buffer into IplImage.
Definition: iplimage.cpp:46
virtual ~FacesClassifier()
Destructor.
Definition: faces.cpp:96
Classifier to extract regions of interest.
Definition: classifier.h:37
unsigned int num_hint_points
Minimum estimate of points in ROI that are attributed to the ROI hint.
Definition: roi.h:139
virtual std::list< ROI > * classify()
Classify image.
Definition: faces.cpp:107
unsigned char * _src
Source buffer, encoded as YUV422_PLANAR.
Definition: classifier.h:52