Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpDetectorFace.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 *****************************************************************************/
35 #include <visp3/core/vpConfig.h>
36 
37 #if defined(HAVE_OPENCV_OBJDETECT)
38 
39 #include <algorithm>
40 
41 #include <visp3/core/vpImageConvert.h>
42 #include <visp3/detection/vpDetectorFace.h>
43 
44 bool vpSortLargestFace(cv::Rect rect1, cv::Rect rect2) { return (rect1.area() > rect2.area()); }
45 
50 vpDetectorFace::vpDetectorFace() : m_faces(), m_face_cascade(), m_frame_gray() { }
51 
58 void vpDetectorFace::setCascadeClassifierFile(const std::string &filename)
59 {
60  if (!m_face_cascade.load(filename)) {
61  throw vpException(vpException::ioError, "Cannot read haar file: %s", filename.c_str());
62  }
63 }
64 
83 {
85 
86  return detect(m_frame_gray);
87 }
101 bool vpDetectorFace::detect(const cv::Mat &frame_gray)
102 {
103  bool detected = false;
104  m_message.clear();
105  m_polygon.clear();
106  m_nb_objects = 0;
107 
108  m_faces.clear();
109 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
110  m_face_cascade.detectMultiScale(frame_gray, m_faces, 1.1, 2, 0, cv::Size(30, 30));
111 #else
112  m_face_cascade.detectMultiScale(frame_gray, m_faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(30, 30));
113 #endif
114 
115  m_nb_objects = m_faces.size();
116 
117  std::sort(m_faces.begin(), m_faces.end(), vpSortLargestFace);
118 
119  if (m_faces.size())
120  for (size_t i = 0; i < m_faces.size(); i++) {
121  std::ostringstream message;
122  message << "Face " << i;
123  m_message.push_back(message.str());
124 
125  detected = true;
126 
127  std::vector<vpImagePoint> polygon;
128  double x = m_faces[i].tl().x;
129  double y = m_faces[i].tl().y;
130  double w = m_faces[i].size().width;
131  double h = m_faces[i].size().height;
132 
133  polygon.push_back(vpImagePoint(y, x));
134  polygon.push_back(vpImagePoint(y + h, x));
135  polygon.push_back(vpImagePoint(y + h, x + w));
136  polygon.push_back(vpImagePoint(y, x + w));
137 
138  m_polygon.push_back(polygon);
139  }
140 
141  return detected;
142 }
143 END_VISP_NAMESPACE
144 #elif !defined(VISP_BUILD_SHARED_LIBS)
145 // Work around to avoid warning: libvisp_core.a(vpDetectorFace.cpp.o) has no symbols
146 void dummy_vpDetectorFace() { };
147 #endif
std::vector< std::string > m_message
Message attached to each object.
std::vector< std::vector< vpImagePoint > > m_polygon
For each object, defines the polygon that contains the object.
size_t m_nb_objects
Number of detected objects.
cv::Mat m_frame_gray
OpenCV image used as input for the face detection.
void setCascadeClassifierFile(const std::string &filename)
bool detect(const vpImage< unsigned char > &I) VP_OVERRIDE
std::vector< cv::Rect > m_faces
Bounding box of each detected face.
cv::CascadeClassifier m_face_cascade
Haar cascade classifier file name.
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ ioError
I/O error.
Definition: vpException.h:67
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82