Skip to content

Commit

Permalink
[FIX] relative path to face cascade was unconsistent with CMakeLists.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Huang committed Oct 14, 2014
1 parent 403d07c commit 84d4cfc
Showing 1 changed file with 173 additions and 175 deletions.
348 changes: 173 additions & 175 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@


/** Function Headers */
void detectAndDisplay( cv::Mat frame );
void detectAndDisplay(cv::Mat frame);

/** Global variables */
//-- Note, either copy these two files from opencv/data/haarscascades to your current folder, or change these locations
cv::String face_cascade_name = "../../../res/haarcascade_frontalface_alt.xml";
cv::String face_cascade_name = "../../res/haarcascade_frontalface_alt.xml";
cv::CascadeClassifier face_cascade;
std::string main_window_name = "Capture - Face detection";
std::string face_window_name = "Capture - Face";
Expand All @@ -31,188 +31,186 @@ cv::Mat skinCrCbHist = cv::Mat::zeros(cv::Size(256, 256), CV_8UC1);
/**
* @function main
*/
int main( int argc, const char** argv ) {
CvCapture* capture;
cv::Mat frame;

// Load the cascades
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade, please change face_cascade_name in source code.\n"); return -1; };

cv::namedWindow(main_window_name,CV_WINDOW_NORMAL);
cv::moveWindow(main_window_name, 400, 100);
cv::namedWindow(face_window_name,CV_WINDOW_NORMAL);
cv::moveWindow(face_window_name, 10, 100);
cv::namedWindow("Right Eye",CV_WINDOW_NORMAL);
cv::moveWindow("Right Eye", 10, 600);
cv::namedWindow("Left Eye",CV_WINDOW_NORMAL);
cv::moveWindow("Left Eye", 10, 800);
cv::namedWindow("aa",CV_WINDOW_NORMAL);
cv::moveWindow("aa", 10, 800);
cv::namedWindow("aaa",CV_WINDOW_NORMAL);
cv::moveWindow("aaa", 10, 800);

createCornerKernels();
ellipse(skinCrCbHist, cv::Point(113, 155.6), cv::Size(23.4, 15.2),
43.0, 0.0, 360.0, cv::Scalar(255, 255, 255), -1);

// Read the video stream
capture = cvCaptureFromCAM( -1 );
if( capture ) {
while( true ) {
frame = cvQueryFrame( capture );
// mirror it
cv::flip(frame, frame, 1);
frame.copyTo(debugImage);

// Apply the classifier to the frame
if( !frame.empty() ) {
detectAndDisplay( frame );
}
else {
printf(" --(!) No captured frame -- Break!");
break;
}

imshow(main_window_name,debugImage);

int c = cv::waitKey(10);
if( (char)c == 'c' ) { break; }
if( (char)c == 'f' ) {
imwrite("frame.png",frame);
}

}
}

releaseCornerKernels();

return 0;
int main(int argc, const char** argv) {
cv::Mat frame;

// Load the cascades
if (!face_cascade.load(face_cascade_name)){ printf("--(!)Error loading face cascade, please change face_cascade_name in source code.\n"); return -1; };

cv::namedWindow(main_window_name, CV_WINDOW_NORMAL);
cv::moveWindow(main_window_name, 400, 100);
cv::namedWindow(face_window_name, CV_WINDOW_NORMAL);
cv::moveWindow(face_window_name, 10, 100);
cv::namedWindow("Right Eye", CV_WINDOW_NORMAL);
cv::moveWindow("Right Eye", 10, 600);
cv::namedWindow("Left Eye", CV_WINDOW_NORMAL);
cv::moveWindow("Left Eye", 10, 800);
cv::namedWindow("aa", CV_WINDOW_NORMAL);
cv::moveWindow("aa", 10, 800);
cv::namedWindow("aaa", CV_WINDOW_NORMAL);
cv::moveWindow("aaa", 10, 800);

createCornerKernels();
ellipse(skinCrCbHist, cv::Point(113, 155.6), cv::Size(23.4, 15.2),
43.0, 0.0, 360.0, cv::Scalar(255, 255, 255), -1);

// Read the video stream
cv::VideoCapture capture(0);
while (true) {
capture >> frame;

// mirror it
cv::flip(frame, frame, 1);
frame.copyTo(debugImage);

// Apply the classifier to the frame
if (!frame.empty()) {
detectAndDisplay(frame);
}
else {
printf(" --(!) No captured frame -- Break!");
break;
}

imshow(main_window_name, debugImage);

int c = cv::waitKey(10);
if ((char)c == 'c') { break; }
if ((char)c == 'f') {
imwrite("frame.png", frame);
}
}

releaseCornerKernels();
std::cout << "done" << std::endl;
std::cin.get();
return 0;
}

void findEyes(cv::Mat frame_gray, cv::Rect face) {
cv::Mat faceROI = frame_gray(face);
cv::Mat debugFace = faceROI;

if (kSmoothFaceImage) {
double sigma = kSmoothFaceFactor * face.width;
GaussianBlur( faceROI, faceROI, cv::Size( 0, 0 ), sigma);
}
//-- Find eye regions and draw them
int eye_region_width = face.width * (kEyePercentWidth/100.0);
int eye_region_height = face.width * (kEyePercentHeight/100.0);
int eye_region_top = face.height * (kEyePercentTop/100.0);
cv::Rect leftEyeRegion(face.width*(kEyePercentSide/100.0),
eye_region_top,eye_region_width,eye_region_height);
cv::Rect rightEyeRegion(face.width - eye_region_width - face.width*(kEyePercentSide/100.0),
eye_region_top,eye_region_width,eye_region_height);

//-- Find Eye Centers
cv::Point leftPupil = findEyeCenter(faceROI,leftEyeRegion,"Left Eye");
cv::Point rightPupil = findEyeCenter(faceROI,rightEyeRegion,"Right Eye");
// get corner regions
cv::Rect leftRightCornerRegion(leftEyeRegion);
leftRightCornerRegion.width -= leftPupil.x;
leftRightCornerRegion.x += leftPupil.x;
leftRightCornerRegion.height /= 2;
leftRightCornerRegion.y += leftRightCornerRegion.height / 2;
cv::Rect leftLeftCornerRegion(leftEyeRegion);
leftLeftCornerRegion.width = leftPupil.x;
leftLeftCornerRegion.height /= 2;
leftLeftCornerRegion.y += leftLeftCornerRegion.height / 2;
cv::Rect rightLeftCornerRegion(rightEyeRegion);
rightLeftCornerRegion.width = rightPupil.x;
rightLeftCornerRegion.height /= 2;
rightLeftCornerRegion.y += rightLeftCornerRegion.height / 2;
cv::Rect rightRightCornerRegion(rightEyeRegion);
rightRightCornerRegion.width -= rightPupil.x;
rightRightCornerRegion.x += rightPupil.x;
rightRightCornerRegion.height /= 2;
rightRightCornerRegion.y += rightRightCornerRegion.height / 2;
rectangle(debugFace,leftRightCornerRegion,200);
rectangle(debugFace,leftLeftCornerRegion,200);
rectangle(debugFace,rightLeftCornerRegion,200);
rectangle(debugFace,rightRightCornerRegion,200);
// change eye centers to face coordinates
rightPupil.x += rightEyeRegion.x;
rightPupil.y += rightEyeRegion.y;
leftPupil.x += leftEyeRegion.x;
leftPupil.y += leftEyeRegion.y;
// draw eye centers
circle(debugFace, rightPupil, 3, 1234);
circle(debugFace, leftPupil, 3, 1234);

//-- Find Eye Corners
if (kEnableEyeCorner) {
cv::Point2f leftRightCorner = findEyeCorner(faceROI(leftRightCornerRegion), true, false);
leftRightCorner.x += leftRightCornerRegion.x;
leftRightCorner.y += leftRightCornerRegion.y;
cv::Point2f leftLeftCorner = findEyeCorner(faceROI(leftLeftCornerRegion), true, true);
leftLeftCorner.x += leftLeftCornerRegion.x;
leftLeftCorner.y += leftLeftCornerRegion.y;
cv::Point2f rightLeftCorner = findEyeCorner(faceROI(rightLeftCornerRegion), false, true);
rightLeftCorner.x += rightLeftCornerRegion.x;
rightLeftCorner.y += rightLeftCornerRegion.y;
cv::Point2f rightRightCorner = findEyeCorner(faceROI(rightRightCornerRegion), false, false);
rightRightCorner.x += rightRightCornerRegion.x;
rightRightCorner.y += rightRightCornerRegion.y;
circle(faceROI, leftRightCorner, 3, 200);
circle(faceROI, leftLeftCorner, 3, 200);
circle(faceROI, rightLeftCorner, 3, 200);
circle(faceROI, rightRightCorner, 3, 200);
}

imshow(face_window_name, faceROI);
// cv::Rect roi( cv::Point( 0, 0 ), faceROI.size());
// cv::Mat destinationROI = debugImage( roi );
// faceROI.copyTo( destinationROI );
cv::Mat faceROI = frame_gray(face);
cv::Mat debugFace = faceROI;

if (kSmoothFaceImage) {
double sigma = kSmoothFaceFactor * face.width;
GaussianBlur(faceROI, faceROI, cv::Size(0, 0), sigma);
}
//-- Find eye regions and draw them
int eye_region_width = face.width * (kEyePercentWidth / 100.0);
int eye_region_height = face.width * (kEyePercentHeight / 100.0);
int eye_region_top = face.height * (kEyePercentTop / 100.0);
cv::Rect leftEyeRegion(face.width*(kEyePercentSide / 100.0),
eye_region_top, eye_region_width, eye_region_height);
cv::Rect rightEyeRegion(face.width - eye_region_width - face.width*(kEyePercentSide / 100.0),
eye_region_top, eye_region_width, eye_region_height);

//-- Find Eye Centers
cv::Point leftPupil = findEyeCenter(faceROI, leftEyeRegion, "Left Eye");
cv::Point rightPupil = findEyeCenter(faceROI, rightEyeRegion, "Right Eye");
// get corner regions
cv::Rect leftRightCornerRegion(leftEyeRegion);
leftRightCornerRegion.width -= leftPupil.x;
leftRightCornerRegion.x += leftPupil.x;
leftRightCornerRegion.height /= 2;
leftRightCornerRegion.y += leftRightCornerRegion.height / 2;
cv::Rect leftLeftCornerRegion(leftEyeRegion);
leftLeftCornerRegion.width = leftPupil.x;
leftLeftCornerRegion.height /= 2;
leftLeftCornerRegion.y += leftLeftCornerRegion.height / 2;
cv::Rect rightLeftCornerRegion(rightEyeRegion);
rightLeftCornerRegion.width = rightPupil.x;
rightLeftCornerRegion.height /= 2;
rightLeftCornerRegion.y += rightLeftCornerRegion.height / 2;
cv::Rect rightRightCornerRegion(rightEyeRegion);
rightRightCornerRegion.width -= rightPupil.x;
rightRightCornerRegion.x += rightPupil.x;
rightRightCornerRegion.height /= 2;
rightRightCornerRegion.y += rightRightCornerRegion.height / 2;
rectangle(debugFace, leftRightCornerRegion, 200);
rectangle(debugFace, leftLeftCornerRegion, 200);
rectangle(debugFace, rightLeftCornerRegion, 200);
rectangle(debugFace, rightRightCornerRegion, 200);
// change eye centers to face coordinates
rightPupil.x += rightEyeRegion.x;
rightPupil.y += rightEyeRegion.y;
leftPupil.x += leftEyeRegion.x;
leftPupil.y += leftEyeRegion.y;
// draw eye centers
circle(debugFace, rightPupil, 3, 1234);
circle(debugFace, leftPupil, 3, 1234);

//-- Find Eye Corners
if (kEnableEyeCorner) {
cv::Point2f leftRightCorner = findEyeCorner(faceROI(leftRightCornerRegion), true, false);
leftRightCorner.x += leftRightCornerRegion.x;
leftRightCorner.y += leftRightCornerRegion.y;
cv::Point2f leftLeftCorner = findEyeCorner(faceROI(leftLeftCornerRegion), true, true);
leftLeftCorner.x += leftLeftCornerRegion.x;
leftLeftCorner.y += leftLeftCornerRegion.y;
cv::Point2f rightLeftCorner = findEyeCorner(faceROI(rightLeftCornerRegion), false, true);
rightLeftCorner.x += rightLeftCornerRegion.x;
rightLeftCorner.y += rightLeftCornerRegion.y;
cv::Point2f rightRightCorner = findEyeCorner(faceROI(rightRightCornerRegion), false, false);
rightRightCorner.x += rightRightCornerRegion.x;
rightRightCorner.y += rightRightCornerRegion.y;
circle(faceROI, leftRightCorner, 3, 200);
circle(faceROI, leftLeftCorner, 3, 200);
circle(faceROI, rightLeftCorner, 3, 200);
circle(faceROI, rightRightCorner, 3, 200);
}

imshow(face_window_name, faceROI);
// cv::Rect roi( cv::Point( 0, 0 ), faceROI.size());
// cv::Mat destinationROI = debugImage( roi );
// faceROI.copyTo( destinationROI );
}


cv::Mat findSkin (cv::Mat &frame) {
cv::Mat input;
cv::Mat output = cv::Mat(frame.rows,frame.cols, CV_8U);

cvtColor(frame, input, CV_BGR2YCrCb);

for (int y = 0; y < input.rows; ++y) {
const cv::Vec3b *Mr = input.ptr<cv::Vec3b>(y);
// uchar *Or = output.ptr<uchar>(y);
cv::Vec3b *Or = frame.ptr<cv::Vec3b>(y);
for (int x = 0; x < input.cols; ++x) {
cv::Vec3b ycrcb = Mr[x];
// Or[x] = (skinCrCbHist.at<uchar>(ycrcb[1], ycrcb[2]) > 0) ? 255 : 0;
if(skinCrCbHist.at<uchar>(ycrcb[1], ycrcb[2]) == 0) {
Or[x] = cv::Vec3b(0,0,0);
}
}
}
return output;
cv::Mat findSkin(cv::Mat &frame) {
cv::Mat input;
cv::Mat output = cv::Mat(frame.rows, frame.cols, CV_8U);

cvtColor(frame, input, CV_BGR2YCrCb);

for (int y = 0; y < input.rows; ++y) {
const cv::Vec3b *Mr = input.ptr<cv::Vec3b>(y);
// uchar *Or = output.ptr<uchar>(y);
cv::Vec3b *Or = frame.ptr<cv::Vec3b>(y);
for (int x = 0; x < input.cols; ++x) {
cv::Vec3b ycrcb = Mr[x];
// Or[x] = (skinCrCbHist.at<uchar>(ycrcb[1], ycrcb[2]) > 0) ? 255 : 0;
if (skinCrCbHist.at<uchar>(ycrcb[1], ycrcb[2]) == 0) {
Or[x] = cv::Vec3b(0, 0, 0);
}
}
}
return output;
}

/**
* @function detectAndDisplay
*/
void detectAndDisplay( cv::Mat frame ) {
std::vector<cv::Rect> faces;
//cv::Mat frame_gray;

std::vector<cv::Mat> rgbChannels(3);
cv::split(frame, rgbChannels);
cv::Mat frame_gray = rgbChannels[2];

//cvtColor( frame, frame_gray, CV_BGR2GRAY );
//equalizeHist( frame_gray, frame_gray );
//cv::pow(frame_gray, CV_64F, frame_gray);
//-- Detect faces
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE|CV_HAAR_FIND_BIGGEST_OBJECT, cv::Size(150, 150) );
// findSkin(debugImage);

for( int i = 0; i < faces.size(); i++ )
{
rectangle(debugImage, faces[i], 1234);
}
//-- Show what you got
if (faces.size() > 0) {
findEyes(frame_gray, faces[0]);
}
void detectAndDisplay(cv::Mat frame) {
std::vector<cv::Rect> faces;
//cv::Mat frame_gray;

std::vector<cv::Mat> rgbChannels(3);
cv::split(frame, rgbChannels);
cv::Mat frame_gray = rgbChannels[2];

//cvtColor( frame, frame_gray, CV_BGR2GRAY );
//equalizeHist( frame_gray, frame_gray );
//cv::pow(frame_gray, CV_64F, frame_gray);
//-- Detect faces
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE | CV_HAAR_FIND_BIGGEST_OBJECT, cv::Size(150, 150));
// findSkin(debugImage);

for (int i = 0; i < faces.size(); i++)
{
rectangle(debugImage, faces[i], 1234);
}
//-- Show what you got
if (faces.size() > 0) {
findEyes(frame_gray, faces[0]);
}
}

0 comments on commit 84d4cfc

Please sign in to comment.