4 #include <visp3/core/vpConfig.h>
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))))
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>
27 #if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
28 #include <opencv2/highgui/highgui.hpp>
29 #elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
30 #include <opencv2/videoio/videoio.hpp>
33 #ifdef ENABLE_VISP_NAMESPACE
38 typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
40 #if defined(VISP_HAVE_V4L2)
42 #elif defined(HAVE_OPENCV_VIDEOIO)
43 void captureFunction(cv::VideoCapture &cap, std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state)
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))
52 bool stop_capture_ =
false;
61 std::lock_guard<std::mutex> lock(mutex_capture);
62 if (capture_state == capture_stopped)
65 capture_state = capture_started;
70 std::lock_guard<std::mutex> lock(mutex_capture);
71 capture_state = capture_stopped;
74 std::cout <<
"End of capture thread" << std::endl;
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)
85 t_CaptureState capture_state_;
86 bool display_initialized_ =
false;
87 bool face_available_ =
false;
89 #if defined(VISP_HAVE_X11)
90 vpDisplayX *d_ =
nullptr;
91 #elif defined(VISP_HAVE_GDI)
97 capture_state_ = capture_state;
98 mutex_capture.unlock();
101 if (capture_state_ == capture_started) {
105 std::lock_guard<std::mutex> lock(mutex_capture);
106 #if defined(VISP_HAVE_V4L2)
108 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
114 if (!display_initialized_) {
116 #if defined(VISP_HAVE_X11)
117 d_ =
new vpDisplayX(I_);
118 display_initialized_ =
true;
119 #elif defined(VISP_HAVE_GDI)
121 display_initialized_ =
true;
131 std::lock_guard<std::mutex> lock(mutex_face);
132 face_available_ = face_available;
133 face_bbox_ = face_bbox;
135 if (face_available_) {
138 face_available_ =
false;
144 std::lock_guard<std::mutex> lock(mutex_capture);
145 capture_state = capture_stopped;
154 }
while (capture_state_ != capture_stopped);
156 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
160 std::cout <<
"End of display thread" << std::endl;
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)
173 t_CaptureState capture_state_;
174 #if defined(VISP_HAVE_V4L2)
176 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
180 mutex_capture.lock();
181 capture_state_ = capture_state;
182 mutex_capture.unlock();
185 if (capture_state_ == capture_started) {
188 std::lock_guard<std::mutex> lock(mutex_capture);
193 bool face_found_ = face_detector_.
detect(frame_);
195 std::lock_guard<std::mutex> lock(mutex_face);
196 face_available =
true;
197 face_bbox = face_detector_.
getBBox(0);
203 }
while (capture_state_ != capture_stopped);
204 std::cout <<
"End of face detection thread" << std::endl;
209 int main(
int argc,
const char *argv[])
211 std::string opt_face_cascade_name =
"./haarcascade_frontalface_alt.xml";
212 unsigned int opt_device = 0;
213 unsigned int opt_scale = 2;
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]);
220 else if (std::string(argv[i]) ==
"--device" && i + 1 < argc) {
221 opt_device = (
unsigned int)atoi(argv[++i]);
223 else if (std::string(argv[i]) ==
"--scale" && i + 1 < argc) {
224 opt_scale = (
unsigned int)atoi(argv[++i]);
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>]"
238 #if defined(VISP_HAVE_V4L2)
241 std::ostringstream device;
242 device <<
"/dev/video" << opt_device;
245 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
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);
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);
262 std::mutex mutex_capture;
263 std::mutex mutex_face;
265 t_CaptureState capture_state = capture_waiting;
266 bool face_available =
false;
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));
276 thread_capture.join();
277 thread_display.join();
278 thread_detection.join();
287 #if !defined(VISP_HAVE_THREADS)
288 std::cout <<
"This tutorial needs std::threads that is missing." << std::endl;
290 #if !defined(HAVE_OPENCV_HIGHGUI)
291 std::cout <<
"This tutorial needs OpenCV highgui module that is missing." << std::endl;
293 #if !defined(HAVE_OPENCV_VIDEOIO)
294 std::cout <<
"This tutorial needs OpenCV videoio module that is missing." << std::endl;
296 #if !defined(HAVE_OPENCV_IMGPROC)
297 std::cout <<
"This tutorial needs OpenCV imgproc module that is missing." << std::endl;
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;
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;
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
Display for windows using GDI (available on any windows 32 platform).
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.
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()