converted some more samples to C++
[profile/ivi/opencv.git] / samples / c / facedetect.cpp
1 #include <opencv2/objdetect/objdetect.hpp>
2 #include <opencv2/highgui/highgui.hpp>
3 #include <opencv2/imgproc/imgproc.hpp>
4
5 #include <iostream>
6 #include <stdio.h>
7
8 using namespace std;
9 using namespace cv;
10
11 void detectAndDraw( Mat& img,
12                    CascadeClassifier& cascade, CascadeClassifier& nestedCascade,
13                    double scale);
14
15 String cascadeName =
16 "../../data/haarcascades/haarcascade_frontalface_alt.xml";
17 String nestedCascadeName =
18 "../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml";
19
20 int main( int argc, const char** argv )
21 {
22     CvCapture* capture = 0;
23     Mat frame, frameCopy, image;
24     const String scaleOpt = "--scale=";
25     size_t scaleOptLen = scaleOpt.length();
26     const String cascadeOpt = "--cascade=";
27     size_t cascadeOptLen = cascadeOpt.length();
28     const String nestedCascadeOpt = "--nested-cascade";
29     size_t nestedCascadeOptLen = nestedCascadeOpt.length();
30     String inputName;
31
32     CascadeClassifier cascade, nestedCascade;
33     double scale = 1;
34
35     for( int i = 1; i < argc; i++ )
36     {
37         if( cascadeOpt.compare( 0, cascadeOptLen, argv[i], cascadeOptLen ) == 0 )
38             cascadeName.assign( argv[i] + cascadeOptLen );
39         else if( nestedCascadeOpt.compare( 0, nestedCascadeOptLen, argv[i], nestedCascadeOptLen ) == 0 )
40         {
41             if( argv[i][nestedCascadeOpt.length()] == '=' )
42                 nestedCascadeName.assign( argv[i] + nestedCascadeOpt.length() + 1 );
43             if( !nestedCascade.load( nestedCascadeName ) )
44                 cerr << "WARNING: Could not load classifier cascade for nested objects" << endl;
45         }
46         else if( scaleOpt.compare( 0, scaleOptLen, argv[i], scaleOptLen ) == 0 )
47         {
48             if( !sscanf( argv[i] + scaleOpt.length(), "%lf", &scale ) || scale < 1 )
49                 scale = 1;
50         }
51         else if( argv[i][0] == '-' )
52         {
53             cerr << "WARNING: Unknown option %s" << argv[i] << endl;
54         }
55         else
56             inputName.assign( argv[i] );
57     }
58
59     if( !cascade.load( cascadeName ) )
60     {
61         cerr << "ERROR: Could not load classifier cascade" << endl;
62         cerr << "Usage: facedetect [--cascade=\"<cascade_path>\"]\n"
63             "   [--nested-cascade[=\"nested_cascade_path\"]]\n"
64             "   [--scale[=<image scale>\n"
65             "   [filename|camera_index]\n" ;
66         return -1;
67     }
68
69     if( inputName.empty() || (isdigit(inputName.c_str()[0]) && inputName.c_str()[1] == '\0') )
70         capture = cvCaptureFromCAM( inputName.empty() ? 0 : inputName.c_str()[0] - '0' );
71     else if( inputName.size() )
72     {
73         image = imread( inputName, 1 );
74         if( image.empty() )
75             capture = cvCaptureFromAVI( inputName.c_str() );
76     }
77     else
78         image = imread( "lena.jpg", 1 );
79
80     cvNamedWindow( "result", 1 );
81
82     if( capture )
83     {
84         for(;;)
85         {
86             IplImage* iplImg = cvQueryFrame( capture );
87             frame = iplImg;
88             if( frame.empty() )
89                 break;
90             if( iplImg->origin == IPL_ORIGIN_TL )
91                 frame.copyTo( frameCopy );
92             else
93                 flip( frame, frameCopy, 0 );
94
95             detectAndDraw( frameCopy, cascade, nestedCascade, scale );
96
97             if( waitKey( 10 ) >= 0 )
98                 goto _cleanup_;
99         }
100
101         waitKey(0);
102 _cleanup_:
103         cvReleaseCapture( &capture );
104     }
105     else
106     {
107         if( !image.empty() )
108         {
109             detectAndDraw( image, cascade, nestedCascade, scale );
110             waitKey(0);
111         }
112         else if( !inputName.empty() )
113         {
114             /* assume it is a text file containing the
115             list of the image filenames to be processed - one per line */
116             FILE* f = fopen( inputName.c_str(), "rt" );
117             if( f )
118             {
119                 char buf[1000+1];
120                 while( fgets( buf, 1000, f ) )
121                 {
122                     int len = (int)strlen(buf), c;
123                     while( len > 0 && isspace(buf[len-1]) )
124                         len--;
125                     buf[len] = '\0';
126                     cout << "file " << buf << endl;
127                     image = imread( buf, 1 );
128                     if( !image.empty() )
129                     {
130                         detectAndDraw( image, cascade, nestedCascade, scale );
131                         c = waitKey(0);
132                         if( c == 27 || c == 'q' || c == 'Q' )
133                             break;
134                     }
135                 }
136                 fclose(f);
137             }
138         }
139     }
140
141     cvDestroyWindow("result");
142
143     return 0;
144 }
145
146 void detectAndDraw( Mat& img,
147                    CascadeClassifier& cascade, CascadeClassifier& nestedCascade,
148                    double scale)
149 {
150     int i = 0;
151     double t = 0;
152     vector<Rect> faces;
153     const static Scalar colors[] =  { CV_RGB(0,0,255),
154         CV_RGB(0,128,255),
155         CV_RGB(0,255,255),
156         CV_RGB(0,255,0),
157         CV_RGB(255,128,0),
158         CV_RGB(255,255,0),
159         CV_RGB(255,0,0),
160         CV_RGB(255,0,255)} ;
161     Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
162
163     cvtColor( img, gray, CV_BGR2GRAY );
164     resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
165     equalizeHist( smallImg, smallImg );
166
167     t = (double)cvGetTickCount();
168     cascade.detectMultiScale( smallImg, faces,
169         1.1, 2, 0
170         //|CV_HAAR_FIND_BIGGEST_OBJECT
171         //|CV_HAAR_DO_ROUGH_SEARCH
172         |CV_HAAR_SCALE_IMAGE
173         ,
174         Size(30, 30) );
175     t = (double)cvGetTickCount() - t;
176     printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
177     for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
178     {
179         Mat smallImgROI;
180         vector<Rect> nestedObjects;
181         Point center;
182         Scalar color = colors[i%8];
183         int radius;
184         center.x = cvRound((r->x + r->width*0.5)*scale);
185         center.y = cvRound((r->y + r->height*0.5)*scale);
186         radius = cvRound((r->width + r->height)*0.25*scale);
187         circle( img, center, radius, color, 3, 8, 0 );
188         if( nestedCascade.empty() )
189             continue;
190         smallImgROI = smallImg(*r);
191         nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
192             1.1, 2, 0
193             //|CV_HAAR_FIND_BIGGEST_OBJECT
194             //|CV_HAAR_DO_ROUGH_SEARCH
195             //|CV_HAAR_DO_CANNY_PRUNING
196             |CV_HAAR_SCALE_IMAGE
197             ,
198             Size(30, 30) );
199         for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
200         {
201             center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
202             center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
203             radius = cvRound((nr->width + nr->height)*0.25*scale);
204             circle( img, center, radius, color, 3, 8, 0 );
205         }
206     }  
207     cv::imshow( "result", img );    
208 }