Visual Servoing Platform  version 3.6.1 under development (2024-04-18)
tutorial-face-detector.cpp
1 #include <visp3/gui/vpDisplayGDI.h>
3 #include <visp3/gui/vpDisplayOpenCV.h>
4 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/detection/vpDetectorFace.h>
8 #include <visp3/io/vpVideoReader.h>
9 
10 int main(int argc, const char *argv[])
11 {
13 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_OBJDETECT)
15  try {
17  std::string opt_face_cascade_name = "./haarcascade_frontalface_alt.xml";
18  std::string opt_video = "video.mp4";
20 
21  for (int i = 0; i < argc; i++) {
22  if (std::string(argv[i]) == "--haar")
23  opt_face_cascade_name = std::string(argv[i + 1]);
24  else if (std::string(argv[i]) == "--video")
25  opt_video = std::string(argv[i + 1]);
26  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
27  std::cout << "Usage: " << argv[0] << " [--haar <haarcascade xml filename>] [--video <input video file>]"
28  << " [--help] [-h]" << std::endl;
29  return EXIT_SUCCESS;
30  }
31  }
32 
34 
35  vpVideoReader g;
36  g.setFileName(opt_video);
37  g.open(I);
38 
39 #if defined(VISP_HAVE_X11)
40  vpDisplayX d(I);
41 #elif defined(VISP_HAVE_GDI)
42  vpDisplayGDI d(I);
43 #elif defined(HAVE_OPENCV_HIGHGUI)
44  vpDisplayOpenCV d(I);
45 #endif
46  vpDisplay::setTitle(I, "ViSP viewer");
47 
49  vpDetectorFace face_detector;
52  face_detector.setCascadeClassifierFile(opt_face_cascade_name);
54 
55  bool exit_requested = false;
56  while (!g.end() && !exit_requested) {
57  g.acquire(I);
58 
61  bool face_found = face_detector.detect(I);
63 
64  if (face_found) {
65  std::ostringstream text;
67  text << "Found " << face_detector.getNbObjects() << " face(s)";
69  vpDisplay::displayText(I, 10, 10, text.str(), vpColor::red);
71  for (size_t i = 0; i < face_detector.getNbObjects(); i++) {
72  vpRect bbox = face_detector.getBBox(i);
73  vpDisplay::displayRectangle(I, bbox, vpColor::green, false, 4);
74  vpDisplay::displayText(I, (int)bbox.getTop() - 10, (int)bbox.getLeft(),
75  "Message: \"" + face_detector.getMessage(i) + "\"", vpColor::red);
76  }
78  }
79  vpDisplay::displayText(I, (int)I.getHeight() - 25, 10, "Click to quit...", vpColor::red);
81  if (vpDisplay::getClick(I, false)) // a click to exit
82  exit_requested = true;
83  }
84  if (!exit_requested)
86  } catch (const vpException &e) {
87  std::cout << e.getMessage() << std::endl;
88  }
89 #else
90  (void)argc;
91  (void)argv;
92 #endif
93 }
static const vpColor red
Definition: vpColor.h:211
static const vpColor green
Definition: vpColor.h:214
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:128
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
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:59
const char * getMessage() const
Definition: vpException.cpp:64
unsigned int getHeight() const
Definition: vpImage.h:184
Defines a rectangle in the plane.
Definition: vpRect.h:76
double getLeft() const
Definition: vpRect.h:170
double getTop() const
Definition: vpRect.h:189
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)