Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
tutorial-face-detector-live.cpp
1 #include <visp3/core/vpConfig.h>
3 #include <visp3/detection/vpDetectorFace.h>
4 #include <visp3/gui/vpDisplayGDI.h>
5 #include <visp3/gui/vpDisplayOpenCV.h>
6 #include <visp3/gui/vpDisplayX.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
18  // it to 1 to avoid subsampling
19 
20  for (int i = 0; i < argc; i++) {
21  if (std::string(argv[i]) == "--haar")
22  opt_face_cascade_name = std::string(argv[i + 1]);
23  else if (std::string(argv[i]) == "--device")
24  opt_device = (unsigned int)atoi(argv[i + 1]);
25  else if (std::string(argv[i]) == "--scale")
26  opt_scale = (unsigned int)atoi(argv[i + 1]);
27  else if (std::string(argv[i]) == "--help") {
28  std::cout << "Usage: " << argv[0]
29  << " [--haar <haarcascade xml filename>] [--device <camera "
30  "device>] [--scale <subsampling factor>] [--help]"
31  << std::endl;
32  return 0;
33  }
34  }
35 
36  vpImage<unsigned char> I; // for gray images
37 
39 #if defined(VISP_HAVE_V4L2)
40  vpV4l2Grabber g;
41  std::ostringstream device;
42  device << "/dev/video" << opt_device;
43  g.setDevice(device.str());
44  g.setScale(opt_scale); // Default value is 2 in the constructor. Turn it
45  // to 1 to avoid subsampling
46  g.acquire(I);
47 #elif defined(VISP_HAVE_OPENCV)
48  cv::VideoCapture cap(opt_device); // open the default camera
49 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
50  int width = (int)cap.get(cv::CAP_PROP_FRAME_WIDTH);
51  int height = (int)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 #else
55  int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
56  int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
57  cap.set(CV_CAP_PROP_FRAME_WIDTH, width / opt_scale);
58  cap.set(CV_CAP_PROP_FRAME_HEIGHT, height / opt_scale);
59 #endif
60  if (!cap.isOpened()) { // check if we succeeded
61  std::cout << "Failed to open the camera" << std::endl;
62  return -1;
63  }
64  cv::Mat frame;
65  cap >> frame; // get a new frame from camera
66  vpImageConvert::convert(frame, I);
67 #endif
68 
70 #if defined(VISP_HAVE_X11)
71  vpDisplayX d(I);
72 #elif defined(VISP_HAVE_GDI)
73  vpDisplayGDI d(I);
74 #elif defined(VISP_HAVE_OPENCV)
75  vpDisplayOpenCV d(I);
76 #endif
77  vpDisplay::setTitle(I, "ViSP viewer");
78 
79  vpDetectorFace face_detector;
80  face_detector.setCascadeClassifierFile(opt_face_cascade_name);
81 
82  while (1) {
83  double t = vpTime::measureTimeMs();
85 #if defined(VISP_HAVE_V4L2)
86  g.acquire(I);
87  bool face_found = face_detector.detect(I);
88 #else
89  cap >> frame; // get a new frame from camera
90  vpImageConvert::convert(frame, I);
91  bool face_found = face_detector.detect(frame); // We pass frame to avoid an internal image conversion
92 #endif
93 
96 
97  if (face_found) {
98  std::ostringstream text;
99  text << "Found " << face_detector.getNbObjects() << " face(s)";
100  vpDisplay::displayText(I, 10, 10, text.str(), vpColor::red);
101  for (size_t i = 0; i < face_detector.getNbObjects(); i++) {
102  vpRect bbox = face_detector.getBBox(i);
103  vpDisplay::displayRectangle(I, bbox, vpColor::green, false, 4);
104  vpDisplay::displayText(I, (int)bbox.getTop() - 10, (int)bbox.getLeft(),
105  "Message: \"" + face_detector.getMessage(i) + "\"", vpColor::red);
106  }
107  }
108  vpDisplay::displayText(I, (int)I.getHeight() - 25, 10, "Click to quit...", vpColor::red);
109  vpDisplay::flush(I);
110  if (vpDisplay::getClick(I, false)) // a click to exit
111  break;
112 
113  std::cout << "Loop time: " << vpTime::measureTimeMs() - t << " ms" << std::endl;
114  }
115  } catch (const vpException &e) {
116  std::cout << e.getMessage() << std::endl;
117  }
118 #else
119  (void)argc;
120  (void)argv;
121 #endif
122 }
vpRect getBBox(size_t i) const
double getTop() const
Definition: vpRect.h:192
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:150
void setDevice(const std::string &devname)
error that can be emited by ViSP classes.
Definition: vpException.h:71
size_t getNbObjects() const
static const vpColor green
Definition: vpColor.h:182
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
static const vpColor red
Definition: vpColor.h:179
bool detect(const vpImage< unsigned char > &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...
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)
double getLeft() const
Definition: vpRect.h:173
const char * getMessage(void) const
Definition: vpException.cpp:90
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
unsigned int getHeight() const
Definition: vpImage.h:186
Defines a rectangle in the plane.
Definition: vpRect.h:78
std::vector< std::string > & getMessage()
static void setTitle(const vpImage< unsigned char > &I, const std::string &windowtitle)