5#include <visp3/core/vpConfig.h>
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))))
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>
30#if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
31#include <opencv2/highgui/highgui.hpp>
32#elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
33#include <opencv2/videoio/videoio.hpp>
36#ifdef ENABLE_VISP_NAMESPACE
41typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
43#if defined(VISP_HAVE_V4L2)
47#elif defined(HAVE_OPENCV_VIDEOIO)
48void captureFunction(cv::VideoCapture &cap, std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state);
50void captureFunction(cv::VideoCapture &cap, std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state)
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))
59 bool stop_capture_ =
false;
68 std::lock_guard<std::mutex> lock(mutex_capture);
69 if (capture_state == capture_stopped)
72 capture_state = capture_started;
77 std::lock_guard<std::mutex> lock(mutex_capture);
78 capture_state = capture_stopped;
81 std::cout <<
"End of capture thread" << std::endl;
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);
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);
91void displayFunction(std::mutex &mutex_capture, std::mutex &mutex_face, cv::Mat &frame, t_CaptureState &capture_state,
vpRect &face_bbox,
bool &face_available)
96 t_CaptureState capture_state_;
97 bool display_initialized_ =
false;
98 bool face_available_ =
false;
100#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
101 std::shared_ptr<vpDisplay> display;
107 mutex_capture.lock();
108 capture_state_ = capture_state;
109 mutex_capture.unlock();
112 if (capture_state_ == capture_started) {
116 std::lock_guard<std::mutex> lock(mutex_capture);
117#if defined(VISP_HAVE_V4L2)
119#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
125 if (!display_initialized_) {
127#if defined(VISP_HAVE_DISPLAY)
128#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
133 display_initialized_ =
true;
143 std::lock_guard<std::mutex> lock(mutex_face);
144 face_available_ = face_available;
145 face_bbox_ = face_bbox;
147 if (face_available_) {
150 face_available_ =
false;
156 std::lock_guard<std::mutex> lock(mutex_capture);
157 capture_state = capture_stopped;
166 }
while (capture_state_ != capture_stopped);
168#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
169 if (display !=
nullptr) {
174 std::cout <<
"End of display thread" << std::endl;
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);
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);
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)
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))
198 mutex_capture.lock();
199 capture_state_ = capture_state;
200 mutex_capture.unlock();
203 if (capture_state_ == capture_started) {
206 std::lock_guard<std::mutex> lock(mutex_capture);
211 bool face_found_ = face_detector_.
detect(frame_);
213 std::lock_guard<std::mutex> lock(mutex_face);
214 face_available =
true;
215 face_bbox = face_detector_.
getBBox(0);
221 }
while (capture_state_ != capture_stopped);
222 std::cout <<
"End of face detection thread" << std::endl;
227int main(
int argc,
const char *argv[])
229 std::string opt_face_cascade_name =
"./haarcascade_frontalface_alt.xml";
230 unsigned int opt_device = 0;
231 unsigned int opt_scale = 2;
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]);
238 else if (std::string(argv[i]) ==
"--device" && i + 1 < argc) {
239 opt_device =
static_cast<unsigned int>(atoi(argv[++i]));
241 else if (std::string(argv[i]) ==
"--scale" && i + 1 < argc) {
242 opt_scale =
static_cast<unsigned int>(atoi(argv[++i]));
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>]"
256#if defined(VISP_HAVE_V4L2)
259 std::ostringstream device;
260 device <<
"/dev/video" << opt_device;
263#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
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);
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);
280 std::mutex mutex_capture;
281 std::mutex mutex_face;
283 t_CaptureState capture_state = capture_waiting;
284 bool face_available =
false;
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));
294 thread_capture.join();
295 thread_display.join();
296 thread_detection.join();
305#if !defined(VISP_HAVE_THREADS)
306 std::cout <<
"This tutorial needs std::threads that is missing." << std::endl;
308#if !defined(HAVE_OPENCV_HIGHGUI)
309 std::cout <<
"This tutorial needs OpenCV highgui module that is missing." << std::endl;
311#if !defined(HAVE_OPENCV_VIDEOIO)
312 std::cout <<
"This tutorial needs OpenCV videoio module that is missing." << std::endl;
314#if !defined(HAVE_OPENCV_IMGPROC)
315 std::cout <<
"This tutorial needs OpenCV imgproc module that is missing." << std::endl;
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;
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;
static const vpColor green
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.
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.
Defines a rectangle in the plane.
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()