/** * @function detectAndDisplay */ int 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]; 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]); if (frame_gray.data) { frame_gray.convertTo(frame_gray, CV_32FC1); cv::Mat target = CropFace(frame_gray,leftPupil, rightPupil, offset_pct, dest_sz); if (errorflag) { return -1; } if (target.data) { target.convertTo(target, CV_8UC1); equalizeHist(target,target); target.convertTo(target, CV_32FC1); int index = query(target,ef); index+= startnum; char temp[3]; sprintf(temp, "%d", index); std::string s(temp); std::cout << "face recognized, index : " << index << std::endl; // imshow("target", ef.norm_0_255(target).reshape(1, dest_sz.x)); imshow("result"+s, ef.getdb()[index-startnum]); waitKey(0); destroyWindow("result"+s); return index; } } } return -1; }
/** * @function detectAndDisplay */ cv::Mat 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) { return findEyes(frame_gray, faces[0]); } }
std::vector<float> FeatureVectorBuilder::detectFeatures(const cv::Mat& input) { //For saving puropse static int imgNumber = 0; assert(input.data != NULL); //Finding face size int rightFace, leftFace; int size = faceSize(input, leftFace, rightFace); //Eyes section int eyeLine = findEyesLine(input); cv::Mat rEye, lEye; cv::Rect rEyeRect, lEyeRect; findEyes(input, eyeLine, rEye, lEye, rEyeRect, lEyeRect); if(rEye.data == NULL || lEye.data == NULL) { return std::vector<float>(7); } cv::Point rEyeCenter, lEyeCenter; int rEyeRadius, lEyeRadius; findEyeCenter(rEye, rEyeCenter, rEyeRadius); findEyeCenter(lEye, lEyeCenter, lEyeRadius); transformPoint(rEyeCenter, rEyeRect); transformPoint(lEyeCenter, lEyeRect); //nose cv::Mat nose; cv::Rect noseRect = findNose(input); input(noseRect).copyTo(nose); if((noseRect.width <= 0 && noseRect.height <= 0) || nose.data == NULL) { return std::vector<float>(7); } cv::Point noseLeft, noseRight; findNosePoints(nose, noseLeft, noseRight); transformPoint(noseLeft, noseRect); transformPoint(noseRight, noseRect); //mouth cv::Mat mouth; cv::Point left, right, up, down; cv::Rect mouthRect = findMouth(input); input(mouthRect).copyTo(mouth); if((mouthRect.width <= 0 && mouthRect.height <= 0) || mouth.data == NULL) { return std::vector<float>(7); } findMouthPoints(mouth, left, right, up, down); transformPoint(left, mouthRect); transformPoint(right, mouthRect); transformPoint(up, mouthRect); transformPoint(down, mouthRect); #ifdef _DEBUG cv::Mat cpy; input.copyTo(cpy); cv::circle(cpy, lEyeCenter, 4, cv::Scalar(0, 0, 255)); cv::circle(cpy, rEyeCenter, 4, cv::Scalar(0, 0, 255)); cv::circle(cpy, noseLeft, 4, cv::Scalar(0, 255, 0)); cv::circle(cpy, noseRight, 4, cv::Scalar(0, 255, 0)); cv::circle(cpy, left, 4, cv::Scalar(255, 0, 255)); cv::circle(cpy, right, 4, cv::Scalar(255, 0, 255)); cv::circle(cpy, up, 4, cv::Scalar(255, 0, 255)); cv::circle(cpy, down, 4, cv::Scalar(255, 0, 255)); std::stringstream ss; ss << "debug//img" << imgNumber << ".jpg"; cv::imwrite(ss.str(), cpy); imgNumber++; #endif //wektor cech float x1, x2, x3, x4, x5, x6, x7; //odlegosc miedzy srodkami oczy x1 = distance(lEyeCenter, rEyeCenter); //szerokosc nosa x2 = distance(noseLeft, noseRight); //szerokosc ust x3 = distance(left, right); //wysokosc ust x4 = distance(up, down); //szerokosc twarzy x5 = (float)size; //odgleglosc miedzy linia oczu a lina nosa float tmp1, tmp2; tmp1 = distance(lEyeCenter, noseLeft); tmp2 = distance(rEyeCenter, noseRight); x6 = (tmp1 + tmp2) / 2.0f; //odleglosc miedzy linia oczu a linia ust tmp1 = distance(lEyeCenter, left); tmp2 = distance(rEyeCenter, right); x7 = (tmp1 + tmp2) / 2.0f; std::vector<float> characteristic; characteristic.push_back(x1/x2); characteristic.push_back(x1/x3); characteristic.push_back(x2/x3); characteristic.push_back(x1/x5); characteristic.push_back(x1/x6); characteristic.push_back(x1/x7); characteristic.push_back(x4/x5); return characteristic; }