Blame view

3rdparty/opencv-4.5.4/samples/cpp/tutorial_code/objectDetection/objectDetection.cpp 3.21 KB
f4334277   Hu Chunming   提交3rdparty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  #include "opencv2/objdetect.hpp"
  #include "opencv2/highgui.hpp"
  #include "opencv2/imgproc.hpp"
  #include "opencv2/videoio.hpp"
  #include <iostream>
  
  using namespace std;
  using namespace cv;
  
  /** Function Headers */
  void detectAndDisplay( Mat frame );
  
  /** Global variables */
  CascadeClassifier face_cascade;
  CascadeClassifier eyes_cascade;
  
  /** @function main */
  int main( int argc, const char** argv )
  {
      CommandLineParser parser(argc, argv,
                               "{help h||}"
                               "{face_cascade|data/haarcascades/haarcascade_frontalface_alt.xml|Path to face cascade.}"
                               "{eyes_cascade|data/haarcascades/haarcascade_eye_tree_eyeglasses.xml|Path to eyes cascade.}"
                               "{camera|0|Camera device number.}");
  
      parser.about( "\nThis program demonstrates using the cv::CascadeClassifier class to detect objects (Face + eyes) in a video stream.\n"
                    "You can use Haar or LBP features.\n\n" );
      parser.printMessage();
  
      String face_cascade_name = samples::findFile( parser.get<String>("face_cascade") );
      String eyes_cascade_name = samples::findFile( parser.get<String>("eyes_cascade") );
  
      //-- 1. Load the cascades
      if( !face_cascade.load( face_cascade_name ) )
      {
          cout << "--(!)Error loading face cascade\n";
          return -1;
      };
      if( !eyes_cascade.load( eyes_cascade_name ) )
      {
          cout << "--(!)Error loading eyes cascade\n";
          return -1;
      };
  
      int camera_device = parser.get<int>("camera");
      VideoCapture capture;
      //-- 2. Read the video stream
      capture.open( camera_device );
      if ( ! capture.isOpened() )
      {
          cout << "--(!)Error opening video capture\n";
          return -1;
      }
  
      Mat frame;
      while ( capture.read(frame) )
      {
          if( frame.empty() )
          {
              cout << "--(!) No captured frame -- Break!\n";
              break;
          }
  
          //-- 3. Apply the classifier to the frame
          detectAndDisplay( frame );
  
          if( waitKey(10) == 27 )
          {
              break; // escape
          }
      }
      return 0;
  }
  
  /** @function detectAndDisplay */
  void detectAndDisplay( Mat frame )
  {
      Mat frame_gray;
      cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
      equalizeHist( frame_gray, frame_gray );
  
      //-- Detect faces
      std::vector<Rect> faces;
      face_cascade.detectMultiScale( frame_gray, faces );
  
      for ( size_t i = 0; i < faces.size(); i++ )
      {
          Point center( faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2 );
          ellipse( frame, center, Size( faces[i].width/2, faces[i].height/2 ), 0, 0, 360, Scalar( 255, 0, 255 ), 4 );
  
          Mat faceROI = frame_gray( faces[i] );
  
          //-- In each face, detect eyes
          std::vector<Rect> eyes;
          eyes_cascade.detectMultiScale( faceROI, eyes );
  
          for ( size_t j = 0; j < eyes.size(); j++ )
          {
              Point eye_center( faces[i].x + eyes[j].x + eyes[j].width/2, faces[i].y + eyes[j].y + eyes[j].height/2 );
              int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
              circle( frame, eye_center, radius, Scalar( 255, 0, 0 ), 4 );
          }
      }
  
      //-- Show what you got
      imshow( "Capture - Face detection", frame );
  }