Visual Servoing Platform  version 3.5.1 under development (2022-08-17)
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 (VISP_HAVE_OPENCV_VERSION >= 0x020200) && defined(VISP_HAVE_OPENCV_OBJDETECT)
14  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 0;
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(VISP_HAVE_OPENCV)
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 }
double getTop() const
Definition: vpRect.h:193
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:134
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
error that can be emited by ViSP classes.
Definition: vpException.h:71
const char * getMessage() const
Definition: vpException.cpp:87
static const vpColor green
Definition: vpColor.h:220
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:217
void open(vpImage< vpRGBa > &I)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void acquire(vpImage< vpRGBa > &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)
double getLeft() const
Definition: vpRect.h:174
unsigned int getHeight() const
Definition: vpImage.h:188
void setFileName(const std::string &filename)
Defines a rectangle in the plane.
Definition: vpRect.h:79
static void setTitle(const vpImage< unsigned char > &I, const std::string &windowtitle)