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 <core/exception.h>
25 #include <core/exceptions/software.h>
26 #include <fvclassifiers/faces.h>
27 #include <fvutils/adapters/iplimage.h>
28 #include <fvutils/color/colorspaces.h>
29 #include <fvutils/color/conversions.h>
30 #include <opencv/cv.h>
31 
32 #include <cstddef>
33 
34 namespace firevision {
35 
36 /** @class FacesClassifier <fvclassifiers/faces.h>
37  * Faces classifier.
38  * This class provides a classifier that uses OpenCV to detect images in the given
39  * image. The faces are reported back as regions of interest. Each ROI is considered
40  * to contain a face.
41  *
42  * This code is based on the OpenCV example provided and works with the Haar cascade
43  * files that come with OpenCV. The code is based on investigations by Stefan Schiffer.
44  *
45  * @author Tim Niemueller
46  */
47 
48 /** Constructor.
49  * @param haarcascade_file Haar cascade file to use
50  * @param pixel_width width of images that will be processed
51  * @param pixel_height height of images that will be processed
52  * @param image Optional image that is used by the classifier. If this image is NULL
53  * an internal IplImage is created and the buffer converted. If you need the buffer
54  * anyway pass a pointer to this image to do the conversion only once. In that case
55  * the classifier assume that the image has already been converted!
56  * @param haar_scale_factor Haar scale factor
57  * @param min_neighbours minimum neighbours
58  * @param flags flags, can only be CV_HAAR_DO_CANNY_PRUNING at the moment.
59  */
60 FacesClassifier::FacesClassifier(const char * haarcascade_file,
61  unsigned int pixel_width,
62  unsigned int pixel_height,
63  IplImage * image,
64  float haar_scale_factor,
65  int min_neighbours,
66  int flags)
67 : Classifier("FacesClassifier")
68 {
69  haar_scale_factor_ = haar_scale_factor;
70  min_neighbours_ = min_neighbours;
71  flags_ = flags;
72 
73  cascade_ = (CvHaarClassifierCascade *)cvLoad(haarcascade_file);
74  if (!cascade_) {
75  throw fawkes::Exception("Could not load Haar casca via OpenCV");
76  }
77 
78  storage_ = cvCreateMemStorage(0);
79  if (!storage_) {
80  cvReleaseHaarClassifierCascade(&cascade_);
81  throw fawkes::Exception("Could not initialize OpenCV memory");
82  }
83 
84  if (image) {
85  image_ = image;
86  own_image_ = false;
87  } else {
88  image_ = cvCreateImage(cvSize(pixel_width, pixel_height), IPL_DEPTH_8U, 3);
89  own_image_ = true;
90  }
91 }
92 
93 /** Destructor. */
95 {
96  cvReleaseHaarClassifierCascade(&cascade_);
97  cvReleaseMemStorage(&storage_);
98  if (own_image_) {
99  cvReleaseImage(&image_);
100  }
101 }
102 
103 std::list<ROI> *
105 {
106  std::list<ROI> *rv = new std::list<ROI>();
107 
108  if (own_image_) {
110  }
111 
112  CvSeq *face_seq =
113  cvHaarDetectObjects(image_, cascade_, storage_, haar_scale_factor_, min_neighbours_, flags_);
114 
115  for (int i = 0; i < face_seq->total; ++i) {
116  CvAvgComp el = *(CvAvgComp *)cvGetSeqElem(face_seq, i);
117  ROI r(el.rect.x, el.rect.y, el.rect.width, el.rect.height, _width, _height);
118  r.num_hint_points = el.rect.width * el.rect.height;
119  rv->push_back(r);
120  }
121 
122  // sort, smallest first, we define num_hint_points as area enclosed by the ROI
123  rv->sort();
124 
125  return rv;
126 }
127 
128 } // end namespace firevision
unsigned int _width
Width in pixels of _src buffer.
Definition: classifier.h:51
Region of interest.
Definition: roi.h:54
unsigned int _height
Height in pixels of _src buffer.
Definition: classifier.h:53
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:60
Base class for exceptions in Fawkes.
Definition: exception.h:35
static void convert_image_bgr(unsigned char *buffer, IplImage *image)
Convert image from buffer into IplImage.
Definition: iplimage.cpp:43
virtual ~FacesClassifier()
Destructor.
Definition: faces.cpp:94
Classifier to extract regions of interest.
Definition: classifier.h:35
unsigned int num_hint_points
Minimum estimate of points in ROI that are attributed to the ROI hint.
Definition: roi.h:135
virtual std::list< ROI > * classify()
Classify image.
Definition: faces.cpp:104
unsigned char * _src
Source buffer, encoded as YUV422_PLANAR.
Definition: classifier.h:49