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