4 #include <visp3/core/vpImageConvert.h>
5 #include <visp3/core/vpMutex.h>
6 #include <visp3/core/vpThread.h>
7 #include <visp3/core/vpTime.h>
8 #include <visp3/gui/vpDisplayX.h>
9 #include <visp3/gui/vpDisplayGDI.h>
10 #include <visp3/detection/vpDetectorFace.h>
11 #include <visp3/sensor/vpV4l2Grabber.h>
13 #if (VISP_HAVE_OPENCV_VERSION >= 0x020200) && (defined(VISP_HAVE_PTHREAD) || defined(_WIN32))
15 #include <opencv2/highgui/highgui.hpp>
23 t_CaptureState s_capture_state = capture_waiting;
24 bool s_face_available =
false;
25 #if defined(VISP_HAVE_V4L2)
27 #elif defined(VISP_HAVE_OPENCV)
36 #if defined(VISP_HAVE_V4L2)
38 #elif defined(VISP_HAVE_OPENCV)
39 cv::VideoCapture cap = *((cv::VideoCapture *) args);
43 #if defined(VISP_HAVE_V4L2)
45 #elif defined(VISP_HAVE_OPENCV)
48 bool stop_capture_ =
false;
58 if (s_capture_state == capture_stopped)
61 s_capture_state = capture_started;
67 s_capture_state = capture_stopped;
70 std::cout <<
"End of capture thread" << std::endl;
79 t_CaptureState capture_state_;
80 bool display_initialized_ =
false;
81 bool face_available_ =
false;
83 #if defined(VISP_HAVE_X11)
85 #elif defined(VISP_HAVE_GDI)
90 s_mutex_capture.
lock();
91 capture_state_ = s_capture_state;
95 if (capture_state_ == capture_started) {
99 #if defined(VISP_HAVE_V4L2)
101 #elif defined(VISP_HAVE_OPENCV)
107 if (! display_initialized_) {
109 #if defined(VISP_HAVE_X11)
111 display_initialized_ =
true;
112 #elif defined(VISP_HAVE_GDI)
114 display_initialized_ =
true;
124 face_available_ = s_face_available;
125 face_bbox_ = s_face_bbox;
127 if (face_available_) {
130 face_available_ =
false;
137 s_capture_state = capture_stopped;
146 }
while(capture_state_ != capture_stopped);
148 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
152 std::cout <<
"End of display thread" << std::endl;
159 std::string opt_face_cascade_name = *((std::string *) args);
164 t_CaptureState capture_state_;
165 #if defined(VISP_HAVE_V4L2)
167 #elif defined(VISP_HAVE_OPENCV)
171 s_mutex_capture.
lock();
172 capture_state_ = s_capture_state;
176 if (capture_state_ == capture_started) {
184 bool face_found_ = face_detector_.
detect(frame_);
187 s_face_available =
true;
188 s_face_bbox = face_detector_.
getBBox(0);
194 }
while(capture_state_ != capture_stopped);
195 std::cout <<
"End of face detection thread" << std::endl;
202 int main(
int argc,
const char* argv[])
204 std::string opt_face_cascade_name =
"./haarcascade_frontalface_alt.xml";
205 unsigned int opt_device = 0;
206 unsigned int opt_scale = 2;
208 for (
int i=0; i<argc; i++) {
209 if (std::string(argv[i]) ==
"--haar")
210 opt_face_cascade_name = std::string(argv[i+1]);
211 else if (std::string(argv[i]) ==
"--device")
212 opt_device = (
unsigned int)atoi(argv[i+1]);
213 else if (std::string(argv[i]) ==
"--scale")
214 opt_scale = (
unsigned int)atoi(argv[i+1]);
215 else if (std::string(argv[i]) ==
"--help") {
216 std::cout <<
"Usage: " << argv[0] <<
" [--haar <haarcascade xml filename>] [--device <camera device>] [--scale <subsampling factor>] [--help]" << std::endl;
222 #if defined(VISP_HAVE_V4L2)
224 std::ostringstream device;
225 device <<
"/dev/video" << opt_device;
228 #elif defined(VISP_HAVE_OPENCV)
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);
250 thread_capture.join();
251 thread_display.join();
252 thread_detection.join();
261 # ifndef VISP_HAVE_OPENCV
262 std::cout <<
"You should install OpenCV to make this example working..." << std::endl;
263 # elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
264 std::cout <<
"You should enable pthread usage and rebuild ViSP..." << std::endl;
266 std::cout <<
"Multi-threading seems not supported on this platform" << std::endl;
Class that allows protection by mutex.
VISP_EXPORT int wait(double t0, double t)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void setCascadeClassifierFile(const std::string &filename)
void open(vpImage< unsigned char > &I)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
VISP_EXPORT double measureTimeSecond()
Display for windows using GDI (available on any windows 32 platform).
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
void setDevice(const std::string &devname)
static const vpColor green
static void flush(const vpImage< unsigned char > &I)
bool detect(const vpImage< unsigned char > &I)
static void display(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)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
Defines a rectangle in the plane.
vpRect getBBox(size_t i) const