Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
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
44bool vpSortLargestFace(cv::Rect rect1, cv::Rect rect2) { return (rect1.area() > rect2.area()); }
45
49vpDetectorFace::vpDetectorFace() : m_faces(), m_face_cascade(), m_frame_gray() {}
50
57void vpDetectorFace::setCascadeClassifierFile(const std::string &filename)
58{
59 if (!m_face_cascade.load(filename)) {
60 throw vpException(vpException::ioError, "Cannot read haar file: %s", filename.c_str());
61 }
62}
63
100bool vpDetectorFace::detect(const cv::Mat &frame_gray)
101{
102 bool detected = false;
103 m_message.clear();
104 m_polygon.clear();
105 m_nb_objects = 0;
106
107 m_faces.clear();
108#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
109 m_face_cascade.detectMultiScale(frame_gray, m_faces, 1.1, 2, 0, cv::Size(30, 30));
110#else
111 m_face_cascade.detectMultiScale(frame_gray, m_faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(30, 30));
112#endif
113
114 m_nb_objects = m_faces.size();
115
116 std::sort(m_faces.begin(), m_faces.end(), vpSortLargestFace);
117
118 if (m_faces.size())
119 for (size_t i = 0; i < m_faces.size(); i++) {
120 std::ostringstream message;
121 message << "Face " << i;
122 m_message.push_back(message.str());
123
124 detected = true;
125
126 std::vector<vpImagePoint> polygon;
127 double x = m_faces[i].tl().x;
128 double y = m_faces[i].tl().y;
129 double w = m_faces[i].size().width;
130 double h = m_faces[i].size().height;
131
132 polygon.push_back(vpImagePoint(y, x));
133 polygon.push_back(vpImagePoint(y + h, x));
134 polygon.push_back(vpImagePoint(y + h, x + w));
135 polygon.push_back(vpImagePoint(y, x + w));
136
137 m_polygon.push_back(polygon);
138 }
139
140 return detected;
141}
142
143#elif !defined(VISP_BUILD_SHARED_LIBS)
144// Work around to avoid warning: libvisp_core.a(vpDetectorFace.cpp.o) has no
145// symbols
146void 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.
bool detect(const vpImage< unsigned char > &I)
cv::Mat m_frame_gray
OpenCV image used as input for the face detection.
void setCascadeClassifierFile(const std::string &filename)
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:59
@ ioError
I/O error.
Definition vpException.h:79
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 of the vpImage class member functions.
Definition vpImage.h:135