1 //! \example tutorial-face-detector.cpp
2 #include <visp3/gui/vpDisplayGDI.h>
3 #include <visp3/gui/vpDisplayOpenCV.h>
4 #include <visp3/gui/vpDisplayX.h>
5 //! [Include]
6 #include <visp3/detection/vpDetectorFace.h>
7 //! [Include]
8 #include <visp3/io/vpVideoReader.h>
9 
main(int argc,const char * argv[])10 int main(int argc, const char *argv[])
11 {
12 //! [Macro defined]
13 #if (VISP_HAVE_OPENCV_VERSION >= 0x020200) && defined(VISP_HAVE_OPENCV_OBJDETECT)
14   //! [Macro defined]
15   try {
16     //! [Default settings]
17     std::string opt_face_cascade_name = "./haarcascade_frontalface_alt.xml";
18     std::string opt_video = "video.mp4";
19     //! [Default settings]
20 
21     for (int i = 0; i < argc; i++) {
22       if (std::string(argv[i]) == "--haar")
23         opt_face_cascade_name = std::string(argv[i + 1]);
24       else if (std::string(argv[i]) == "--video")
25         opt_video = std::string(argv[i + 1]);
26       else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
27         std::cout << "Usage: " << argv[0]
28                   << " [--haar <haarcascade xml filename>] [--video <input video file>]"
29                   << " [--help] [-h]"
30                   << std::endl;
31         return 0;
32       }
33     }
34 
35     vpImage<unsigned char> I;
36 
37     vpVideoReader g;
38     g.setFileName(opt_video);
39     g.open(I);
40 
41 #if defined(VISP_HAVE_X11)
42     vpDisplayX d(I);
43 #elif defined(VISP_HAVE_GDI)
44     vpDisplayGDI d(I);
45 #elif defined(VISP_HAVE_OPENCV)
46     vpDisplayOpenCV d(I);
47 #endif
48     vpDisplay::setTitle(I, "ViSP viewer");
49 
50     //! [Face detector construction]
51     vpDetectorFace face_detector;
52     //! [Face detector construction]
53     //! [Face detector setting]
54     face_detector.setCascadeClassifierFile(opt_face_cascade_name);
55     //! [Face detector setting]
56 
57     bool exit_requested = false;
58     while (!g.end() && !exit_requested) {
59       g.acquire(I);
60 
61       vpDisplay::display(I);
62       //! [Face detection]
63       bool face_found = face_detector.detect(I);
64       //! [Face detection]
65 
66       if (face_found) {
67         std::ostringstream text;
68         //! [Get number faces]
69         text << "Found " << face_detector.getNbObjects() << " face(s)";
70         //! [Get number faces]
71         vpDisplay::displayText(I, 10, 10, text.str(), vpColor::red);
72         //! [Get face characteristics]
73         for (size_t i = 0; i < face_detector.getNbObjects(); i++) {
74           vpRect bbox = face_detector.getBBox(i);
75           vpDisplay::displayRectangle(I, bbox, vpColor::green, false, 4);
76           vpDisplay::displayText(I, (int)bbox.getTop() - 10, (int)bbox.getLeft(),
77                                  "Message: \"" + face_detector.getMessage(i) + "\"", vpColor::red);
78         }
79         //! [Get face characteristics]
80       }
81       vpDisplay::displayText(I, (int)I.getHeight() - 25, 10, "Click to quit...", vpColor::red);
82       vpDisplay::flush(I);
83       if (vpDisplay::getClick(I, false)) // a click to exit
84         exit_requested = true;
85     }
86     if (!exit_requested)
87       vpDisplay::getClick(I);
88   } catch (const vpException &e) {
89     std::cout << e.getMessage() << std::endl;
90   }
91 #else
92   (void)argc;
93   (void)argv;
94 #endif
95 }
96