Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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  unsigned int opt_device = 0;
17  unsigned int opt_scale = 2; // Default value is 2 in the constructor. Turn it to 1 to avoid subsampling
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 = (unsigned int)atoi(argv[i+1]);
24  else if (std::string(argv[i]) == "--scale")
25  opt_scale = (unsigned int)atoi(argv[i+1]);
26  else if (std::string(argv[i]) == "--help") {
27  std::cout << "Usage: " << argv[0] << " [--haar <haarcascade xml filename>] [--device <camera device>] [--scale <subsampling factor>] [--help]" << std::endl;
28  return 0;
29  }
30  }
31 
32  vpImage<unsigned char> I; // for gray images
33 
35 #if defined(VISP_HAVE_V4L2)
36  vpV4l2Grabber g;
37  std::ostringstream device;
38  device << "/dev/video" << opt_device;
39  g.setDevice(device.str());
40  g.setScale(opt_scale); // Default value is 2 in the constructor. Turn it to 1 to avoid subsampling
41  g.acquire(I);
42 #elif defined(VISP_HAVE_OPENCV)
43  cv::VideoCapture cap(opt_device); // open the default camera
44 # if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
45  int width = (int)cap.get(cv::CAP_PROP_FRAME_WIDTH);
46  int height = (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT);
47  cap.set(cv::CAP_PROP_FRAME_WIDTH, width/opt_scale);
48  cap.set(cv::CAP_PROP_FRAME_HEIGHT, height/opt_scale);
49 # else
50  int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
51  int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
52  cap.set(CV_CAP_PROP_FRAME_WIDTH, width/opt_scale);
53  cap.set(CV_CAP_PROP_FRAME_HEIGHT, height/opt_scale);
54 # endif
55  if(!cap.isOpened()) { // check if we succeeded
56  std::cout << "Failed to open the camera" << std::endl;
57  return -1;
58  }
59  cv::Mat frame;
60  cap >> frame; // get a new frame from camera
61  vpImageConvert::convert(frame, I);
62 #endif
63 
65 #if defined(VISP_HAVE_X11)
66  vpDisplayX d(I);
67 #elif defined(VISP_HAVE_GDI)
68  vpDisplayGDI d(I);
69 #elif defined(VISP_HAVE_OPENCV)
70  vpDisplayOpenCV d(I);
71 #endif
72  vpDisplay::setTitle(I, "ViSP viewer");
73 
74  vpDetectorFace face_detector;
75  face_detector.setCascadeClassifierFile(opt_face_cascade_name);
76 
77  while(1) {
78  double t = vpTime::measureTimeMs();
80 #if defined(VISP_HAVE_V4L2)
81  g.acquire(I);
82  bool face_found = face_detector.detect(I);
83 #else
84  cap >> frame; // get a new frame from camera
85  vpImageConvert::convert(frame, I);
86  bool face_found = face_detector.detect(frame); // We pass frame to avoid an internal image conversion
87 #endif
88 
91 
92  if (face_found) {
93  std::ostringstream text;
94  text << "Found " << face_detector.getNbObjects() << " face(s)";
95  vpDisplay::displayText(I, 10, 10, text.str(), vpColor::red);
96  for(size_t i=0; i < face_detector.getNbObjects(); i++) {
97  std::vector<vpImagePoint> p = face_detector.getPolygon(i);
98  vpRect bbox = face_detector.getBBox(i);
99  vpDisplay::displayRectangle(I, bbox, vpColor::green, false, 4);
100  vpDisplay::displayText(I, (int)bbox.getTop()-10, (int)bbox.getLeft(), "Message: \"" + face_detector.getMessage(i) + "\"", vpColor::red);
101  }
102  }
103  vpDisplay::displayText(I, (int)I.getHeight()-25, 10, "Click to quit...", vpColor::red);
104  vpDisplay::flush(I);
105  if (vpDisplay::getClick(I, false)) // a click to exit
106  break;
107 
108  std::cout << "Loop time: " << vpTime::measureTimeMs() - t << " ms" << std::endl;
109  }
110  }
111  catch(vpException &e) {
112  std::cout << e.getMessage() << std::endl;
113  }
114 #else
115  (void)argc;
116  (void)argv;
117 #endif
118 }
double getTop() const
Definition: vpRect.h:178
void acquire(vpImage< unsigned char > &I)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
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)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
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)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93
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)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
const char * getMessage(void) const
Definition: vpException.cpp:97
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)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
std::string & getMessage(size_t i)
unsigned int getHeight() const
Definition: vpImage.h:175
Defines a rectangle in the plane.
Definition: vpRect.h:82
vpRect getBBox(size_t i) const
static void setTitle(const vpImage< unsigned char > &I, const std::string &windowtitle)
double getLeft() const
Definition: vpRect.h:159