Visual Servoing Platform version 3.5.0
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) && defined(VISP_HAVE_OPENCV_OBJDETECT)
41
42#include <algorithm>
43
44#include <visp3/core/vpImageConvert.h>
45#include <visp3/detection/vpDetectorFace.h>
46
47bool vpSortLargestFace(cv::Rect rect1, cv::Rect rect2) { return (rect1.area() > rect2.area()); }
48
52vpDetectorFace::vpDetectorFace() : m_faces(), m_face_cascade(), m_frame_gray() {}
53
60void 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
84bool vpDetectorFace::detect(const vpImage<unsigned char> &I)
85{
86 vpImageConvert::convert(I, m_frame_gray);
87
88 return detect(m_frame_gray);
89}
103bool 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
149void dummy_vpDetectorFace(){};
150#endif
error that can be emited by ViSP classes.
Definition: vpException.h:72
@ ioError
I/O error.
Definition: vpException.h:91
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:88