Visual Servoing Platform version 3.5.0
tutorial-face-detector-live-threaded.cpp
1
2#include <iostream>
3
4#include <visp3/core/vpImageConvert.h>
5#include <visp3/core/vpMutex.h>
6#include <visp3/core/vpThread.h>
7#include <visp3/core/vpTime.h>
8#include <visp3/detection/vpDetectorFace.h>
9#include <visp3/gui/vpDisplayGDI.h>
10#include <visp3/gui/vpDisplayX.h>
11#include <visp3/sensor/vpV4l2Grabber.h>
12
13#if (VISP_HAVE_OPENCV_VERSION >= 0x020200) && defined(VISP_HAVE_OPENCV_OBJDETECT) \
14 && (defined(VISP_HAVE_PTHREAD) || defined(_WIN32))
15
16#include <opencv2/highgui/highgui.hpp>
17
18// Shared vars
19typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
20t_CaptureState s_capture_state = capture_waiting;
21bool s_face_available = false;
22#if defined(VISP_HAVE_V4L2)
24#elif defined(VISP_HAVE_OPENCV)
25cv::Mat s_frame;
26#endif
27vpMutex s_mutex_capture;
28vpMutex s_mutex_face;
29vpRect s_face_bbox;
30
31vpThread::Return captureFunction(vpThread::Args args)
32{
33#if defined(VISP_HAVE_V4L2)
34 vpV4l2Grabber cap = *(static_cast<vpV4l2Grabber *>(args));
35#elif defined(VISP_HAVE_OPENCV)
36 cv::VideoCapture cap = *((cv::VideoCapture *)args);
37#endif
38
39// If the image is larger than 640 by 480, we subsample
40#if defined(VISP_HAVE_V4L2)
42#elif defined(VISP_HAVE_OPENCV)
43 cv::Mat frame_;
44#endif
45 bool stop_capture_ = false;
46
47 double start_time = vpTime::measureTimeSecond();
48 while ((vpTime::measureTimeSecond() - start_time) < 30 && !stop_capture_) {
49 // Capture in progress
50 cap >> frame_; // get a new frame from camera
51
52 // Update shared data
53 {
54 vpMutex::vpScopedLock lock(s_mutex_capture);
55 if (s_capture_state == capture_stopped)
56 stop_capture_ = true;
57 else
58 s_capture_state = capture_started;
59 s_frame = frame_;
60 }
61 }
62 {
63 vpMutex::vpScopedLock lock(s_mutex_capture);
64 s_capture_state = capture_stopped;
65 }
66
67 std::cout << "End of capture thread" << std::endl;
68 return 0;
69}
70
71vpThread::Return displayFunction(vpThread::Args args)
72{
73 (void)args; // Avoid warning: unused parameter args
75
76 t_CaptureState capture_state_;
77 bool display_initialized_ = false;
78 bool face_available_ = false;
79 vpRect face_bbox_;
80#if defined(VISP_HAVE_X11)
81 vpDisplayX *d_ = NULL;
82#elif defined(VISP_HAVE_GDI)
83 vpDisplayGDI *d_ = NULL;
84#endif
85
86 do {
87 s_mutex_capture.lock();
88 capture_state_ = s_capture_state;
89 s_mutex_capture.unlock();
90
91 // Check if a frame is available
92 if (capture_state_ == capture_started) {
93 // Get the frame and convert it to a ViSP image used by the display
94 // class
95 {
96 vpMutex::vpScopedLock lock(s_mutex_capture);
97#if defined(VISP_HAVE_V4L2)
98 I_ = s_frame;
99#elif defined(VISP_HAVE_OPENCV)
100 vpImageConvert::convert(s_frame, I_);
101#endif
102 }
103
104 // Check if we need to initialize the display with the first frame
105 if (!display_initialized_) {
106// Initialize the display
107#if defined(VISP_HAVE_X11)
108 d_ = new vpDisplayX(I_);
109 display_initialized_ = true;
110#elif defined(VISP_HAVE_GDI)
111 d_ = new vpDisplayGDI(I_);
112 display_initialized_ = true;
113#endif
114 }
115
116 // Display the image
118
119 // Check if a face was detected
120 {
121 vpMutex::vpScopedLock lock(s_mutex_face);
122 face_available_ = s_face_available;
123 face_bbox_ = s_face_bbox;
124 }
125 if (face_available_) {
126 // Access to the face bounding box to display it
127 vpDisplay::displayRectangle(I_, face_bbox_, vpColor::green, false, 4);
128 face_available_ = false;
129 }
130
131 // Trigger end of acquisition with a mouse click
132 vpDisplay::displayText(I_, 10, 10, "Click to exit...", vpColor::red);
133 if (vpDisplay::getClick(I_, false)) {
134 vpMutex::vpScopedLock lock(s_mutex_capture);
135 s_capture_state = capture_stopped;
136 }
137
138 // Update the display
140 } else {
141 vpTime::wait(2); // Sleep 2ms
142 }
143 } while (capture_state_ != capture_stopped);
144
145#if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
146 delete d_;
147#endif
148
149 std::cout << "End of display thread" << std::endl;
150 return 0;
151}
152
154vpThread::Return detectionFunction(vpThread::Args args)
155{
156 std::string opt_face_cascade_name = *((std::string *)args);
157
158 vpDetectorFace face_detector_;
159 face_detector_.setCascadeClassifierFile(opt_face_cascade_name);
160
161 t_CaptureState capture_state_;
162#if defined(VISP_HAVE_V4L2)
164#elif defined(VISP_HAVE_OPENCV)
165 cv::Mat frame_;
166#endif
167 do {
168 s_mutex_capture.lock();
169 capture_state_ = s_capture_state;
170 s_mutex_capture.unlock();
171
172 // Check if a frame is available
173 if (capture_state_ == capture_started) {
174 // Backup the frame
175 {
176 vpMutex::vpScopedLock lock(s_mutex_capture);
177 frame_ = s_frame;
178 }
179
180 // Detect faces
181 bool face_found_ = face_detector_.detect(frame_);
182 if (face_found_) {
183 vpMutex::vpScopedLock lock(s_mutex_face);
184 s_face_available = true;
185 s_face_bbox = face_detector_.getBBox(0); // Get largest face bounding box
186 }
187 } else {
188 vpTime::wait(2); // Sleep 2ms
189 }
190 } while (capture_state_ != capture_stopped);
191 std::cout << "End of face detection thread" << std::endl;
192
193 return 0;
194}
196
198int main(int argc, const char *argv[])
199{
200 std::string opt_face_cascade_name = "./haarcascade_frontalface_alt.xml";
201 unsigned int opt_device = 0;
202 unsigned int opt_scale = 2; // Default value is 2 in the constructor. Turn
203 // it to 1 to avoid subsampling
204
205 for (int i = 0; i < argc; i++) {
206 if (std::string(argv[i]) == "--haar")
207 opt_face_cascade_name = std::string(argv[i + 1]);
208 else if (std::string(argv[i]) == "--device")
209 opt_device = (unsigned int)atoi(argv[i + 1]);
210 else if (std::string(argv[i]) == "--scale")
211 opt_scale = (unsigned int)atoi(argv[i + 1]);
212 else if (std::string(argv[i]) == "--help") {
213 std::cout << "Usage: " << argv[0]
214 << " [--haar <haarcascade xml filename>] [--device <camera "
215 "device>] [--scale <subsampling factor>] [--help]"
216 << std::endl;
217 return 0;
218 }
219 }
220
221// Instanciate the capture
222#if defined(VISP_HAVE_V4L2)
223 vpV4l2Grabber cap;
224 std::ostringstream device;
225 device << "/dev/video" << opt_device;
226 cap.setDevice(device.str());
227 cap.setScale(opt_scale);
228#elif defined(VISP_HAVE_OPENCV)
229 cv::VideoCapture cap;
230 cap.open(opt_device);
231#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
232 int width = (int)cap.get(cv::CAP_PROP_FRAME_WIDTH);
233 int height = (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT);
234 cap.set(cv::CAP_PROP_FRAME_WIDTH, width / opt_scale);
235 cap.set(cv::CAP_PROP_FRAME_HEIGHT, height / opt_scale);
236#else
237 int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
238 int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
239 cap.set(CV_CAP_PROP_FRAME_WIDTH, width / opt_scale);
240 cap.set(CV_CAP_PROP_FRAME_HEIGHT, height / opt_scale);
241#endif
242#endif
243
244 // Start the threads
245 vpThread thread_capture((vpThread::Fn)captureFunction, (vpThread::Args)&cap);
246 vpThread thread_display((vpThread::Fn)displayFunction);
247 vpThread thread_detection((vpThread::Fn)detectionFunction, (vpThread::Args)&opt_face_cascade_name);
248
249 // Wait until thread ends up
250 thread_capture.join();
251 thread_display.join();
252 thread_detection.join();
253
254 return 0;
255}
257
258#else
259int main()
260{
261#ifndef VISP_HAVE_OPENCV
262 std::cout << "You should install OpenCV to make this example working..." << std::endl;
263#elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
264 std::cout << "You should enable pthread usage and rebuild ViSP..." << std::endl;
265#else
266 std::cout << "Multi-threading seems not supported on this platform" << std::endl;
267#endif
268}
269
270#endif
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
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 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)
Class that allows protection by mutex.
Definition: vpMutex.h:171
void unlock()
Definition: vpMutex.h:111
void lock()
Definition: vpMutex.h:95
Defines a rectangle in the plane.
Definition: vpRect.h:80
void * Return
Definition: vpThread.h:78
void *(* Fn)(Args)
Definition: vpThread.h:79
void * Args
Definition: vpThread.h:77
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)
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeSecond()