bool TemplateBased::eyesInit(cv::Mat& gray, double timestamp) { cv::Rect face; cv::Mat faceROI; std::chrono::time_point<std::chrono::steady_clock> t1; t1 = std::chrono::steady_clock::now(); int fdRes = this->faceDetect(gray, &face); difftime("Face detect", t1, debug_tmpl_perf2); if (fdRes != 0) { return false; } faceROI = gray(face); imshowWrapper("face", faceROI, debug_show_img_face); int rowsO = (face).height/4.3; int colsO = (face).width/5.5; int rows2 = (face).height/4.3; int cols2 = (face).width/4.5; cv::Rect leftE(colsO, rowsO, cols2, rows2); cv::Rect rightE((face).width-colsO-rows2, rowsO, cols2, rows2); // mark where on the gray are eyes so we can define search region in the next frame // region size is double size of leftTemplate and leftTemplate size this->lEye = cv::Point(face.x+leftE.x, face.y+leftE.y); this->rEye = cv::Point(face.x+rightE.x, face.y+rightE.y); this->initialEyesDistance = this->rEye.x-this->lEye.x; this->lLastTime = timestamp; this->rLastTime = timestamp; doLog(debug_tmpl_log, "debug_tmpl_log: lEye %d %d rEye %d %d\n", lEye.x, lEye.y, rEye.x, rEye.y); cv::Mat left = faceROI(leftE); cv::Mat right = faceROI(rightE); // TODO - should wait for initial blink, so that eyes are open for sure left.copyTo(this->leftTemplate); right.copyTo(this->rightTemplate); cv::Mat lTemplSearch, rTemplSearch; cv::Rect lTemplSearchR, rTemplSearchR; bool res = this->updateTemplSearch(gray, lTemplSearchR, rTemplSearchR, lTemplSearch, rTemplSearch); if (res == false) { return false; } left.copyTo(this->leftTemplate); right.copyTo(this->rightTemplate); imshowWrapper("left", this->leftTemplate, debug_show_img_templ_eyes_tmpl); imshowWrapper("right", this->rightTemplate, debug_show_img_templ_eyes_tmpl); doLog(debug_tmpl_log, "debug_tmpl_log: reinit: timestamp %lf lLastTime %lf rLastTime %lf\n", timestamp, this->lLastTime, this->rLastTime); return true; };
void findEyes(cv::Mat frame_gray, cv::Rect face) { cv::Mat faceROI = frame_gray(face); 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 leftCornerRegion(leftEyeRegion); leftCornerRegion.width -= leftPupil.x; leftCornerRegion.x += leftPupil.x; leftCornerRegion.height /= 2; leftCornerRegion.y += leftCornerRegion.height / 2; cv::Rect rightCornerRegion(rightEyeRegion); rightCornerRegion.width = rightPupil.x; rightCornerRegion.height /= 2; rightCornerRegion.y += rightCornerRegion.height / 2; rectangle(faceROI,leftCornerRegion,200); rectangle(faceROI,rightCornerRegion,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(faceROI, rightPupil, 3, 1234); circle(faceROI, leftPupil, 3, 1234); //-- Find Eye Corners if (kEnableEyeCorner) { cv::Point leftCorner = findEyeCorner(faceROI(leftCornerRegion), false); leftCorner.x += leftCornerRegion.x; leftCorner.y += leftCornerRegion.y; cv::Point rightCorner = findEyeCorner(faceROI(leftCornerRegion), false); rightCorner.x += rightCornerRegion.x; rightCorner.y += rightCornerRegion.y; circle(faceROI, rightCorner, 3, 200); circle(faceROI, leftCorner, 3, 200); } imshow(face_window_name, faceROI); }