Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
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 #if defined(HAVE_OPENCV_VIDEOIO)
12 #include <opencv2/videoio.hpp>
13 #endif
14 
15 int main(int argc, const char *argv[])
16 {
17 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_OBJDETECT)
18 #ifdef ENABLE_VISP_NAMESPACE
19  using namespace VISP_NAMESPACE_NAME;
20 #endif
21  try {
22  std::string opt_face_cascade_name = "./haarcascade_frontalface_alt.xml";
23  unsigned int opt_device = 0;
24  unsigned int opt_scale = 2; // Default value is 2 in the constructor. Turn
25  // it to 1 to avoid subsampling
26 
27  for (int i = 0; i < argc; i++) {
28  if (std::string(argv[i]) == "--haar")
29  opt_face_cascade_name = std::string(argv[i + 1]);
30  else if (std::string(argv[i]) == "--device")
31  opt_device = (unsigned int)atoi(argv[i + 1]);
32  else if (std::string(argv[i]) == "--scale")
33  opt_scale = (unsigned int)atoi(argv[i + 1]);
34  else if (std::string(argv[i]) == "--help") {
35  std::cout << "Usage: " << argv[0]
36  << " [--haar <haarcascade xml filename>] [--device <camera "
37  "device>] [--scale <subsampling factor>] [--help]"
38  << std::endl;
39  return EXIT_SUCCESS;
40  }
41  }
42 
43  vpImage<unsigned char> I; // for gray images
44 
46 #if defined(VISP_HAVE_V4L2)
47  vpV4l2Grabber g;
48  std::ostringstream device;
49  device << "/dev/video" << opt_device;
50  g.setDevice(device.str());
51  g.setScale(opt_scale); // Default value is 2 in the constructor. Turn it
52  // to 1 to avoid subsampling
53  g.acquire(I);
54 #elif defined(HAVE_OPENCV_VIDEOIO)
55  cv::VideoCapture cap(opt_device); // open the default camera
56 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
57  int width = (int)cap.get(cv::CAP_PROP_FRAME_WIDTH);
58  int height = (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT);
59  cap.set(cv::CAP_PROP_FRAME_WIDTH, width / opt_scale);
60  cap.set(cv::CAP_PROP_FRAME_HEIGHT, height / opt_scale);
61 #else
62  int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
63  int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
64  cap.set(CV_CAP_PROP_FRAME_WIDTH, width / opt_scale);
65  cap.set(CV_CAP_PROP_FRAME_HEIGHT, height / opt_scale);
66 #endif
67  if (!cap.isOpened()) { // check if we succeeded
68  std::cout << "Failed to open the camera" << std::endl;
69  return EXIT_FAILURE;
70  }
71  cv::Mat frame;
72  cap >> frame; // get a new frame from camera
73  vpImageConvert::convert(frame, I);
74 #endif
76 
77 #if defined(VISP_HAVE_X11)
78  vpDisplayX d(I);
79 #elif defined(VISP_HAVE_GDI)
80  vpDisplayGDI d(I);
81 #elif defined(HAVE_OPENCV_HIGHGUI)
82  vpDisplayOpenCV d(I);
83 #endif
84  vpDisplay::setTitle(I, "ViSP viewer");
85 
86  vpDetectorFace face_detector;
87  face_detector.setCascadeClassifierFile(opt_face_cascade_name);
88 
89  while (1) {
90  double t = vpTime::measureTimeMs();
92 #if defined(VISP_HAVE_V4L2)
93  g.acquire(I);
94  bool face_found = face_detector.detect(I);
95 #else
96  cap >> frame; // get a new frame from camera
97  vpImageConvert::convert(frame, I);
98  bool face_found = face_detector.detect(frame); // We pass frame to avoid an internal image conversion
99 #endif
101 
103 
104  if (face_found) {
105  std::ostringstream text;
106  text << "Found " << face_detector.getNbObjects() << " face(s)";
107  vpDisplay::displayText(I, 10, 10, text.str(), vpColor::red);
108  for (size_t i = 0; i < face_detector.getNbObjects(); i++) {
109  vpRect bbox = face_detector.getBBox(i);
110  vpDisplay::displayRectangle(I, bbox, vpColor::green, false, 4);
111  vpDisplay::displayText(I, (int)bbox.getTop() - 10, (int)bbox.getLeft(),
112  "Message: \"" + face_detector.getMessage(i) + "\"", vpColor::red);
113  }
114  }
115  vpDisplay::displayText(I, (int)I.getHeight() - 25, 10, "Click to quit...", vpColor::red);
116  vpDisplay::flush(I);
117  if (vpDisplay::getClick(I, false)) // a click to exit
118  break;
119 
120  std::cout << "Loop time: " << vpTime::measureTimeMs() - t << " ms" << std::endl;
121  }
122  }
123  catch (const vpException &e) {
124  std::cout << e.getMessage() << std::endl;
125  }
126 #else
127  (void)argc;
128  (void)argv;
129 #endif
130 }
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
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
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 is a wrapper over the Video4Linux2 (V4L2) driver.
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void setDevice(const std::string &devname)
void acquire(vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()