Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
Loading...
Searching...
No Matches
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 defined(VISP_HAVE_OPENCV) && \
36
(((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_OBJDETECT)) || \
37
((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_XOBJDETECT)))
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);
45
46
bool
vpSortLargestFace(cv::Rect rect1, cv::Rect rect2) {
return
(rect1.area() > rect2.area()); }
47
48
BEGIN_VISP_NAMESPACE
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
84
bool
vpDetectorFace::detect
(
const
vpImage<unsigned char>
&I)
85
{
86
vpImageConvert::convert
(I,
m_frame_gray
);
87
88
return
detect
(
m_frame_gray
);
89
}
90
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
END_VISP_NAMESPACE
146
#elif !defined(VISP_BUILD_SHARED_LIBS)
147
// Work around to avoid warning: libvisp_core.a(vpDetectorFace.cpp.o) has no symbols
148
void
dummy_vpDetectorFace() { }
149
#endif
vpDetectorBase::m_message
std::vector< std::string > m_message
Message attached to each object.
Definition
vpDetectorBase.h:129
vpDetectorBase::m_polygon
std::vector< std::vector< vpImagePoint > > m_polygon
For each object, defines the polygon that contains the object.
Definition
vpDetectorBase.h:128
vpDetectorBase::m_nb_objects
size_t m_nb_objects
Number of detected objects.
Definition
vpDetectorBase.h:130
vpDetectorFace::m_frame_gray
cv::Mat m_frame_gray
OpenCV image used as input for the face detection.
Definition
vpDetectorFace.h:106
vpDetectorFace::setCascadeClassifierFile
void setCascadeClassifierFile(const std::string &filename)
Definition
vpDetectorFace.cpp:60
vpDetectorFace::vpDetectorFace
vpDetectorFace()
Definition
vpDetectorFace.cpp:52
vpDetectorFace::detect
bool detect(const vpImage< unsigned char > &I) VP_OVERRIDE
Definition
vpDetectorFace.cpp:84
vpDetectorFace::m_faces
std::vector< cv::Rect > m_faces
Bounding box of each detected face.
Definition
vpDetectorFace.h:104
vpDetectorFace::m_face_cascade
cv::CascadeClassifier m_face_cascade
Haar cascade classifier file name.
Definition
vpDetectorFace.h:105
vpException
error that can be emitted by ViSP classes.
Definition
vpException.h:60
vpException::ioError
@ ioError
I/O error.
Definition
vpException.h:67
vpImageConvert::convert
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Definition
vpImageConvert.cpp:73
vpImagePoint
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition
vpImagePoint.h:82
vpImage
Definition of the vpImage class member functions.
Definition
vpImage.h:131
BEGIN_VISP_NAMESPACE
Definition
vpMbtDistanceCircle.cpp:55
modules
detection
src
face
vpDetectorFace.cpp
Generated by
1.17.0