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/vpDisplayFactory.h>
24 #include <visp3/sensor/vpV4l2Grabber.h>
26 #if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
27 #include <opencv2/highgui/highgui.hpp>
28 #elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
29 #include <opencv2/videoio/videoio.hpp>
32 #ifdef ENABLE_VISP_NAMESPACE
37 typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
39 #if defined(VISP_HAVE_V4L2)
41 #elif defined(HAVE_OPENCV_VIDEOIO)
42 void captureFunction(cv::VideoCapture &cap, std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state)
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))
51 bool stop_capture_ =
false;
60 std::lock_guard<std::mutex> lock(mutex_capture);
61 if (capture_state == capture_stopped)
64 capture_state = capture_started;
69 std::lock_guard<std::mutex> lock(mutex_capture);
70 capture_state = capture_stopped;
73 std::cout <<
"End of capture thread" << std::endl;
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)
84 t_CaptureState capture_state_;
85 bool display_initialized_ =
false;
86 bool face_available_ =
false;
88 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
89 std::shared_ptr<vpDisplay> display;
96 capture_state_ = capture_state;
97 mutex_capture.unlock();
100 if (capture_state_ == capture_started) {
104 std::lock_guard<std::mutex> lock(mutex_capture);
105 #if defined(VISP_HAVE_V4L2)
107 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
113 if (!display_initialized_) {
115 #if defined(VISP_HAVE_DISPLAY)
116 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
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 (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
157 if (display !=
nullptr) {
162 std::cout <<
"End of display thread" << std::endl;
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)
175 t_CaptureState capture_state_;
176 #if defined(VISP_HAVE_V4L2)
178 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
182 mutex_capture.lock();
183 capture_state_ = capture_state;
184 mutex_capture.unlock();
187 if (capture_state_ == capture_started) {
190 std::lock_guard<std::mutex> lock(mutex_capture);
195 bool face_found_ = face_detector_.
detect(frame_);
197 std::lock_guard<std::mutex> lock(mutex_face);
198 face_available =
true;
199 face_bbox = face_detector_.
getBBox(0);
205 }
while (capture_state_ != capture_stopped);
206 std::cout <<
"End of face detection thread" << std::endl;
211 int main(
int argc,
const char *argv[])
213 std::string opt_face_cascade_name =
"./haarcascade_frontalface_alt.xml";
214 unsigned int opt_device = 0;
215 unsigned int opt_scale = 2;
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]);
222 else if (std::string(argv[i]) ==
"--device" && i + 1 < argc) {
223 opt_device = (
unsigned int)atoi(argv[++i]);
225 else if (std::string(argv[i]) ==
"--scale" && i + 1 < argc) {
226 opt_scale = (
unsigned int)atoi(argv[++i]);
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>]"
240 #if defined(VISP_HAVE_V4L2)
243 std::ostringstream device;
244 device <<
"/dev/video" << opt_device;
247 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
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);
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);
264 std::mutex mutex_capture;
265 std::mutex mutex_face;
267 t_CaptureState capture_state = capture_waiting;
268 bool face_available =
false;
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));
278 thread_capture.join();
279 thread_display.join();
280 thread_detection.join();
289 #if !defined(VISP_HAVE_THREADS)
290 std::cout <<
"This tutorial needs std::threads that is missing." << std::endl;
292 #if !defined(HAVE_OPENCV_HIGHGUI)
293 std::cout <<
"This tutorial needs OpenCV highgui module that is missing." << std::endl;
295 #if !defined(HAVE_OPENCV_VIDEOIO)
296 std::cout <<
"This tutorial needs OpenCV videoio module that is missing." << std::endl;
298 #if !defined(HAVE_OPENCV_IMGPROC)
299 std::cout <<
"This tutorial needs OpenCV imgproc module that is missing." << std::endl;
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;
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;
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)
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()