Visual Servoing Platform  version 3.6.1 under development (2025-02-05)
tutorial-face-detector.cpp
1 #include <visp3/core/vpConfig.h>
3 #include <visp3/gui/vpDisplayGDI.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/gui/vpDisplayX.h>
7 #include <visp3/detection/vpDetectorFace.h>
9 #include <visp3/io/vpVideoReader.h>
10 
11 int main(int argc, const char *argv[])
12 {
14 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && \
15  (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_OBJDETECT)) || ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_XOBJDETECT)))
16 #ifdef ENABLE_VISP_NAMESPACE
17  using namespace VISP_NAMESPACE_NAME;
18 #endif
20  try {
22  std::string opt_face_cascade_name = "./haarcascade_frontalface_alt.xml";
23  std::string opt_video = "video.mp4";
25 
26  for (int i = 1; i < argc; i++) {
27  if (std::string(argv[i]) == "--haar" && i + 1 < argc) {
28  opt_face_cascade_name = std::string(argv[++i]);
29  }
30  else if (std::string(argv[i]) == "--video" && i + 1 < argc) {
31  opt_video = std::string(argv[++i]);
32  }
33  else if ((std::string(argv[i]) == "--help") || (std::string(argv[i]) == "-h")) {
34  std::cout << "Usage: " << argv[0]
35  << " [--haar <haarcascade xml filename>]"
36  << " [--video <input video file>]"
37  << " [--help] [-h]" << std::endl;
38  return EXIT_SUCCESS;
39  }
40  }
41 
43 
44  vpVideoReader g;
45  g.setFileName(opt_video);
46  g.open(I);
47 
48 #if defined(VISP_HAVE_X11)
49  vpDisplayX d(I);
50 #elif defined(VISP_HAVE_GDI)
51  vpDisplayGDI d(I);
52 #elif defined(HAVE_OPENCV_HIGHGUI)
53  vpDisplayOpenCV d(I);
54 #endif
55  vpDisplay::setTitle(I, "ViSP viewer");
56 
58  vpDetectorFace face_detector;
61  face_detector.setCascadeClassifierFile(opt_face_cascade_name);
63 
64  bool exit_requested = false;
65  while (!g.end() && !exit_requested) {
66  g.acquire(I);
67 
70  bool face_found = face_detector.detect(I);
72 
73  if (face_found) {
74  std::ostringstream text;
76  text << "Found " << face_detector.getNbObjects() << " face(s)";
78  vpDisplay::displayText(I, 10, 10, text.str(), vpColor::red);
80  for (size_t i = 0; i < face_detector.getNbObjects(); i++) {
81  vpRect bbox = face_detector.getBBox(i);
82  vpDisplay::displayRectangle(I, bbox, vpColor::green, false, 4);
83  vpDisplay::displayText(I, (int)bbox.getTop() - 10, (int)bbox.getLeft(),
84  "Message: \"" + face_detector.getMessage(i) + "\"", vpColor::red);
85  }
87  }
88  vpDisplay::displayText(I, (int)I.getHeight() - 25, 10, "Click to quit...", vpColor::red);
90  if (vpDisplay::getClick(I, false)) // a click to exit
91  exit_requested = true;
92  }
93  if (!exit_requested)
95  }
96  catch (const vpException &e) {
97  std::cout << e.getMessage() << std::endl;
98  }
99 #else
100 
101 #if !defined(HAVE_OPENCV_HIGHGUI)
102  std::cout << "This tutorial needs OpenCV highgui module that is missing." << std::endl;
103 #endif
104 #if !defined(HAVE_OPENCV_IMGPROC)
105  std::cout << "This tutorial needs OpenCV imgproc module that is missing." << std::endl;
106 #endif
107 #if (VISP_HAVE_OPENCV_VERSION < 0x050000) && !defined(HAVE_OPENCV_OBJDETECT)
108  std::cout << "This tutorial needs OpenCV objdetect module that is missing." << std::endl;
109 #endif
110 #if ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && !defined(HAVE_OPENCV_XOBJDETECT))
111  std::cout << "This tutorial needs OpenCV xobjdetect module that is missing." << std::endl;
112 #endif
113 
114  (void)argc;
115  (void)argv;
116 #endif
117 }
static const vpColor red
Definition: vpColor.h:198
static const vpColor green
Definition: vpColor.h:201
vpRect getBBox(size_t i) const
size_t getNbObjects() const
std::vector< std::string > & getMessage()
void setCascadeClassifierFile(const std::string &filename)
bool detect(const vpImage< unsigned char > &I) VP_OVERRIDE
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
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
const char * getMessage() const
Definition: vpException.cpp:65
unsigned int getHeight() const
Definition: vpImage.h:181
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 acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)