コード例 #1
0
        public Point Detect(FaceRect face, Mat frame)
        {
            mode = DetectMode;
            var properties = ScreenProperties;

            if (face == null)
            {
                throw new ArgumentNullException("face");
            }
            if (frame == null || frame.IsEmpty)
            {
                throw new ArgumentNullException("frame");
            }
            if (properties == null)
            {
                throw new ArgumentNullException("properties");
            }

            switch (mode)
            {
            case EyeGazeDetectMode.LeftOnly:
                if (face.LeftEye == null)
                {
                    return(null);
                }
                break;

            case EyeGazeDetectMode.FaceV2Mobile:
            case EyeGazeDetectMode.FaceV2:
            case EyeGazeDetectMode.FaceMobile:
            case EyeGazeDetectMode.Face:
            case EyeGazeDetectMode.Both:
                if (face.LeftEye == null || face.RightEye == null)
                {
                    return(null);
                }
                break;

            default:
                throw new NotImplementedException();
            }

            Profiler.Start("GazeDetect");

            Point vecPt  = null;
            Point result = new Point(0, 0);
            Point pt     = new Point(0, 0);

            switch (mode)
            {
            case EyeGazeDetectMode.LeftOnly:
                using (Mat left = face.LeftEye.RoiCropByPercent(frame, .33))
                    result = DetectLeftEyes(left);
                break;

            case EyeGazeDetectMode.Both:
                using (Mat left = face.LeftEye.RoiCropByPercent(frame, .33))
                    using (Mat right = face.RightEye.RoiCropByPercent(frame, .33))
                        result = DetectBothEyes(left, right);
                break;

            case EyeGazeDetectMode.FaceV2Mobile:
            case EyeGazeDetectMode.FaceV2:
            case EyeGazeDetectMode.FaceMobile:
            case EyeGazeDetectMode.Face:
                using (Mat left = face.LeftEye.RoiCropByPercent(frame, .25))
                    using (Mat right = face.RightEye.RoiCropByPercent(frame, .25))
                        using (Mat faceRoi = face.ROI(frame))
                            result = DetectFace(faceRoi, left, right);
                break;

            default:
                throw new NotImplementedException();
            }

            var x = result.X * -1;
            var y = result.Y * -1;

            if (UseModification)
            {
                x = (x + OffsetX) * SensitiveX;
                y = (y + OffsetY) * SensitiveY;
            }

            vecPt = new Point(x, y);
            if (UseSmoothing && !Calibrator.IsCalibrating)
            {
                vecPt = Smoother.Smooth(vecPt);
            }

            Vector <double> vec = CreateVector.Dense(new double[] { vecPt.X, vecPt.Y, -1 });

            pt = face.SolveRayScreenVector(new Point3D(vec.ToArray()), properties);

            if (ClipToBound)
            {
                pt.X = Util.Clamp(pt.X, 0, ScreenProperties.PixelSize.Width);
                pt.Y = Util.Clamp(pt.Y, 0, ScreenProperties.PixelSize.Height);
            }

            face.GazeInfo = new EyeGazeInfo()
            {
                ScreenPoint = pt,
                Vector      = new Point3D(vecPt.X, vecPt.Y, -1),
                ClipToBound = ClipToBound,
            };

            Calibrator.Push(new CalibratingPushData(face));
            if (UseCalibrator)
            {
                Calibrator.Apply(face, ScreenProperties);
            }

            Profiler.End("GazeDetect");
            return(face.GazeInfo.ScreenPoint);
        }