Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpDetectorFace Class Reference

#include <vpDetectorFace.h>

Inheritance diagram for vpDetectorFace:

Public Member Functions

 vpDetectorFace ()
bool detect (const vpImage< unsigned char > &I) VP_OVERRIDE
bool detect (const cv::Mat &frame_gray)
void setCascadeClassifierFile (const std::string &filename)

Protected Attributes

std::vector< cv::Rect > m_faces
cv::CascadeClassifier m_face_cascade
cv::Mat m_frame_gray

Inherited functionalities from vpDetectorBase

std::vector< std::vector< vpImagePoint > > m_polygon
std::vector< std::string > m_message
size_t m_nb_objects
unsigned long m_timeout_ms
vpRect getBBox (size_t i) const
vpImagePoint getCog (size_t i) const
std::vector< std::string > & getMessage ()
std::string & getMessage (size_t i)
size_t getNbObjects () const
std::vector< std::vector< vpImagePoint > > & getPolygon ()
std::vector< vpImagePoint > & getPolygon (size_t i)
void setTimeout (unsigned long timeout_ms)

Detailed Description

The vpDetectorFace class is a wrapper over OpenCV Haar cascade face detection capabilities. To use this class ViSP should be build against OpenCV 2.2.0 or a more recent version. Installation instructions are provided here https://visp.inria.fr/3rd_opencv.

The following sample code shows how to use this class to detect the largest face in the image. The cascade classifier file "haarcascade_frontalface_alt.xml" can be found in ViSP source code or in OpenCV.

#include <visp3/detection/vpDetectorFace.h>
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
int main()
{
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_OBJDETECT)
vpDetectorFace face_detector;
face_detector.setCascadeClassifierFile("haarcascade_frontalface_alt.xml");
while(1) {
// Acquire a new image in I
bool face_found = face_detector.detect(I);
if (face_found) {
vpRect face_bbox = face_detector.getBBox(0); // largest face has index 0
}
}
#endif
}
vpRect getBBox(size_t i) const
void setCascadeClassifierFile(const std::string &filename)
bool detect(const vpImage< unsigned char > &I) VP_OVERRIDE
Definition of the vpImage class member functions.
Definition vpImage.h:131
Defines a rectangle in the plane.
Definition vpRect.h:79

Tutorials & Examples

Tutorials
If you want a more complete example that works with images acquired from a camera, you may have a look at:

Examples
tutorial-face-detector-live-threaded.cpp, tutorial-face-detector-live.cpp, and tutorial-face-detector.cpp.

Definition at line 101 of file vpDetectorFace.h.

Constructor & Destructor Documentation

◆ vpDetectorFace()

BEGIN_VISP_NAMESPACE vpDetectorFace::vpDetectorFace ( )

Default constructor.

Definition at line 52 of file vpDetectorFace.cpp.

References m_face_cascade, m_faces, and m_frame_gray.

Member Function Documentation

◆ detect() [1/2]

bool vpDetectorFace::detect ( const cv::Mat & frame_gray)

Allows to detect a face in the image. When more than one face is detected, faces are sorted from largest to smallest.

Parameters
frame_gray: Input gray level image to process.
Returns
true if one or more faces are found, false otherwise.

The number of detected faces is returned using getNbObjects(). If a face is found the functions getBBox(), getCog() return some information about the location of the face.

The largest face is always available using getBBox(0) or getCog(0).

Definition at line 103 of file vpDetectorFace.cpp.

References m_face_cascade, m_faces, vpDetectorBase::m_message, vpDetectorBase::m_nb_objects, and vpDetectorBase::m_polygon.

Referenced by visp.python.rbt.xfeat.XFeatTrackingBackend.XFeatTrackingBackend::process_frame().

◆ detect() [2/2]

bool vpDetectorFace::detect ( const vpImage< unsigned char > & I)
virtual

Allows to detect a face in the image. When more than one face is detected, faces are sorted from largest to smallest.

Parameters
I: Input image to process. This ViSP image is converted internally in an OpenCV cv::Mat image. If you original image is an gray level OpenCV image, we suggest rather the use of detect(const cv::Mat &).
Returns
true if one or more faces are found, false otherwise.

The number of detected faces is returned using getNbObjects(). If a face is found the functions getBBox(), getCog() return some information about the location of the face.

The largest face is always available using getBBox(0) or getCog(0).

See also
detect(const cv::Mat &)

Implements vpDetectorBase.

Examples
tutorial-face-detector-live-threaded.cpp, tutorial-face-detector-live.cpp, and tutorial-face-detector.cpp.

