Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-face-detector-live.cpp
1
2#include <iostream>
3
4#include <visp3/core/vpConfig.h>
5
7// Comment / uncomment following lines to use the specific 3rd party compatible with your camera
8// #undef VISP_HAVE_V4L2
9// #undef HAVE_OPENCV_HIGHGUI
10// #undef HAVE_OPENCV_VIDEOIO
12
13#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && \
14 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_OBJDETECT)) || \
15 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_XOBJDETECT))) && \
16 (defined(VISP_HAVE_V4L2) || \
17 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
18 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))))
19
20#include <visp3/detection/vpDetectorFace.h>
21#include <visp3/gui/vpDisplayFactory.h>
22#ifdef VISP_HAVE_MODULE_SENSOR
23#include <visp3/sensor/vpV4l2Grabber.h>
24#endif
25
26#if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
27#include <opencv2/highgui/highgui.hpp> // for cv::VideoCapture
28#elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
29#include <opencv2/videoio/videoio.hpp>
30#endif
31
32int main(int argc, const char *argv[])
33{
34#ifdef ENABLE_VISP_NAMESPACE
35 using namespace VISP_NAMESPACE_NAME;
36#endif
37#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
38 std::shared_ptr<vpDisplay> display;
39#else
40 vpDisplay *display = nullptr;
41#endif
42 try {
43 std::string opt_face_cascade_name = "./haarcascade_frontalface_alt.xml";
44 unsigned int opt_device = 0;
45 unsigned int opt_scale = 2; // Default value is 2 in the constructor. Turn
46 // it to 1 to avoid subsampling
47
48 for (int i = 1; i < argc; i++) {
49 if (std::string(argv[i]) == "--haar" && i + 1 < argc) {
50 opt_face_cascade_name = std::string(argv[++i]);
51 }
52 else if (std::string(argv[i]) == "--device" && i + 1 < argc) {
53 opt_device = static_cast<unsigned int>(atoi(argv[++i]));
54 }
55 else if (std::string(argv[i]) == "--scale" && i + 1 < argc) {
56 opt_scale = static_cast<unsigned int>(atoi(argv[++i]));
57 }
58 else if ((std::string(argv[i]) == "--help") || (std::string(argv[i]) == "-h")) {
59 std::cout << "Usage: " << argv[0]
60 << " [--haar <haarcascade xml filename>]"
61 << " [--device <camera device>]"
62 << " [--scale <subsampling factor>]"
63 << " [--help] [-h]"
64 << std::endl;
65 return EXIT_SUCCESS;
66 }
67 }
68
69 vpImage<unsigned char> I; // for gray images
70
72#if defined(VISP_HAVE_V4L2)
74 std::ostringstream device;
75 device << "/dev/video" << opt_device;
76 g.setDevice(device.str());
77 g.setScale(opt_scale); // Default value is 2 in the constructor. Turn it
78 // to 1 to avoid subsampling
79 g.acquire(I);
80#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
81 cv::VideoCapture cap(opt_device); // open the default camera
82#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
83 int width = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_WIDTH));
84 int height = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_HEIGHT));
85 cap.set(cv::CAP_PROP_FRAME_WIDTH, width / opt_scale);
86 cap.set(cv::CAP_PROP_FRAME_HEIGHT, height / opt_scale);
87#else
88 int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
89 int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
90 cap.set(CV_CAP_PROP_FRAME_WIDTH, width / opt_scale);
91 cap.set(CV_CAP_PROP_FRAME_HEIGHT, height / opt_scale);
92#endif
93 if (!cap.isOpened()) { // check if we succeeded
94 std::cout << "Failed to open the camera" << std::endl;
95 return EXIT_FAILURE;
96 }
97 cv::Mat frame;
98 cap >> frame; // get a new frame from camera
100#endif
102
103#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
105#else
107#endif
108 vpDisplay::setTitle(I, "ViSP viewer");
109
110 vpDetectorFace face_detector;
111 face_detector.setCascadeClassifierFile(opt_face_cascade_name);
112
113 while (1) {
114 double t = vpTime::measureTimeMs();
116#if defined(VISP_HAVE_V4L2)
117 g.acquire(I);
118 bool face_found = face_detector.detect(I);
119#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
120 cap >> frame; // get a new frame from camera
121 vpImageConvert::convert(frame, I);
122 bool face_found = face_detector.detect(frame); // We pass frame to avoid an internal image conversion
123#endif
125
127
128 if (face_found) {
129 std::ostringstream text;
130 text << "Found " << face_detector.getNbObjects() << " face(s)";
131 vpDisplay::displayText(I, 10, 10, text.str(), vpColor::red);
132 for (size_t i = 0; i < face_detector.getNbObjects(); i++) {
133 vpRect bbox = face_detector.getBBox(i);
135 vpDisplay::displayText(I, static_cast<int>(bbox.getTop()) - 10, static_cast<int>(bbox.getLeft()),
136 "Message: \"" + face_detector.getMessage(i) + "\"", vpColor::red);
137 }
138 }
139 vpDisplay::displayText(I, static_cast<int>(I.getHeight()) - 25, 10, "Click to quit...", vpColor::red);
141 if (vpDisplay::getClick(I, false)) // a click to exit
142 break;
143
144 std::cout << "Loop time: " << vpTime::measureTimeMs() - t << " ms" << std::endl;
145 }
146 }
147 catch (const vpException &e) {
148 std::cout << e.getMessage() << std::endl;
149 }
150#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
151 if (display != nullptr) {
152 delete display;
153 }
154#endif
155}
156
157#else
158
159int main()
160{
161#if !defined(HAVE_OPENCV_IMGPROC)
162 std::cout << "This tutorial needs OpenCV imgproc module that is missing." << std::endl;
163#endif
164#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION < 0x050000) && !defined(HAVE_OPENCV_OBJDETECT)
165 std::cout << "This tutorial needs OpenCV objdetect module that is missing." << std::endl;
166#endif
167#if defined(VISP_HAVE_OPENCV) && ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && !defined(HAVE_OPENCV_XOBJDETECT))
168 std::cout << "This tutorial needs OpenCV xobjdetect module that is missing." << std::endl;
169#endif
170}
171
172#endif
static const vpColor red
Definition vpColor.h:198
static const vpColor green
Definition vpColor.h:201
vpRect getBBox(size_t i) const
std::vector< std::string > & getMessage()
size_t getNbObjects() const
void setCascadeClassifierFile(const std::string &filename)
bool detect(const vpImage< unsigned char > &I) VP_OVERRIDE
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Definition of the vpImage class member functions.
Definition vpImage.h:131
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)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT double measureTimeMs()