Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
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]
28  << " [--haar <haarcascade xml filename>] [--video <input video file>]"
29  << " [--help] [-h]"
30  << std::endl;
31  return 0;
32  }
33  }
34 
36 
37  vpVideoReader g;
38  g.setFileName(opt_video);
39  g.open(I);
40 
41 #if defined(VISP_HAVE_X11)
42  vpDisplayX d(I);
43 #elif defined(VISP_HAVE_GDI)
44  vpDisplayGDI d(I);
45 #elif defined(VISP_HAVE_OPENCV)
46  vpDisplayOpenCV d(I);
47 #endif
48  vpDisplay::setTitle(I, "ViSP viewer");
49 
51  vpDetectorFace face_detector;
54  face_detector.setCascadeClassifierFile(opt_face_cascade_name);
56 
57  bool exit_requested = false;
58  while (!g.end() && !exit_requested) {
59  g.acquire(I);
60 
63  bool face_found = face_detector.detect(I);
65 
66  if (face_found) {
67  std::ostringstream text;
69  text << "Found " << face_detector.getNbObjects() << " face(s)";
71  vpDisplay::displayText(I, 10, 10, text.str(), vpColor::red);
73  for (size_t i = 0; i < face_detector.getNbObjects(); i++) {
74  vpRect bbox = face_detector.getBBox(i);
75  vpDisplay::displayRectangle(I, bbox, vpColor::green, false, 4);
76  vpDisplay::displayText(I, (int)bbox.getTop() - 10, (int)bbox.getLeft(),
77  "Message: \"" + face_detector.getMessage(i) + "\"", vpColor::red);
78  }
80  }
81  vpDisplay::displayText(I, (int)I.getHeight() - 25, 10, "Click to quit...", vpColor::red);
83  if (vpDisplay::getClick(I, false)) // a click to exit
84  exit_requested = true;
85  }
86  if (!exit_requested)
88  } catch (const vpException &e) {
89  std::cout << e.getMessage() << std::endl;
90  }
91 #else
92  (void)argc;
93  (void)argv;
94 #endif
95 }
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:90
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)