Visual Servoing Platform version 3.5.0
tutorial-face-detector-live.cpp
1
2#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
11int main(int argc, const char *argv[])
12{
13#if (VISP_HAVE_OPENCV_VERSION >= 0x020200) && defined(VISP_HAVE_OPENCV_OBJDETECT)
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)
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
67#endif
69
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
91 bool face_found = face_detector.detect(frame); // We pass frame to avoid an internal image conversion
92#endif
94
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);
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);
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}
static const vpColor red
Definition: vpColor.h:217
static const vpColor green
Definition: vpColor.h:220
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
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 emited by ViSP classes.
Definition: vpException.h:72
const char * getMessage() const
Definition: vpException.cpp:90
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
unsigned int getHeight() const
Definition: vpImage.h:188
Defines a rectangle in the plane.
Definition: vpRect.h:80
double getLeft() const
Definition: vpRect.h:174
double getTop() const
Definition: vpRect.h:193
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()