Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-face-detector.cpp
1
2#include <visp3/core/vpConfig.h>
3#include <visp3/gui/vpDisplayFactory.h>
5#include <visp3/detection/vpDetectorFace.h>
7#include <visp3/io/vpVideoReader.h>
8
9int main(int argc, const char *argv[])
10{
12#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && \
13 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_OBJDETECT)) || \
14 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_XOBJDETECT)))
15#ifdef ENABLE_VISP_NAMESPACE
16 using namespace VISP_NAMESPACE_NAME;
17#endif
19#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
20 std::shared_ptr<vpDisplay> display;
21#else
22 vpDisplay *display = nullptr;
23#endif
24 try {
26 std::string opt_face_cascade_name = "./haarcascade_frontalface_alt.xml";
27 std::string opt_video = "video.mp4";
29
30 for (int i = 1; i < argc; i++) {
31 if (std::string(argv[i]) == "--haar" && i + 1 < argc) {
32 opt_face_cascade_name = std::string(argv[++i]);
33 }
34 else if (std::string(argv[i]) == "--video" && i + 1 < argc) {
35 opt_video = std::string(argv[++i]);
36 }
37 else if ((std::string(argv[i]) == "--help") || (std::string(argv[i]) == "-h")) {
38 std::cout << "Usage: " << argv[0]
39 << " [--haar <haarcascade xml filename>]"
40 << " [--video <input video file>]"
41 << " [--help] [-h]" << std::endl;
42 return EXIT_SUCCESS;
43 }
44 }
45
47
49 g.setFileName(opt_video);
50 g.open(I);
51
52#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
54#else
56#endif
57 vpDisplay::setTitle(I, "ViSP viewer");
58
60 vpDetectorFace face_detector;
63 face_detector.setCascadeClassifierFile(opt_face_cascade_name);
65
66 bool exit_requested = false;
67 while (!g.end() && !exit_requested) {
68 g.acquire(I);
69
72 bool face_found = face_detector.detect(I);
74
75 if (face_found) {
76 std::ostringstream text;
78 text << "Found " << face_detector.getNbObjects() << " face(s)";
80 vpDisplay::displayText(I, 10, 10, text.str(), vpColor::red);
82 for (size_t i = 0; i < face_detector.getNbObjects(); i++) {
83 vpRect bbox = face_detector.getBBox(i);
85 vpDisplay::displayText(I, static_cast<int>(bbox.getTop()) - 10, static_cast<int>(bbox.getLeft()),
86 "Message: \"" + face_detector.getMessage(i) + "\"", vpColor::red);
87 }
89 }
90 vpDisplay::displayText(I, static_cast<int>(I.getHeight()) - 25, 10, "Click to quit...", vpColor::red);
92 if (vpDisplay::getClick(I, false)) // a click to exit
93 exit_requested = true;
94 }
95 if (!exit_requested)
97 }
98 catch (const vpException &e) {
99 std::cout << e.getMessage() << std::endl;
100 }
101#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
102 if (display != nullptr) {
103 delete display;
104 }
105#endif
106#else
107
108#if !defined(HAVE_OPENCV_HIGHGUI)
109 std::cout << "This tutorial needs OpenCV highgui module that is missing." << std::endl;
110#endif
111#if !defined(HAVE_OPENCV_IMGPROC)
112 std::cout << "This tutorial needs OpenCV imgproc module that is missing." << std::endl;
113#endif
114#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION < 0x050000) && !defined(HAVE_OPENCV_OBJDETECT)
115 std::cout << "This tutorial needs OpenCV objdetect module that is missing." << std::endl;
116#endif
117#if defined(VISP_HAVE_OPENCV) && ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && !defined(HAVE_OPENCV_XOBJDETECT))
118 std::cout << "This tutorial needs OpenCV xobjdetect module that is missing." << std::endl;
119#endif
120
121 (void)argc;
122 (void)argv;
123#endif
124}
static const vpColor red
Definition vpColor.h:198
static const vpColor green
Definition vpColor.h:201
vpRect getBBox(size_t i) const
std::vector< std::string > & getMessage()
size_t getNbObjects() const
void setCascadeClassifierFile(const std::string &filename)
bool detect(const vpImage< unsigned char > &I) VP_OVERRIDE
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void setTitle(const vpImage< unsigned char > &I, const std::string &windowtitle)
static void flush(const vpImage< unsigned char > &I)
static void displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition vpException.h:60
Definition of the vpImage class member functions.
Definition vpImage.h:131
Defines a rectangle in the plane.
Definition vpRect.h:79
double getLeft() const
Definition vpRect.h:173
double getTop() const
Definition vpRect.h:192
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void open(vpImage< vpRGBa > &I) VP_OVERRIDE
void setFileName(const std::string &filename)
void acquire(vpImage< vpRGBa > &I) VP_OVERRIDE
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.