ViSP  2.10.0
tutorial-face-detector-live.cpp
1 #include <visp/vpDisplayGDI.h>
3 #include <visp/vpDisplayOpenCV.h>
4 #include <visp/vpDisplayX.h>
5 #include <visp/vpDetectorFace.h>
6 #include <visp/vpV4l2Grabber.h>
7 
8 int main(int argc, const char* argv[])
9 {
10 #if (VISP_HAVE_OPENCV_VERSION >= 0x020200)
11  try {
12  std::string opt_face_cascade_name = "./haarcascade_frontalface_alt.xml";
13 
14  int opt_device = 0;
15 
16  for (int i=0; i<argc; i++) {
17  if (std::string(argv[i]) == "--haar")
18  opt_face_cascade_name = std::string(argv[i+1]);
19  else if (std::string(argv[i]) == "--device")
20  opt_device = atoi(argv[i+1]);
21  else if (std::string(argv[i]) == "--help") {
22  std::cout << "Usage: " << argv[0] << " [--haar <haarcascade xml filename>] [--device <camera device>] [--help]" << std::endl;
23  return 0;
24  }
25  }
26 
27  vpImage<unsigned char> I; // for gray images
28 
30 #if defined(VISP_HAVE_V4L2)
31  vpV4l2Grabber g;
32  std::ostringstream device;
33  device << "/dev/video" << opt_device;
34  g.setDevice(device.str());
35  g.setScale(2);
36  g.acquire(I);
37 #elif defined(VISP_HAVE_OPENCV)
38  cv::VideoCapture cap(opt_device); // open the default camera
39  if(!cap.isOpened()) { // check if we succeeded
40  std::cout << "Failed to open the camera" << std::endl;
41  return -1;
42  }
43  cv::Mat frame;
44  cap >> frame; // get a new frame from camera
45  vpImageConvert::convert(frame, I);
46 #endif
47 
49 #if defined(VISP_HAVE_X11)
50  vpDisplayX d(I);
51 #elif defined(VISP_HAVE_GDI)
52  vpDisplayGDI d(I);
53 #elif defined(VISP_HAVE_OPENCV)
54  vpDisplayOpenCV d(I);
55 #endif
56  vpDisplay::setTitle(I, "ViSP viewer");
57 
58  vpDetectorFace face_detector;
59  face_detector.setCascadeClassifierFile(opt_face_cascade_name);
60 
61  while(1) {
62  double t = vpTime::measureTimeMs();
64 #if defined(VISP_HAVE_V4L2)
65  g.acquire(I);
66 #else
67  cap >> frame; // get a new frame from camera
68  vpImageConvert::convert(frame, I);
69 #endif
70 
73  bool face_found = face_detector.detect(I);
74 
75  if (face_found) {
76  std::ostringstream text;
77  text << "Found " << face_detector.getNbObjects() << " face(s)";
78  vpDisplay::displayText(I, 10, 10, text.str(), vpColor::red);
79  for(size_t i=0; i < face_detector.getNbObjects(); i++) {
80  std::vector<vpImagePoint> p = face_detector.getPolygon(i);
81  vpRect bbox = face_detector.getBBox(i);
82  vpDisplay::displayRectangle(I, bbox, vpColor::green, false, 4);
83  vpDisplay::displayText(I, (int)bbox.getTop()-10, (int)bbox.getLeft(), "Message: \"" + face_detector.getMessage(i) + "\"", vpColor::red);
84  }
85  }
86  vpDisplay::displayText(I, (int)I.getHeight()-25, 10, "Click to quit...", vpColor::red);
88  if (vpDisplay::getClick(I, false)) // a click to exit
89  break;
90 
91  std::cout << "Loop time: " << vpTime::measureTimeMs() - t << " ms" << std::endl;
92  }
93  }
94  catch(vpException &e) {
95  std::cout << e.getMessage() << std::endl;
96  }
97 #else
98  (void)argc;
99  (void)argv;
100 #endif
101 }
const char * getMessage(void)
double getTop() const
Definition: vpRect.h:180
void acquire(vpImage< unsigned char > &I)
void setCascadeClassifierFile(const std::string &filename)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay.cpp:887
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void setDevice(const std::string &devname)
error that can be emited by ViSP classes.
Definition: vpException.h:76
static double measureTimeMs()
Definition: vpTime.cpp:86
size_t getNbObjects() const
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
static const vpColor red
Definition: vpColor.h:167
bool detect(const vpImage< unsigned char > &I)
std::vector< std::vector< vpImagePoint > > & getPolygon()
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void setTitle(const char *title)=0
virtual void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)=0
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
Class for the Video4Linux2 video device.
std::string & getMessage(size_t i)
unsigned int getHeight() const
Definition: vpImage.h:152
Defines a rectangle in the plane.
Definition: vpRect.h:85
virtual bool getClick(bool blocking=true)=0
vpRect getBBox(size_t i) const
double getLeft() const
Definition: vpRect.h:161