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_OPENCV_OBJDETECT) \ 14 && (defined(VISP_HAVE_PTHREAD) || defined(_WIN32)) 16 #include <opencv2/highgui/highgui.hpp> 19 typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
20 t_CaptureState s_capture_state = capture_waiting;
21 bool s_face_available =
false;
22 #if defined(VISP_HAVE_V4L2) 24 #elif defined(VISP_HAVE_OPENCV) 33 #if defined(VISP_HAVE_V4L2) 35 #elif defined(VISP_HAVE_OPENCV) 36 cv::VideoCapture cap = *((cv::VideoCapture *)args);
40 #if defined(VISP_HAVE_V4L2) 42 #elif defined(VISP_HAVE_OPENCV) 45 bool stop_capture_ =
false;
55 if (s_capture_state == capture_stopped)
58 s_capture_state = capture_started;
64 s_capture_state = capture_stopped;
67 std::cout <<
"End of capture thread" << std::endl;
76 t_CaptureState capture_state_;
77 bool display_initialized_ =
false;
78 bool face_available_ =
false;
80 #if defined(VISP_HAVE_X11) 82 #elif defined(VISP_HAVE_GDI) 87 s_mutex_capture.
lock();
88 capture_state_ = s_capture_state;
92 if (capture_state_ == capture_started) {
97 #if defined(VISP_HAVE_V4L2) 99 #elif defined(VISP_HAVE_OPENCV) 105 if (!display_initialized_) {
107 #if defined(VISP_HAVE_X11) 109 display_initialized_ =
true;
110 #elif defined(VISP_HAVE_GDI) 112 display_initialized_ =
true;
122 face_available_ = s_face_available;
123 face_bbox_ = s_face_bbox;
125 if (face_available_) {
128 face_available_ =
false;
135 s_capture_state = capture_stopped;
143 }
while (capture_state_ != capture_stopped);
145 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) 149 std::cout <<
"End of display thread" << std::endl;
156 std::string opt_face_cascade_name = *((std::string *)args);
158 vpDetectorFace face_detector_;
159 face_detector_.setCascadeClassifierFile(opt_face_cascade_name);
161 t_CaptureState capture_state_;
162 #if defined(VISP_HAVE_V4L2) 164 #elif defined(VISP_HAVE_OPENCV) 168 s_mutex_capture.
lock();
169 capture_state_ = s_capture_state;
173 if (capture_state_ == capture_started) {
181 bool face_found_ = face_detector_.detect(frame_);
184 s_face_available =
true;
185 s_face_bbox = face_detector_.getBBox(0);
190 }
while (capture_state_ != capture_stopped);
191 std::cout <<
"End of face detection thread" << std::endl;
198 int main(
int argc,
const char *argv[])
200 std::string opt_face_cascade_name =
"./haarcascade_frontalface_alt.xml";
201 unsigned int opt_device = 0;
202 unsigned int opt_scale = 2;
205 for (
int i = 0; i < argc; i++) {
206 if (std::string(argv[i]) ==
"--haar")
207 opt_face_cascade_name = std::string(argv[i + 1]);
208 else if (std::string(argv[i]) ==
"--device")
209 opt_device = (
unsigned int)atoi(argv[i + 1]);
210 else if (std::string(argv[i]) ==
"--scale")
211 opt_scale = (
unsigned int)atoi(argv[i + 1]);
212 else if (std::string(argv[i]) ==
"--help") {
213 std::cout <<
"Usage: " << argv[0]
214 <<
" [--haar <haarcascade xml filename>] [--device <camera " 215 "device>] [--scale <subsampling factor>] [--help]" 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 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)
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.