Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
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(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_OBJDETECT)
15 #ifdef ENABLE_VISP_NAMESPACE
16  using namespace VISP_NAMESPACE_NAME;
17 #endif
19  try {
21  std::string opt_face_cascade_name = "./haarcascade_frontalface_alt.xml";
22  std::string opt_video = "video.mp4";
24 
25  for (int i = 0; i < argc; i++) {
26  if (std::string(argv[i]) == "--haar")
27  opt_face_cascade_name = std::string(argv[i + 1]);
28  else if (std::string(argv[i]) == "--video")
29  opt_video = std::string(argv[i + 1]);
30  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
31  std::cout << "Usage: " << argv[0] << " [--haar <haarcascade xml filename>] [--video <input video file>]"
32  << " [--help] [-h]" << std::endl;
33  return EXIT_SUCCESS;
34  }
35  }
36 
38 
39  vpVideoReader g;
40  g.setFileName(opt_video);
41  g.open(I);
42 
43 #if defined(VISP_HAVE_X11)
44  vpDisplayX d(I);
45 #elif defined(VISP_HAVE_GDI)
46  vpDisplayGDI d(I);
47 #elif defined(HAVE_OPENCV_HIGHGUI)
48  vpDisplayOpenCV d(I);
49 #endif
50  vpDisplay::setTitle(I, "ViSP viewer");
51 
53  vpDetectorFace face_detector;
56  face_detector.setCascadeClassifierFile(opt_face_cascade_name);
58 
59  bool exit_requested = false;
60  while (!g.end() && !exit_requested) {
61  g.acquire(I);
62 
65  bool face_found = face_detector.detect(I);
67 
68  if (face_found) {
69  std::ostringstream text;
71  text << "Found " << face_detector.getNbObjects() << " face(s)";
73  vpDisplay::displayText(I, 10, 10, text.str(), vpColor::red);
75  for (size_t i = 0; i < face_detector.getNbObjects(); i++) {
76  vpRect bbox = face_detector.getBBox(i);
77  vpDisplay::displayRectangle(I, bbox, vpColor::green, false, 4);
78  vpDisplay::displayText(I, (int)bbox.getTop() - 10, (int)bbox.getLeft(),
79  "Message: \"" + face_detector.getMessage(i) + "\"", vpColor::red);
80  }
82  }
83  vpDisplay::displayText(I, (int)I.getHeight() - 25, 10, "Click to quit...", vpColor::red);
85  if (vpDisplay::getClick(I, false)) // a click to exit
86  exit_requested = true;
87  }
88  if (!exit_requested)
90  }
91  catch (const vpException &e) {
92  std::cout << e.getMessage() << std::endl;
93 }
94 #else
95  (void)argc;
96  (void)argv;
97 #endif
98 }
static const vpColor red
Definition: vpColor.h:217
static const vpColor green
Definition: vpColor.h:220
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)