private Mat get_template(CascadeClassifier clasificator, Rect area, int size) { Mat template = new Mat(); Mat mROI = mGray.submat(area); MatOfRect eyes = new MatOfRect(); Point iris = new Point(); Rect eye_template = new Rect(); clasificator.detectMultiScale( mROI, eyes, 1.15, 2, Objdetect.CASCADE_FIND_BIGGEST_OBJECT | Objdetect.CASCADE_SCALE_IMAGE, new Size(30, 30), new Size()); Rect[] eyesArray = eyes.toArray(); for (int i = 0; i < eyesArray.length; ) { Rect e = eyesArray[i]; e.x = area.x + e.x; e.y = area.y + e.y; Rect eye_only_rectangle = new Rect( (int) e.tl().x, (int) (e.tl().y + e.height * 0.4), (int) e.width, (int) (e.height * 0.6)); mROI = mGray.submat(eye_only_rectangle); Mat vyrez = mRgba.submat(eye_only_rectangle); Core.MinMaxLocResult mmG = Core.minMaxLoc(mROI); Imgproc.circle(vyrez, mmG.minLoc, 2, new Scalar(255, 255, 255, 255), 2); iris.x = mmG.minLoc.x + eye_only_rectangle.x; iris.y = mmG.minLoc.y + eye_only_rectangle.y; eye_template = new Rect((int) iris.x - size / 2, (int) iris.y - size / 2, size, size); Imgproc.rectangle(mRgba, eye_template.tl(), eye_template.br(), new Scalar(255, 0, 0, 255), 2); template = (mGray.submat(eye_template)).clone(); return template; } return template; }
public Mat onCameraFrame(CvCameraViewFrame inputFrame) { mRgba = inputFrame.rgba(); mGray = inputFrame.gray(); if (mAbsoluteFaceSize == 0) { int height = mGray.rows(); if (Math.round(height * mRelativeFaceSize) > 0) { mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize); } } if (mZoomWindow == null || mZoomWindow2 == null) CreateAuxiliaryMats(); MatOfRect faces = new MatOfRect(); if (mJavaDetector != null) mJavaDetector.detectMultiScale( mGray, faces, 1.1, 2, 2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size()); Rect[] facesArray = faces.toArray(); for (int i = 0; i < facesArray.length; i++) { Imgproc.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 3); xCenter = (facesArray[i].x + facesArray[i].width + facesArray[i].x) / 2; yCenter = (facesArray[i].y + facesArray[i].y + facesArray[i].height) / 2; Point center = new Point(xCenter, yCenter); Imgproc.circle(mRgba, center, 10, new Scalar(255, 0, 0, 255), 3); Imgproc.putText( mRgba, "[" + center.x + "," + center.y + "]", new Point(center.x + 20, center.y + 20), Core.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(255, 255, 255, 255)); Rect r = facesArray[i]; // compute the eye area Rect eyearea = new Rect( r.x + r.width / 8, (int) (r.y + (r.height / 4.5)), r.width - 2 * r.width / 8, (int) (r.height / 3.0)); // split it Rect eyearea_right = new Rect( r.x + r.width / 16, (int) (r.y + (r.height / 4.5)), (r.width - 2 * r.width / 16) / 2, (int) (r.height / 3.0)); Rect eyearea_left = new Rect( r.x + r.width / 16 + (r.width - 2 * r.width / 16) / 2, (int) (r.y + (r.height / 4.5)), (r.width - 2 * r.width / 16) / 2, (int) (r.height / 3.0)); // draw the area - mGray is working grayscale mat, if you want to // see area in rgb preview, change mGray to mRgba Imgproc.rectangle(mRgba, eyearea_left.tl(), eyearea_left.br(), new Scalar(255, 0, 0, 255), 2); Imgproc.rectangle( mRgba, eyearea_right.tl(), eyearea_right.br(), new Scalar(255, 0, 0, 255), 2); if (learn_frames < 5) { teplateR = get_template(mJavaDetectorEye, eyearea_right, 24); teplateL = get_template(mJavaDetectorEye, eyearea_left, 24); learn_frames++; } else { // Learning finished, use the new templates for template // matching match_eye(eyearea_right, teplateR, method); match_eye(eyearea_left, teplateL, method); } // cut eye areas and put them to zoom windows Imgproc.resize(mRgba.submat(eyearea_left), mZoomWindow2, mZoomWindow2.size()); Imgproc.resize(mRgba.submat(eyearea_right), mZoomWindow, mZoomWindow.size()); } return mRgba; }