Visual Servoing Platform  version 3.2.1 under development (2019-09-17) under development (2019-09-17)
vpDetectorFace.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
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 <visp3/core/vpConfig.h>
39 
40 #if (VISP_HAVE_OPENCV_VERSION >= 0x020200)
41 
42 #include <algorithm>
43 
44 #include <visp3/core/vpImageConvert.h>
45 #include <visp3/detection/vpDetectorFace.h>
46 
47 bool vpSortLargestFace(cv::Rect rect1, cv::Rect rect2) { return (rect1.area() > rect2.area()); }
48 
52 vpDetectorFace::vpDetectorFace() : m_faces(), m_face_cascade(), m_frame_gray() {}
53 
60 void vpDetectorFace::setCascadeClassifierFile(const std::string &filename)
61 {
62  if (!m_face_cascade.load(filename)) {
63  throw vpException(vpException::ioError, "Cannot read haar file: %s", filename.c_str());
64  }
65 }
66 
85 {
87 
88  return detect(m_frame_gray);
89 }
103 bool vpDetectorFace::detect(const cv::Mat &frame_gray)
104 {
105  bool detected = false;
106  m_message.clear();
107  m_polygon.clear();
108  m_nb_objects = 0;
109 
110  m_faces.clear();
111 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
112  m_face_cascade.detectMultiScale(frame_gray, m_faces, 1.1, 2, 0, cv::Size(30, 30));
113 #else
114  m_face_cascade.detectMultiScale(frame_gray, m_faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(30, 30));
115 #endif
116 
117  m_nb_objects = m_faces.size();
118 
119  std::sort(m_faces.begin(), m_faces.end(), vpSortLargestFace);
120 
121  if (m_faces.size())
122  for (size_t i = 0; i < m_faces.size(); i++) {
123  std::ostringstream message;
124  message << "Face " << i;
125  m_message.push_back(message.str());
126 
127  detected = true;
128 
129  std::vector<vpImagePoint> polygon;
130  double x = m_faces[i].tl().x;
131  double y = m_faces[i].tl().y;
132  double w = m_faces[i].size().width;
133  double h = m_faces[i].size().height;
134 
135  polygon.push_back(vpImagePoint(y, x));
136  polygon.push_back(vpImagePoint(y + h, x));
137  polygon.push_back(vpImagePoint(y + h, x + w));
138  polygon.push_back(vpImagePoint(y, x + w));
139 
140  m_polygon.push_back(polygon);
141  }
142 
143  return detected;
144 }
145 
146 #elif !defined(VISP_BUILD_SHARED_LIBS)
147 // Work arround to avoid warning: libvisp_core.a(vpDetectorFace.cpp.o) has no
148 // symbols
149 void dummy_vpDetectorFace(){};
150 #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:71
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::vector< vpImagePoint > > m_polygon
For each object, defines the polygon that contains the 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::string > m_message
Message attached to each object.