Definition at line 84 of file vpDetectorFace.cpp.

References vpImageConvert::convert(), detect(), and m_frame_gray.

Referenced by detect(), and visp.python.rbt.xfeat.XFeatTrackingBackend.XFeatTrackingBackend::process_frame().

◆ getBBox()

vpRect vpDetectorBase::getBBox ( size_t i) const
inherited

◆ getCog()

vpImagePoint vpDetectorBase::getCog ( size_t i) const
inherited

Return the center of gravity location of the ith object.

Examples
servoBiclopsPoint2DArtVelocity.cpp, servoPololuPtuPoint2DJointVelocity.cpp, and tutorial-flir-ptu-ibvs.cpp.

Definition at line 78 of file vpDetectorBase.cpp.

References m_polygon.

Referenced by detect().

◆ getMessage() [1/2]

std::vector< std::string > & vpDetectorBase::getMessage ( )
inlineinherited

Returns the contained message of the ith object if there is one.

Examples
catchAprilTag.cpp, catchArUco.cpp, tutorial-barcode-detector-live.cpp, tutorial-barcode-detector.cpp, tutorial-face-detector-live.cpp, and tutorial-face-detector.cpp.

Definition at line 98 of file vpDetectorBase.h.

References m_message.

◆ getMessage() [2/2]

std::string & vpDetectorBase::getMessage ( size_t i)
inherited

Returns the contained message of the ith object if there is one.

Definition at line 64 of file vpDetectorBase.cpp.

References vpException::badValue, m_message, and m_polygon.

◆ getNbObjects()

◆ getPolygon() [1/2]

std::vector< std::vector< vpImagePoint > > & vpDetectorBase::getPolygon ( )
inlineinherited

Returns object container box as a vector of points.

Examples
catchAprilTag.cpp, tutorial-barcode-detector-live.cpp, tutorial-barcode-detector.cpp, and tutorial-pose-from-qrcode-image.cpp.

Definition at line 113 of file vpDetectorBase.h.

References m_polygon.

◆ getPolygon() [2/2]

std::vector< vpImagePoint > & vpDetectorBase::getPolygon ( size_t i)
inherited

Returns ith object container box as a vector of points.

Definition at line 50 of file vpDetectorBase.cpp.

References vpException::badValue, and m_polygon.

◆ setCascadeClassifierFile()

void vpDetectorFace::setCascadeClassifierFile ( const std::string & filename)

Set the name of the OpenCV cascade classifier file used for face detection.

Parameters
filename: Full path to access to the file. Such a file can be found in OpenCV. Within the last versions it was name "haarcascade_frontalface_alt.xml".
Examples
tutorial-face-detector-live-threaded.cpp, tutorial-face-detector-live.cpp, and tutorial-face-detector.cpp.

Definition at line 60 of file vpDetectorFace.cpp.

References vpException::ioError, and m_face_cascade.

◆ setTimeout()

void vpDetectorBase::setTimeout ( unsigned long timeout_ms)
inlineinherited

Set detector timeout in milli-seconds. When set to 0, there is no timeout.

Definition at line 123 of file vpDetectorBase.h.

References m_timeout_ms.

Referenced by vpDetectorDataMatrixCode::vpDetectorDataMatrixCode().

Member Data Documentation

◆ m_face_cascade

cv::CascadeClassifier vpDetectorFace::m_face_cascade
protected

Haar cascade classifier file name.

Definition at line 105 of file vpDetectorFace.h.

Referenced by detect(), setCascadeClassifierFile(), and vpDetectorFace().

◆ m_faces

std::vector<cv::Rect> vpDetectorFace::m_faces
protected

Bounding box of each detected face.

Definition at line 104 of file vpDetectorFace.h.

Referenced by detect(), and vpDetectorFace().

◆ m_frame_gray

cv::Mat vpDetectorFace::m_frame_gray
protected

OpenCV image used as input for the face detection.

Definition at line 106 of file vpDetectorFace.h.

Referenced by detect(), and vpDetectorFace().

◆ m_message

std::vector<std::string> vpDetectorBase::m_message
protectedinherited

◆ m_nb_objects

size_t vpDetectorBase::m_nb_objects
protectedinherited

◆ m_polygon

std::vector<std::vector<vpImagePoint> > vpDetectorBase::m_polygon
protectedinherited

◆ m_timeout_ms

unsigned long vpDetectorBase::m_timeout_ms
protectedinherited

Detection timeout.

Definition at line 131 of file vpDetectorBase.h.

Referenced by vpDetectorDataMatrixCode::detect(), setTimeout(), vpDetectorBase(), and vpDetectorBase().