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/detection/vpDetectorFace.h> 9 #include <visp3/gui/vpDisplayGDI.h> 10 #include <visp3/gui/vpDisplayX.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> 18 typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
19 t_CaptureState s_capture_state = capture_waiting;
20 bool s_face_available =
false;
21 #if defined(VISP_HAVE_V4L2) 23 #elif defined(VISP_HAVE_OPENCV) 32 #if defined(VISP_HAVE_V4L2) 34 #elif defined(VISP_HAVE_OPENCV) 35 cv::VideoCapture cap = *((cv::VideoCapture *)args);
39 #if defined(VISP_HAVE_V4L2) 41 #elif defined(VISP_HAVE_OPENCV) 44 bool stop_capture_ =
false;
54 if (s_capture_state == capture_stopped)
57 s_capture_state = capture_started;
63 s_capture_state = capture_stopped;
66 std::cout <<
"End of capture thread" << std::endl;
75 t_CaptureState capture_state_;
76 bool display_initialized_ =
false;
77 bool face_available_ =
false;
79 #if defined(VISP_HAVE_X11) 81 #elif defined(VISP_HAVE_GDI) 86 s_mutex_capture.
lock();
87 capture_state_ = s_capture_state;
91 if (capture_state_ == capture_started) {
96 #if defined(VISP_HAVE_V4L2) 98 #elif defined(VISP_HAVE_OPENCV) 104 if (!display_initialized_) {
106 #if defined(VISP_HAVE_X11) 108 display_initialized_ =
true;
109 #elif defined(VISP_HAVE_GDI) 111 display_initialized_ =
true;
121 face_available_ = s_face_available;
122 face_bbox_ = s_face_bbox;
124 if (face_available_) {
127 face_available_ =
false;
134 s_capture_state = capture_stopped;
142 }
while (capture_state_ != capture_stopped);
144 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) 148 std::cout <<
"End of display thread" << std::endl;
155 std::string opt_face_cascade_name = *((std::string *)args);
160 t_CaptureState capture_state_;
161 #if defined(VISP_HAVE_V4L2) 163 #elif defined(VISP_HAVE_OPENCV) 167 s_mutex_capture.
lock();
168 capture_state_ = s_capture_state;
172 if (capture_state_ == capture_started) {
180 bool face_found_ = face_detector_.
detect(frame_);
183 s_face_available =
true;
184 s_face_bbox = face_detector_.
getBBox(0);
189 }
while (capture_state_ != capture_stopped);
190 std::cout <<
"End of face detection thread" << std::endl;
197 int main(
int argc,
const char *argv[])
199 std::string opt_face_cascade_name =
"./haarcascade_frontalface_alt.xml";
200 unsigned int opt_device = 0;
201 unsigned int opt_scale = 2;
204 for (
int i = 0; i < argc; i++) {
205 if (std::string(argv[i]) ==
"--haar")
206 opt_face_cascade_name = std::string(argv[i + 1]);
207 else if (std::string(argv[i]) ==
"--device")
208 opt_device = (
unsigned int)atoi(argv[i + 1]);
209 else if (std::string(argv[i]) ==
"--scale")
210 opt_scale = (
unsigned int)atoi(argv[i + 1]);
211 else if (std::string(argv[i]) ==
"--help") {
212 std::cout <<
"Usage: " << argv[0]
213 <<
" [--haar <haarcascade xml filename>] [--device <camera " 214 "device>] [--scale <subsampling factor>] [--help]" 221 #if defined(VISP_HAVE_V4L2) 223 std::ostringstream device;
224 device <<
"/dev/video" << opt_device;
227 #elif defined(VISP_HAVE_OPENCV) 228 cv::VideoCapture cap;
229 cap.
open(opt_device);
230 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000) 231 int width = (int)cap.get(cv::CAP_PROP_FRAME_WIDTH);
232 int height = (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT);
233 cap.set(cv::CAP_PROP_FRAME_WIDTH, width / opt_scale);
234 cap.set(cv::CAP_PROP_FRAME_HEIGHT, height / opt_scale);
236 int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
237 int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
238 cap.set(CV_CAP_PROP_FRAME_WIDTH, width / opt_scale);
239 cap.set(CV_CAP_PROP_FRAME_HEIGHT, height / opt_scale);
249 thread_capture.join();
250 thread_display.join();
251 thread_detection.join();
260 #ifndef VISP_HAVE_OPENCV 261 std::cout <<
"You should install OpenCV to make this example working..." << std::endl;
262 #elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 263 std::cout <<
"You should enable pthread usage and rebuild ViSP..." << std::endl;
265 std::cout <<
"Multi-threading seems not supported on this platform" << std::endl;
Class that allows protection by mutex.
vpRect getBBox(size_t i) const
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.