Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpDetectorFace.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 
86 {
88 
89  return detect(m_frame_gray);
90 }
102 bool vpDetectorFace::detect(const cv::Mat &frame_gray)
103 {
104  bool detected = false;
105  m_message.clear();
106  m_polygon.clear();
107  m_nb_objects = 0;
108 
109  m_faces.clear();
110 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
111  m_face_cascade.detectMultiScale( frame_gray, m_faces, 1.1, 2, 0, cv::Size(30, 30) );
112 #else
113  m_face_cascade.detectMultiScale( frame_gray, m_faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30, 30) );
114 #endif
115 
116  m_nb_objects = m_faces.size();
117 
118  std::sort(m_faces.begin(), m_faces.end(), vpSortLargestFace);
119 
120  if (m_faces.size())
121  for( size_t i = 0; i < m_faces.size(); i++ ) {
122  std::ostringstream message;
123  message << "Face " << i;
124  m_message.push_back( message.str() );
125 
126  detected = true;
127 
128  std::vector<vpImagePoint> polygon;
129  double x = m_faces[i].tl().x;
130  double y = m_faces[i].tl().y;
131  double w = m_faces[i].size().width;
132  double h = m_faces[i].size().height;
133 
134  polygon.push_back(vpImagePoint(y , x ));
135  polygon.push_back(vpImagePoint(y+h, x ));
136  polygon.push_back(vpImagePoint(y+h, x+w));
137  polygon.push_back(vpImagePoint(y , x+w));
138 
139  m_polygon.push_back(polygon);
140  }
141 
142  return detected;
143 }
144 
145 #elif !defined(VISP_BUILD_SHARED_LIBS)
146 // Work arround to avoid warning: libvisp_core.a(vpDetectorFace.cpp.o) has no symbols
147 void dummy_vpDetectorFace() {};
148 #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.