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 defined(HAVE_OPENCV_OBJDETECT) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEOIO) && (defined(VISP_HAVE_PTHREAD) || defined(_WIN32))
15#include <opencv2/videoio.hpp>
18typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
19t_CaptureState s_capture_state = capture_waiting;
20bool 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(HAVE_OPENCV_VIDEOIO)
35 cv::VideoCapture cap = *((cv::VideoCapture *)args);
39#if defined(VISP_HAVE_V4L2)
41#elif defined(HAVE_OPENCV_VIDEOIO)
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;
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);
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);
191 }
while (capture_state_ != capture_stopped);
192 std::cout <<
"End of face detection thread" << std::endl;
199int main(
int argc,
const char *argv [])
201 std::string opt_face_cascade_name =
"./haarcascade_frontalface_alt.xml";
202 unsigned int opt_device = 0;
203 unsigned int opt_scale = 2;
206 for (
int i = 0; i < argc; i++) {
207 if (std::string(argv[i]) ==
"--haar")
208 opt_face_cascade_name = std::string(argv[i + 1]);
209 else if (std::string(argv[i]) ==
"--device")
210 opt_device = (
unsigned int)atoi(argv[i + 1]);
211 else if (std::string(argv[i]) ==
"--scale")
212 opt_scale = (
unsigned int)atoi(argv[i + 1]);
213 else if (std::string(argv[i]) ==
"--help") {
214 std::cout <<
"Usage: " << argv[0]
215 <<
" [--haar <haarcascade xml filename>] [--device <camera "
216 "device>] [--scale <subsampling factor>] [--help]"
223#if defined(VISP_HAVE_V4L2)
225 std::ostringstream device;
226 device <<
"/dev/video" << opt_device;
229#elif defined(HAVE_OPENCV_VIDEOIO)
230 cv::VideoCapture cap;
231 cap.
open(opt_device);
232#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
233 int width = (int)cap.get(cv::CAP_PROP_FRAME_WIDTH);
234 int height = (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT);
235 cap.set(cv::CAP_PROP_FRAME_WIDTH, width / opt_scale);
236 cap.set(cv::CAP_PROP_FRAME_HEIGHT, height / opt_scale);
238 int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
239 int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
240 cap.set(CV_CAP_PROP_FRAME_WIDTH, width / opt_scale);
241 cap.set(CV_CAP_PROP_FRAME_HEIGHT, height / opt_scale);
251 thread_capture.join();
252 thread_display.join();
253 thread_detection.join();
262#ifndef VISP_HAVE_OPENCV
263 std::cout <<
"You should install OpenCV to make this example working..." << std::endl;
264#elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
265 std::cout <<
"You should enable pthread usage and rebuild ViSP..." << std::endl;
267 std::cout <<
"Multi-threading seems not supported on this platform" << std::endl;
static const vpColor green
vpRect getBBox(size_t i) const
bool detect(const vpImage< unsigned char > &I)
void setCascadeClassifierFile(const std::string &filename)
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)
Definition of the vpImage class member functions.
Class that allows protection by mutex.
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()