Visual Servoing Platform  version 3.0.0
vpDetectorFace.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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://visp.inria.fr 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  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Detect faces.
32  *
33  * Authors:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 #include <visp3/core/vpConfig.h>
38 
39 #if (VISP_HAVE_OPENCV_VERSION >= 0x020200)
40 
41 #include <algorithm>
42 
43 #include <visp3/detection/vpDetectorFace.h>
44 #include <visp3/core/vpImageConvert.h>
45 
46 bool vpSortLargestFace(cv::Rect rect1, cv::Rect rect2)
47 {
48  return (rect1.area() > rect2.area());
49 }
50 
55  m_faces(), m_face_cascade(), m_frame_gray()
56 {
57 }
58 
64 void vpDetectorFace::setCascadeClassifierFile(const std::string &filename)
65 {
66  if( ! m_face_cascade.load( filename ) ) {
67  throw vpException(vpException::ioError, "Cannot read haar file: %s", filename.c_str());
68  }
69 }
70 
83 {
85 
86  bool detected = false;
87  m_message.clear();
88  m_polygon.clear();
89  m_nb_objects = 0;
90 
91  m_faces.clear();
92 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
93  m_face_cascade.detectMultiScale( m_frame_gray, m_faces, 1.1, 2, 0, cv::Size(30, 30) );
94 #else
95  m_face_cascade.detectMultiScale( m_frame_gray, m_faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30, 30) );
96 #endif
97 
98  m_nb_objects = m_faces.size();
99 
100  std::sort(m_faces.begin(), m_faces.end(), vpSortLargestFace);
101 
102  if (m_faces.size())
103  for( size_t i = 0; i < m_faces.size(); i++ ) {
104  std::ostringstream message;
105  message << "Face " << i;
106  m_message.push_back( message.str() );
107 
108  detected = true;
109 
110  std::vector<vpImagePoint> polygon;
111  double x = m_faces[i].tl().x;
112  double y = m_faces[i].tl().y;
113  double w = m_faces[i].size().width;
114  double h = m_faces[i].size().height;
115 
116  polygon.push_back(vpImagePoint(y , x ));
117  polygon.push_back(vpImagePoint(y+h, x ));
118  polygon.push_back(vpImagePoint(y+h, x+w));
119  polygon.push_back(vpImagePoint(y , x+w));
120 
121  m_polygon.push_back(polygon);
122  }
123 
124  return detected;
125 }
126 
127 #elif !defined(VISP_BUILD_SHARED_LIBS)
128 // Work arround to avoid warning: libvisp_core.a(vpDetectorFace.cpp.o) has no symbols
129 void dummy_vpDetectorFace() {};
130 #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:73
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:88
std::vector< std::vector< vpImagePoint > > m_polygon
For each object, defines the polygon that contains the object.