4 #include <visp3/core/vpImageConvert.h>
5 #include <visp3/core/vpTime.h>
6 #include <visp3/detection/vpDetectorFace.h>
7 #include <visp3/gui/vpDisplayGDI.h>
8 #include <visp3/gui/vpDisplayX.h>
9 #include <visp3/sensor/vpV4l2Grabber.h>
11 #if defined(HAVE_OPENCV_OBJDETECT) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) \
12 && defined(HAVE_OPENCV_VIDEOIO) && defined(VISP_HAVE_THREADS)
17 #include <opencv2/videoio.hpp>
20 typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
22 #if defined(VISP_HAVE_V4L2)
24 #elif defined(HAVE_OPENCV_VIDEOIO)
25 void captureFunction(cv::VideoCapture &cap, std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state)
29 #if defined(VISP_HAVE_V4L2)
31 #elif defined(HAVE_OPENCV_VIDEOIO)
34 bool stop_capture_ =
false;
43 std::lock_guard<std::mutex> lock(mutex_capture);
44 if (capture_state == capture_stopped)
47 capture_state = capture_started;
52 std::lock_guard<std::mutex> lock(mutex_capture);
53 capture_state = capture_stopped;
56 std::cout <<
"End of capture thread" << std::endl;
59 #if defined(VISP_HAVE_V4L2)
60 void displayFunction(std::mutex &mutex_capture, std::mutex &mutex_face,
vpImage<unsigned char> &frame, t_CaptureState &capture_state,
vpRect &face_bbox,
bool &face_available)
61 #elif defined(HAVE_OPENCV_VIDEOIO)
62 void displayFunction(std::mutex &mutex_capture, std::mutex &mutex_face, cv::Mat &frame, t_CaptureState &capture_state,
vpRect &face_bbox,
bool &face_available)
67 t_CaptureState capture_state_;
68 bool display_initialized_ =
false;
69 bool face_available_ =
false;
71 #if defined(VISP_HAVE_X11)
73 #elif defined(VISP_HAVE_GDI)
79 capture_state_ = capture_state;
80 mutex_capture.unlock();
83 if (capture_state_ == capture_started) {
87 std::lock_guard<std::mutex> lock(mutex_capture);
88 #if defined(VISP_HAVE_V4L2)
90 #elif defined(VISP_HAVE_OPENCV)
96 if (!display_initialized_) {
98 #if defined(VISP_HAVE_X11)
100 display_initialized_ =
true;
101 #elif defined(VISP_HAVE_GDI)
103 display_initialized_ =
true;
113 std::lock_guard<std::mutex> lock(mutex_face);
114 face_available_ = face_available;
115 face_bbox_ = face_bbox;
117 if (face_available_) {
120 face_available_ =
false;
126 std::lock_guard<std::mutex> lock(mutex_capture);
127 capture_state = capture_stopped;
136 }
while (capture_state_ != capture_stopped);
138 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
142 std::cout <<
"End of display thread" << std::endl;
146 #if defined(VISP_HAVE_V4L2)
147 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)
148 #elif defined(HAVE_OPENCV_VIDEOIO)
149 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)
155 t_CaptureState capture_state_;
156 #if defined(VISP_HAVE_V4L2)
158 #elif defined(VISP_HAVE_OPENCV)
162 mutex_capture.lock();
163 capture_state_ = capture_state;
164 mutex_capture.unlock();
167 if (capture_state_ == capture_started) {
170 std::lock_guard<std::mutex> lock(mutex_capture);
175 bool face_found_ = face_detector_.
detect(frame_);
177 std::lock_guard<std::mutex> lock(mutex_face);
178 face_available =
true;
179 face_bbox = face_detector_.
getBBox(0);
185 }
while (capture_state_ != capture_stopped);
186 std::cout <<
"End of face detection thread" << std::endl;
191 int main(
int argc,
const char *argv[])
193 std::string opt_face_cascade_name =
"./haarcascade_frontalface_alt.xml";
194 unsigned int opt_device = 0;
195 unsigned int opt_scale = 2;
198 for (
int i = 0; i < argc; i++) {
199 if (std::string(argv[i]) ==
"--haar")
200 opt_face_cascade_name = std::string(argv[i + 1]);
201 else if (std::string(argv[i]) ==
"--device")
202 opt_device = (
unsigned int)atoi(argv[i + 1]);
203 else if (std::string(argv[i]) ==
"--scale")
204 opt_scale = (
unsigned int)atoi(argv[i + 1]);
205 else if (std::string(argv[i]) ==
"--help") {
206 std::cout <<
"Usage: " << argv[0]
207 <<
" [--haar <haarcascade xml filename>] [--device <camera "
208 "device>] [--scale <subsampling factor>] [--help]"
215 #if defined(VISP_HAVE_V4L2)
218 std::ostringstream device;
219 device <<
"/dev/video" << opt_device;
222 #elif defined(HAVE_OPENCV_VIDEOIO)
224 cv::VideoCapture cap;
225 cap.
open(opt_device);
226 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
227 int width = (int)cap.get(cv::CAP_PROP_FRAME_WIDTH);
228 int height = (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT);
229 cap.set(cv::CAP_PROP_FRAME_WIDTH, width / opt_scale);
230 cap.set(cv::CAP_PROP_FRAME_HEIGHT, height / opt_scale);
232 int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
233 int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
234 cap.set(CV_CAP_PROP_FRAME_WIDTH, width / opt_scale);
235 cap.set(CV_CAP_PROP_FRAME_HEIGHT, height / opt_scale);
239 std::mutex mutex_capture;
240 std::mutex mutex_face;
242 t_CaptureState capture_state = capture_waiting;
243 bool face_available =
false;
246 std::thread thread_capture(&captureFunction, std::ref(cap), std::ref(mutex_capture), std::ref(frame), std::ref(capture_state));
247 std::thread thread_display(&displayFunction, std::ref(mutex_capture), std::ref(mutex_face), std::ref(frame),
248 std::ref(capture_state), std::ref(face_bbox), std::ref(face_available));
249 std::thread thread_detection(&detectionFunction, std::ref(mutex_capture), std::ref(mutex_face), std::ref(frame),
250 std::ref(capture_state), std::ref(face_bbox), std::ref(opt_face_cascade_name), std::ref(face_available));
253 thread_capture.join();
254 thread_display.join();
255 thread_detection.join();
264 #ifndef VISP_HAVE_OPENCV
265 std::cout <<
"You should install OpenCV to make this example working..." << std::endl;
266 #elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
267 std::cout <<
"You should enable pthread usage and rebuild ViSP..." << std::endl;
269 std::cout <<
"Multi-threading seems not supported on this platform" << 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).
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
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()