Visual Servoing Platform  version 3.6.1 under development (2025-02-19)
vpDetectorFace.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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 #include <visp3/core/vpConfig.h>
34 
35 #if ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_OBJDETECT)) || ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_XOBJDETECT))
36 
37 #include <algorithm>
38 
39 #include <visp3/core/vpImageConvert.h>
40 #include <visp3/detection/vpDetectorFace.h>
41 
42 bool vpSortLargestFace(cv::Rect rect1, cv::Rect rect2) { return (rect1.area() > rect2.area()); }
43 
44 BEGIN_VISP_NAMESPACE
48  vpDetectorFace::vpDetectorFace() : m_faces(), m_face_cascade(), m_frame_gray() { }
49 
56 void vpDetectorFace::setCascadeClassifierFile(const std::string &filename)
57 {
58  if (!m_face_cascade.load(filename)) {
59  throw vpException(vpException::ioError, "Cannot read haar file: %s", filename.c_str());
60  }
61 }
62 
81 {
83 
84  return detect(m_frame_gray);
85 }
99 bool vpDetectorFace::detect(const cv::Mat &frame_gray)
100 {
101  bool detected = false;
102  m_message.clear();
103  m_polygon.clear();
104  m_nb_objects = 0;
105 
106  m_faces.clear();
107 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
108  m_face_cascade.detectMultiScale(frame_gray, m_faces, 1.1, 2, 0, cv::Size(30, 30));
109 #else
110  m_face_cascade.detectMultiScale(frame_gray, m_faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(30, 30));
111 #endif
112 
113  m_nb_objects = m_faces.size();
114 
115  std::sort(m_faces.begin(), m_faces.end(), vpSortLargestFace);
116 
117  if (m_faces.size())
118  for (size_t i = 0; i < m_faces.size(); i++) {
119  std::ostringstream message;
120  message << "Face " << i;
121  m_message.push_back(message.str());
122 
123  detected = true;
124 
125  std::vector<vpImagePoint> polygon;
126  double x = m_faces[i].tl().x;
127  double y = m_faces[i].tl().y;
128  double w = m_faces[i].size().width;
129  double h = m_faces[i].size().height;
130 
131  polygon.push_back(vpImagePoint(y, x));
132  polygon.push_back(vpImagePoint(y + h, x));
133  polygon.push_back(vpImagePoint(y + h, x + w));
134  polygon.push_back(vpImagePoint(y, x + w));
135 
136  m_polygon.push_back(polygon);
137  }
138 
139  return detected;
140 }
141 END_VISP_NAMESPACE
142 #elif !defined(VISP_BUILD_SHARED_LIBS)
143 // Work around to avoid warning: libvisp_core.a(vpDetectorFace.cpp.o) has no symbols
144 void dummy_vpDetectorFace() { };
145 #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