ViSP  2.10.0
vpDetectorFace.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact INRIA about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
17  *
18  * This software was developed at:
19  * INRIA Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  * http://www.irisa.fr/lagadic
24  *
25  * If you have questions regarding the use of this file, please contact
26  * INRIA at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Detect faces.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 #include <visp/vpConfig.h>
39 
40 #if (VISP_HAVE_OPENCV_VERSION >= 0x020200)
41 
42 #include <algorithm>
43 
44 #include <visp/vpDetectorFace.h>
45 #include <visp/vpImageConvert.h>
46 
47 bool vpSortLargestFace(cv::Rect rect1, cv::Rect rect2)
48 {
49  return (rect1.area() > rect2.area());
50 }
51 
56  m_faces(), m_face_cascade(), m_frame_gray()
57 {
58 }
59 
65 void vpDetectorFace::setCascadeClassifierFile(const std::string &filename)
66 {
67  if( ! m_face_cascade.load( filename ) ) {
68  throw vpException(vpException::ioError, "Cannot read haar file: %s", filename.c_str());
69  }
70 }
71 
84 {
86 
87  bool detected = false;
88  m_message.clear();
89  m_polygon.clear();
90  m_nb_objects = 0;
91 
92  m_faces.clear();
93 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
94  m_face_cascade.detectMultiScale( m_frame_gray, m_faces, 1.1, 2, 0, cv::Size(30, 30) );
95 #else
96  m_face_cascade.detectMultiScale( m_frame_gray, m_faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30, 30) );
97 #endif
98 
99  m_nb_objects = m_faces.size();
100 
101  std::sort(m_faces.begin(), m_faces.end(), vpSortLargestFace);
102 
103  if (m_faces.size())
104  for( size_t i = 0; i < m_faces.size(); i++ ) {
105  std::ostringstream message;
106  message << "Face " << i;
107  m_message.push_back( message.str() );
108 
109  detected = true;
110 
111  std::vector<vpImagePoint> polygon;
112  double x = m_faces[i].tl().x;
113  double y = m_faces[i].tl().y;
114  double w = m_faces[i].size().width;
115  double h = m_faces[i].size().height;
116 
117  polygon.push_back(vpImagePoint(y , x ));
118  polygon.push_back(vpImagePoint(y+h, x ));
119  polygon.push_back(vpImagePoint(y+h, x+w));
120  polygon.push_back(vpImagePoint(y , x+w));
121 
122  m_polygon.push_back(polygon);
123  }
124 
125  return detected;
126 }
127 
128 
129 #endif
cv::CascadeClassifier m_face_cascade
Haar cascade classifier file name.
void setCascadeClassifierFile(const std::string &filename)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
error that can be emited by ViSP classes.
Definition: vpException.h:76
size_t m_nb_objects
Number of detected objects.
cv::Mat m_frame_gray
OpenCV image used as input for the face detection.
bool detect(const vpImage< unsigned char > &I)
std::vector< cv::Rect > m_faces
Bounding box of each detected face.
std::vector< std::string > m_message
Message attached to each object.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:93
std::vector< std::vector< vpImagePoint > > m_polygon
For each object, defines the polygon that contains the object.