Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-face-detector-live-threaded.cpp
1
3#include <iostream>
4
5#include <visp3/core/vpConfig.h>
6
8// Comment / uncomment following lines to use the specific 3rd party compatible with your camera
9// #undef VISP_HAVE_V4L2
10// #undef HAVE_OPENCV_HIGHGUI
11// #undef HAVE_OPENCV_VIDEOIO
13
14#if defined(VISP_HAVE_THREADS) && defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && \
15 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_OBJDETECT)) || \
16 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_XOBJDETECT))) && \
17 (defined(VISP_HAVE_V4L2) || \
18 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
19 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))))
20
21#include <thread>
22#include <mutex>
23
24#include <visp3/core/vpImageConvert.h>
25#include <visp3/core/vpTime.h>
26#include <visp3/detection/vpDetectorFace.h>
27#include <visp3/gui/vpDisplayFactory.h>
28#include <visp3/sensor/vpV4l2Grabber.h>
29
30#if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
31#include <opencv2/highgui/highgui.hpp> // for cv::VideoCapture
32#elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
33#include <opencv2/videoio/videoio.hpp> // for cv::VideoCapture
34#endif
35
36#ifdef ENABLE_VISP_NAMESPACE
37using namespace VISP_NAMESPACE_NAME;
38#endif
39
40// Shared vars
41typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
42
43#if defined(VISP_HAVE_V4L2)
44void captureFunction(vpV4l2Grabber &cap, std::mutex &mutex_capture, vpImage<unsigned char> &frame, t_CaptureState &capture_state);
45
46void captureFunction(vpV4l2Grabber &cap, std::mutex &mutex_capture, vpImage<unsigned char> &frame, t_CaptureState &capture_state)
47#elif defined(HAVE_OPENCV_VIDEOIO)
48void captureFunction(cv::VideoCapture &cap, std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state);
49
50void captureFunction(cv::VideoCapture &cap, std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state)
51#endif
52{
53 // If the image is larger than 640 by 480, we subsample
54#if defined(VISP_HAVE_V4L2)
56#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
57 cv::Mat frame_;
58#endif
59 bool stop_capture_ = false;
60
62 while ((vpTime::measureTimeSecond() - start_time) < 30 && !stop_capture_) {
63 // Capture in progress
64 cap >> frame_; // get a new frame from camera
65
66 // Update shared data
67 {
68 std::lock_guard<std::mutex> lock(mutex_capture);
69 if (capture_state == capture_stopped)
70 stop_capture_ = true;
71 else
72 capture_state = capture_started;
73 frame = frame_;
74 }
75 }
76 {
77 std::lock_guard<std::mutex> lock(mutex_capture);
78 capture_state = capture_stopped;
79 }
80
81 std::cout << "End of capture thread" << std::endl;
82}
83
84#if defined(VISP_HAVE_V4L2)
85void displayFunction(std::mutex &mutex_capture, std::mutex &mutex_face, vpImage<unsigned char> &frame, t_CaptureState &capture_state, vpRect &face_bbox, bool &face_available);
86
87void displayFunction(std::mutex &mutex_capture, std::mutex &mutex_face, vpImage<unsigned char> &frame, t_CaptureState &capture_state, vpRect &face_bbox, bool &face_available)
88#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
89void displayFunction(std::mutex &mutex_capture, std::mutex &mutex_face, cv::Mat &frame, t_CaptureState &capture_state, vpRect &face_bbox, bool &face_available);
90
91void displayFunction(std::mutex &mutex_capture, std::mutex &mutex_face, cv::Mat &frame, t_CaptureState &capture_state, vpRect &face_bbox, bool &face_available)
92#endif
93{
95
96 t_CaptureState capture_state_;
97 bool display_initialized_ = false;
98 bool face_available_ = false;
99 vpRect face_bbox_;
100#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
101 std::shared_ptr<vpDisplay> display;
102#else
103 vpDisplay *display = nullptr;
104#endif
105
106 do {
107 mutex_capture.lock();
108 capture_state_ = capture_state;
109 mutex_capture.unlock();
110
111 // Check if a frame is available
112 if (capture_state_ == capture_started) {
113 // Get the frame and convert it to a ViSP image used by the display
114 // class
115 {
116 std::lock_guard<std::mutex> lock(mutex_capture);
117#if defined(VISP_HAVE_V4L2)
118 I_ = frame;
119#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
120 vpImageConvert::convert(frame, I_);
121#endif
122 }
123
124 // Check if we need to initialize the display with the first frame
125 if (!display_initialized_) {
126 // Initialize the display
127#if defined(VISP_HAVE_DISPLAY)
128#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
130#else
132#endif
133 display_initialized_ = true;
134#endif
135 }
136
137 // Display the image
139
140 // Check if a face was detected
141 {
142
143 std::lock_guard<std::mutex> lock(mutex_face);
144 face_available_ = face_available;
145 face_bbox_ = face_bbox;
146 }
147 if (face_available_) {
148 // Access to the face bounding box to display it
149 vpDisplay::displayRectangle(I_, face_bbox_, vpColor::green, false, 4);
150 face_available_ = false;
151 }
152
153 // Trigger end of acquisition with a mouse click
154 vpDisplay::displayText(I_, 10, 10, "Click to exit...", vpColor::red);
155 if (vpDisplay::getClick(I_, false)) {
156 std::lock_guard<std::mutex> lock(mutex_capture);
157 capture_state = capture_stopped;
158 }
159
160 // Update the display
162 }
163 else {
164 vpTime::wait(2); // Sleep 2ms
165 }
166 } while (capture_state_ != capture_stopped);
167
168#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
169 if (display != nullptr) {
170 delete display;
171 }
172#endif
173
174 std::cout << "End of display thread" << std::endl;
175}
176
178#if defined(VISP_HAVE_V4L2)
179void detectionFunction(std::mutex &mutex_capture, std::mutex &mutex_face, vpImage<unsigned char> &frame, t_CaptureState &capture_state, vpRect &face_bbox, std::string &face_cascade_name, bool &face_available);
180
181void detectionFunction(std::mutex &mutex_capture, std::mutex &mutex_face, vpImage<unsigned char> &frame, t_CaptureState &capture_state, vpRect &face_bbox, std::string &face_cascade_name, bool &face_available)
182#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
183void detectionFunction(std::mutex &mutex_capture, std::mutex &mutex_face, cv::Mat &frame, t_CaptureState &capture_state, vpRect &face_bbox, std::string &face_cascade_name, bool &face_available);
184
185void detectionFunction(std::mutex &mutex_capture, std::mutex &mutex_face, cv::Mat &frame, t_CaptureState &capture_state, vpRect &face_bbox, std::string &face_cascade_name, bool &face_available)
186#endif
187{
188 vpDetectorFace face_detector_;
189 face_detector_.setCascadeClassifierFile(face_cascade_name);
190
191 t_CaptureState capture_state_;
192#if defined(VISP_HAVE_V4L2)
194#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
195 cv::Mat frame_;
196#endif
197 do {
198 mutex_capture.lock();
199 capture_state_ = capture_state;
200 mutex_capture.unlock();
201
202 // Check if a frame is available
203 if (capture_state_ == capture_started) {
204 // Backup the frame
205 {
206 std::lock_guard<std::mutex> lock(mutex_capture);
207 frame_ = frame;
208 }
209
210 // Detect faces
211 bool face_found_ = face_detector_.detect(frame_);
212 if (face_found_) {
213 std::lock_guard<std::mutex> lock(mutex_face);
214 face_available = true;
215 face_bbox = face_detector_.getBBox(0); // Get largest face bounding box
216 }
217 }
218 else {
219 vpTime::wait(2); // Sleep 2ms
220 }
221 } while (capture_state_ != capture_stopped);
222 std::cout << "End of face detection thread" << std::endl;
223}
225
227int main(int argc, const char *argv[])
228{
229 std::string opt_face_cascade_name = "./haarcascade_frontalface_alt.xml";
230 unsigned int opt_device = 0;
231 unsigned int opt_scale = 2; // Default value is 2 in the constructor. Turn
232 // it to 1 to avoid subsampling
233
234 for (int i = 1; i < argc; i++) {
235 if (std::string(argv[i]) == "--haar" && i + 1 < argc) {
236 opt_face_cascade_name = std::string(argv[++i]);
237 }
238 else if (std::string(argv[i]) == "--device" && i + 1 < argc) {
239 opt_device = static_cast<unsigned int>(atoi(argv[++i]));
240 }
241 else if (std::string(argv[i]) == "--scale" && i + 1 < argc) {
242 opt_scale = static_cast<unsigned int>(atoi(argv[++i]));
243 }
244 else if ((std::string(argv[i]) == "--help") || (std::string(argv[i]) == "-h")) {
245 std::cout << "Usage: " << argv[0]
246 << " [--haar <haarcascade xml filename>]"
247 << " [--device <camera device>]"
248 << " [--scale <subsampling factor>]"
249 << " [--help] [-h]"
250 << std::endl;
251 return EXIT_SUCCESS;
252 }
253 }
254
255 // Instantiate the capture
256#if defined(VISP_HAVE_V4L2)
258 vpV4l2Grabber cap;
259 std::ostringstream device;
260 device << "/dev/video" << opt_device;
261 cap.setDevice(device.str());
262 cap.setScale(opt_scale);
263#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
264 cv::Mat frame;
265 cv::VideoCapture cap;
266 cap.open(opt_device);
267#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
268 int width = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_WIDTH));
269 int height = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_HEIGHT));
270 cap.set(cv::CAP_PROP_FRAME_WIDTH, width / opt_scale);
271 cap.set(cv::CAP_PROP_FRAME_HEIGHT, height / opt_scale);
272#else
273 int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
274 int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
275 cap.set(CV_CAP_PROP_FRAME_WIDTH, width / opt_scale);
276 cap.set(CV_CAP_PROP_FRAME_HEIGHT, height / opt_scale);
277#endif
278#endif
279
280 std::mutex mutex_capture;
281 std::mutex mutex_face;
282 vpRect face_bbox;
283 t_CaptureState capture_state = capture_waiting;
284 bool face_available = false;
285
286 // Start the threads
287 std::thread thread_capture(&captureFunction, std::ref(cap), std::ref(mutex_capture), std::ref(frame), std::ref(capture_state));
288 std::thread thread_display(&displayFunction, std::ref(mutex_capture), std::ref(mutex_face), std::ref(frame),
289 std::ref(capture_state), std::ref(face_bbox), std::ref(face_available));
290 std::thread thread_detection(&detectionFunction, std::ref(mutex_capture), std::ref(mutex_face), std::ref(frame),
291 std::ref(capture_state), std::ref(face_bbox), std::ref(opt_face_cascade_name), std::ref(face_available));
292
293 // Wait until thread ends up
294 thread_capture.join();
295 thread_display.join();
296 thread_detection.join();
297
298 return EXIT_SUCCESS;
299}
301
302#else
303int main()
304{
305#if !defined(VISP_HAVE_THREADS)
306 std::cout << "This tutorial needs std::threads that is missing." << std::endl;
307#endif
308#if !defined(HAVE_OPENCV_HIGHGUI)
309 std::cout << "This tutorial needs OpenCV highgui module that is missing." << std::endl;
310#endif
311#if !defined(HAVE_OPENCV_VIDEOIO)
312 std::cout << "This tutorial needs OpenCV videoio module that is missing." << std::endl;
313#endif
314#if !defined(HAVE_OPENCV_IMGPROC)
315 std::cout << "This tutorial needs OpenCV imgproc module that is missing." << std::endl;
316#endif
317#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION < 0x050000) && !defined(HAVE_OPENCV_OBJDETECT)
318 std::cout << "This tutorial needs OpenCV objdetect module that is missing." << std::endl;
319#endif
320#if defined(VISP_HAVE_OPENCV) && ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && !defined(HAVE_OPENCV_XOBJDETECT))
321 std::cout << "This tutorial needs OpenCV xobjdetect module that is missing." << std::endl;
322#endif
323
324 return EXIT_SUCCESS;
325}
326
327#endif
static const vpColor red
Definition vpColor.h:198
static const vpColor green
Definition vpColor.h:201
vpRect getBBox(size_t i) 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 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)
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
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void open(vpImage< unsigned char > &I)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void setDevice(const std::string &devname)
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 int wait(double t0, double t)
VISP_EXPORT double measureTimeSecond()