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