#include "opencv2\opencv.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
/** Function Headers */
void detectAndDisplay( Mat frame );
/** Global variables */
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);
/** @function main */
int main( int argc, const char** argv )
{
VideoCapture capture;
Mat frame;
//-- 1. Load the cascades
if( !face_cascade.load( face_cascade_name ) )
{
printf("--(!)Error loading\n");
return -1;
};
if( !eyes_cascade.load( eyes_cascade_name ) )
{
printf("--(!)Error loading\n");
return -1;
};
//-- 2. Read the video stream
capture.open(0);
if( capture.isOpened() )
{
while( true )
{
capture >> frame;
//-- 3. Apply the classifier to the frame
if( !frame.empty() )
{
detectAndDisplay( frame );
}
else
{
printf(" --(!) No captured frame -- Break!");
break;
}
int c = waitKey(10);
if( (char)c == 'c' )
{
break;
}
}
}
return 0;
}
/** @function detectAndDisplay */
void detectAndDisplay( Mat frame )
{
std::vector<Rect> faces;
Mat frame_gray;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
//-- Detect faces
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( int i = 0; i < faces.size(); i++ )
{
Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
Mat faceROI = frame_gray( faces[i] );
std::vector<Rect> eyes;
//-- In each face, detect eyes
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( int j = 0; j < eyes.size(); j++ )
{
Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
}
}
//-- Show what you got
imshow( window_name, frame );
}
CiNpbmNsdWRlICJvcGVuY3YyXG9wZW5jdi5ocHAiCgoKCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHN0ZGlvLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwp1c2luZyBuYW1lc3BhY2UgY3Y7CgovKiogRnVuY3Rpb24gSGVhZGVycyAqLwp2b2lkIGRldGVjdEFuZERpc3BsYXkoIE1hdCBmcmFtZSApOwoKLyoqIEdsb2JhbCB2YXJpYWJsZXMgKi8KU3RyaW5nIGZhY2VfY2FzY2FkZV9uYW1lID0gImhhYXJjYXNjYWRlX2Zyb250YWxmYWNlX2FsdC54bWwiOwpTdHJpbmcgZXllc19jYXNjYWRlX25hbWUgPSAiaGFhcmNhc2NhZGVfZXllX3RyZWVfZXllZ2xhc3Nlcy54bWwiOwpDYXNjYWRlQ2xhc3NpZmllciBmYWNlX2Nhc2NhZGU7CkNhc2NhZGVDbGFzc2lmaWVyIGV5ZXNfY2FzY2FkZTsKc3RyaW5nIHdpbmRvd19uYW1lID0gIkNhcHR1cmUgLSBGYWNlIGRldGVjdGlvbiI7ClJORyBybmcoMTIzNDUpOwoKLyoqIEBmdW5jdGlvbiBtYWluICovCmludCBtYWluKCBpbnQgYXJnYywgY29uc3QgY2hhcioqIGFyZ3YgKQp7CiAgICBWaWRlb0NhcHR1cmUgY2FwdHVyZTsKICAgIE1hdCBmcmFtZTsKCiAgICAvLy0tIDEuIExvYWQgdGhlIGNhc2NhZGVzCiAgICBpZiggIWZhY2VfY2FzY2FkZS5sb2FkKCBmYWNlX2Nhc2NhZGVfbmFtZSApICkKICAgIHsKICAgICAgICBwcmludGYoIi0tKCEpRXJyb3IgbG9hZGluZ1xuIik7CiAgICAgICAgcmV0dXJuIC0xOwogICAgfTsKICAgIGlmKCAhZXllc19jYXNjYWRlLmxvYWQoIGV5ZXNfY2FzY2FkZV9uYW1lICkgKQogICAgewogICAgICAgIHByaW50ZigiLS0oISlFcnJvciBsb2FkaW5nXG4iKTsKICAgICAgICByZXR1cm4gLTE7CiAgICB9OwoKICAgIC8vLS0gMi4gUmVhZCB0aGUgdmlkZW8gc3RyZWFtCiAgICBjYXB0dXJlLm9wZW4oMCk7CiAgICBpZiggY2FwdHVyZS5pc09wZW5lZCgpICkKICAgIHsKICAgICAgICB3aGlsZSggdHJ1ZSApCiAgICAgICAgewogICAgICAgICAgICAgY2FwdHVyZSA+PiBmcmFtZTsKICAgICAgICAgICAgLy8tLSAzLiBBcHBseSB0aGUgY2xhc3NpZmllciB0byB0aGUgZnJhbWUKICAgICAgICAgICAgaWYoICFmcmFtZS5lbXB0eSgpICkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZGV0ZWN0QW5kRGlzcGxheSggZnJhbWUgKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHByaW50ZigiIC0tKCEpIE5vIGNhcHR1cmVkIGZyYW1lIC0tIEJyZWFrISIpOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIGludCBjID0gd2FpdEtleSgxMCk7CiAgICAgICAgICAgIGlmKCAoY2hhciljID09ICdjJyApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIDA7Cn0KCi8qKiBAZnVuY3Rpb24gZGV0ZWN0QW5kRGlzcGxheSAqLwp2b2lkIGRldGVjdEFuZERpc3BsYXkoIE1hdCBmcmFtZSApCnsKICAgIHN0ZDo6dmVjdG9yPFJlY3Q+IGZhY2VzOwogICAgTWF0IGZyYW1lX2dyYXk7CgogICAgY3Z0Q29sb3IoIGZyYW1lLCBmcmFtZV9ncmF5LCB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