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