Visual Servoing Platform  version 3.0.0
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) && (VISP_HAVE_OPENCV_VERSION >= 0x020200)
14  try {
17  std::string opt_face_cascade_name = "./haarcascade_frontalface_alt.xml";
18  std::string opt_video = "video.mpeg";
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") {
27  std::cout << "Usage: " << argv[0] << " [--haar <haarcascade xml filename>] [--video <input video file>] [--help]" << std::endl;
28  return 0;
29  }
30  }
31 
33 
34  vpVideoReader g;
35  g.setFileName(opt_video);
36  g.open(I);
37 
38 #if defined(VISP_HAVE_X11)
39  vpDisplayX d(I);
40 #elif defined(VISP_HAVE_GDI)
41  vpDisplayGDI d(I);
42 #elif defined(VISP_HAVE_OPENCV)
43  vpDisplayOpenCV d(I);
44 #endif
45  vpDisplay::setTitle(I, "ViSP viewer");
46 
48  vpDetectorFace face_detector;
51  face_detector.setCascadeClassifierFile(opt_face_cascade_name);
53 
54  bool exit_requested = false;
55  while( ! g.end() && ! exit_requested) {
56  g.acquire(I);
57 
60  bool face_found = face_detector.detect(I);
62 
63  if (face_found) {
64  std::ostringstream text;
66  text << "Found " << face_detector.getNbObjects() << " face(s)";
68  vpDisplay::displayText(I, 10, 10, text.str(), vpColor::red);
70  for(size_t i=0; i < face_detector.getNbObjects(); i++) {
71  std::vector<vpImagePoint> p = face_detector.getPolygon(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  }
87  catch(vpException &e) {
88  std::cout << e.getMessage() << std::endl;
89  }
90 #else
91  (void)argc;
92  (void)argv;
93 #endif
94 }
const char * getMessage(void)
Definition: vpException.cpp:97
double getTop() const
Definition: vpRect.h:176
void setCascadeClassifierFile(const std::string &filename)
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)
Definition: vpDisplay.cpp:888
Define the X11 console to display images.
Definition: vpDisplayX.h:148
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:73
size_t getNbObjects() const
static const vpColor green
Definition: vpColor.h:166
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
static const vpColor red
Definition: vpColor.h:163
void open(vpImage< vpRGBa > &I)
bool detect(const vpImage< unsigned char > &I)
std::vector< std::vector< vpImagePoint > > & getPolygon()
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void setTitle(const char *title)=0
void acquire(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
virtual void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)=0
std::string & getMessage(size_t i)
unsigned int getHeight() const
Definition: vpImage.h:152
Defines a rectangle in the plane.
Definition: vpRect.h:81
virtual bool getClick(bool blocking=true)=0
vpRect getBBox(size_t i) const
double getLeft() const
Definition: vpRect.h:157