4 #include <visp3/core/vpConfig.h>
5 #include <visp3/core/vpImageConvert.h>
6 #include <visp3/core/vpTime.h>
7 #include <visp3/detection/vpDetectorFace.h>
8 #include <visp3/gui/vpDisplayGDI.h>
9 #include <visp3/gui/vpDisplayX.h>
10 #include <visp3/sensor/vpV4l2Grabber.h>
12 #if defined(HAVE_OPENCV_OBJDETECT) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) \
13 && defined(HAVE_OPENCV_VIDEOIO) && defined(VISP_HAVE_THREADS)
18 #include <opencv2/videoio.hpp>
20 #ifdef ENABLE_VISP_NAMESPACE
25 typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
27 #if defined(VISP_HAVE_V4L2)
29 #elif defined(HAVE_OPENCV_VIDEOIO)
30 void captureFunction(cv::VideoCapture &cap, std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state)
34 #if defined(VISP_HAVE_V4L2)
36 #elif defined(HAVE_OPENCV_VIDEOIO)
39 bool stop_capture_ =
false;
48 std::lock_guard<std::mutex> lock(mutex_capture);
49 if (capture_state == capture_stopped)
52 capture_state = capture_started;
57 std::lock_guard<std::mutex> lock(mutex_capture);
58 capture_state = capture_stopped;
61 std::cout <<
"End of capture thread" << std::endl;
64 #if defined(VISP_HAVE_V4L2)
65 void displayFunction(std::mutex &mutex_capture, std::mutex &mutex_face,
vpImage<unsigned char> &frame, t_CaptureState &capture_state,
vpRect &face_bbox,
bool &face_available)
66 #elif defined(HAVE_OPENCV_VIDEOIO)
67 void displayFunction(std::mutex &mutex_capture, std::mutex &mutex_face, cv::Mat &frame, t_CaptureState &capture_state,
vpRect &face_bbox,
bool &face_available)
72 t_CaptureState capture_state_;
73 bool display_initialized_ =
false;
74 bool face_available_ =
false;
76 #if defined(VISP_HAVE_X11)
77 vpDisplayX *d_ =
nullptr;
78 #elif defined(VISP_HAVE_GDI)
84 capture_state_ = capture_state;
85 mutex_capture.unlock();
88 if (capture_state_ == capture_started) {
92 std::lock_guard<std::mutex> lock(mutex_capture);
93 #if defined(VISP_HAVE_V4L2)
95 #elif defined(VISP_HAVE_OPENCV)
101 if (!display_initialized_) {
103 #if defined(VISP_HAVE_X11)
104 d_ =
new vpDisplayX(I_);
105 display_initialized_ =
true;
106 #elif defined(VISP_HAVE_GDI)
108 display_initialized_ =
true;
118 std::lock_guard<std::mutex> lock(mutex_face);
119 face_available_ = face_available;
120 face_bbox_ = face_bbox;
122 if (face_available_) {
125 face_available_ =
false;
131 std::lock_guard<std::mutex> lock(mutex_capture);
132 capture_state = capture_stopped;
141 }
while (capture_state_ != capture_stopped);
143 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
147 std::cout <<
"End of display thread" << std::endl;
151 #if defined(VISP_HAVE_V4L2)
152 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)
153 #elif defined(HAVE_OPENCV_VIDEOIO)
154 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)
160 t_CaptureState capture_state_;
161 #if defined(VISP_HAVE_V4L2)
163 #elif defined(VISP_HAVE_OPENCV)
167 mutex_capture.lock();
168 capture_state_ = capture_state;
169 mutex_capture.unlock();
172 if (capture_state_ == capture_started) {
175 std::lock_guard<std::mutex> lock(mutex_capture);
180 bool face_found_ = face_detector_.
detect(frame_);
182 std::lock_guard<std::mutex> lock(mutex_face);
183 face_available =
true;
184 face_bbox = face_detector_.
getBBox(0);
190 }
while (capture_state_ != capture_stopped);
191 std::cout <<
"End of face detection thread" << std::endl;
196 int main(
int argc,
const char *argv[])
198 std::string opt_face_cascade_name =
"./haarcascade_frontalface_alt.xml";
199 unsigned int opt_device = 0;
200 unsigned int opt_scale = 2;
203 for (
int i = 0; i < argc; i++) {
204 if (std::string(argv[i]) ==
"--haar")
205 opt_face_cascade_name = std::string(argv[i + 1]);
206 else if (std::string(argv[i]) ==
"--device")
207 opt_device = (
unsigned int)atoi(argv[i + 1]);
208 else if (std::string(argv[i]) ==
"--scale")
209 opt_scale = (
unsigned int)atoi(argv[i + 1]);
210 else if (std::string(argv[i]) ==
"--help") {
211 std::cout <<
"Usage: " << argv[0]
212 <<
" [--haar <haarcascade xml filename>] [--device <camera "
213 "device>] [--scale <subsampling factor>] [--help]"
220 #if defined(VISP_HAVE_V4L2)
223 std::ostringstream device;
224 device <<
"/dev/video" << opt_device;
227 #elif defined(HAVE_OPENCV_VIDEOIO)
229 cv::VideoCapture cap;
230 cap.
open(opt_device);
231 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
232 int width = (int)cap.get(cv::CAP_PROP_FRAME_WIDTH);
233 int height = (int)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);
237 int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
238 int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
239 cap.set(CV_CAP_PROP_FRAME_WIDTH, width / opt_scale);
240 cap.set(CV_CAP_PROP_FRAME_HEIGHT, height / opt_scale);
244 std::mutex mutex_capture;
245 std::mutex mutex_face;
247 t_CaptureState capture_state = capture_waiting;
248 bool face_available =
false;
251 std::thread thread_capture(&captureFunction, std::ref(cap), std::ref(mutex_capture), std::ref(frame), std::ref(capture_state));
252 std::thread thread_display(&displayFunction, std::ref(mutex_capture), std::ref(mutex_face), std::ref(frame),
253 std::ref(capture_state), std::ref(face_bbox), std::ref(face_available));
254 std::thread thread_detection(&detectionFunction, std::ref(mutex_capture), std::ref(mutex_face), std::ref(frame),
255 std::ref(capture_state), std::ref(face_bbox), std::ref(opt_face_cascade_name), std::ref(face_available));
258 thread_capture.join();
259 thread_display.join();
260 thread_detection.join();
269 #ifndef VISP_HAVE_OPENCV
270 std::cout <<
"You should install OpenCV to make this example working..." << std::endl;
271 #elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
272 std::cout <<
"You should enable pthread usage and rebuild ViSP..." << std::endl;
274 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).
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()