Visual Servoing Platform  version 3.0.0
tutorial-face-detector-live.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>
6 #include <visp3/detection/vpDetectorFace.h>
7 #ifdef VISP_HAVE_MODULE_SENSOR
8 #include <visp3/sensor/vpV4l2Grabber.h>
9 #endif
10 
11 int main(int argc, const char* argv[])
12 {
13 #if (VISP_HAVE_OPENCV_VERSION >= 0x020200)
14  try {
15  std::string opt_face_cascade_name = "./haarcascade_frontalface_alt.xml";
16 
17  int opt_device = 0;
18 
19  for (int i=0; i<argc; i++) {
20  if (std::string(argv[i]) == "--haar")
21  opt_face_cascade_name = std::string(argv[i+1]);
22  else if (std::string(argv[i]) == "--device")
23  opt_device = atoi(argv[i+1]);
24  else if (std::string(argv[i]) == "--help") {
25  std::cout << "Usage: " << argv[0] << " [--haar <haarcascade xml filename>] [--device <camera device>] [--help]" << std::endl;
26  return 0;
27  }
28  }
29 
30  vpImage<unsigned char> I; // for gray images
31 
33 #if defined(VISP_HAVE_V4L2)
34  vpV4l2Grabber g;
35  std::ostringstream device;
36  device << "/dev/video" << opt_device;
37  g.setDevice(device.str());
38  g.setScale(2);
39  g.acquire(I);
40 #elif defined(VISP_HAVE_OPENCV)
41  cv::VideoCapture cap(opt_device); // open the default camera
42  if(!cap.isOpened()) { // check if we succeeded
43  std::cout << "Failed to open the camera" << std::endl;
44  return -1;
45  }
46  cv::Mat frame;
47  cap >> frame; // get a new frame from camera
48  vpImageConvert::convert(frame, I);
49 #endif
50 
52 #if defined(VISP_HAVE_X11)
53  vpDisplayX d(I);
54 #elif defined(VISP_HAVE_GDI)
55  vpDisplayGDI d(I);
56 #elif defined(VISP_HAVE_OPENCV)
57  vpDisplayOpenCV d(I);
58 #endif
59  vpDisplay::setTitle(I, "ViSP viewer");
60 
61  vpDetectorFace face_detector;
62  face_detector.setCascadeClassifierFile(opt_face_cascade_name);
63 
64  while(1) {
65  double t = vpTime::measureTimeMs();
67 #if defined(VISP_HAVE_V4L2)
68  g.acquire(I);
69 #else
70  cap >> frame; // get a new frame from camera
71  vpImageConvert::convert(frame, I);
72 #endif
73 
76  bool face_found = face_detector.detect(I);
77 
78  if (face_found) {
79  std::ostringstream text;
80  text << "Found " << face_detector.getNbObjects() << " face(s)";
81  vpDisplay::displayText(I, 10, 10, text.str(), vpColor::red);
82  for(size_t i=0; i < face_detector.getNbObjects(); i++) {
83  std::vector<vpImagePoint> p = face_detector.getPolygon(i);
84  vpRect bbox = face_detector.getBBox(i);
85  vpDisplay::displayRectangle(I, bbox, vpColor::green, false, 4);
86  vpDisplay::displayText(I, (int)bbox.getTop()-10, (int)bbox.getLeft(), "Message: \"" + face_detector.getMessage(i) + "\"", vpColor::red);
87  }
88  }
89  vpDisplay::displayText(I, (int)I.getHeight()-25, 10, "Click to quit...", vpColor::red);
91  if (vpDisplay::getClick(I, false)) // a click to exit
92  break;
93 
94  std::cout << "Loop time: " << vpTime::measureTimeMs() - t << " ms" << std::endl;
95  }
96  }
97  catch(vpException &e) {
98  std::cout << e.getMessage() << std::endl;
99  }
100 #else
101  (void)argc;
102  (void)argv;
103 #endif
104 }
const char * getMessage(void)
Definition: vpException.cpp:97
double getTop() const
Definition: vpRect.h:176
void acquire(vpImage< unsigned char > &I)
void setCascadeClassifierFile(const std::string &filename)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
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
void setDevice(const std::string &devname)
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
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
virtual void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)=0
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93
Class for the Video4Linux2 video device.
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