示例#1
0
        private void GetLandmarks(OpenCvSharp.Mat frame, OpenCvSharp.Rect face, List <System.Drawing.Rectangle> rfaces)
        {
            EyePoints      rightEye  = new EyePoints(leftEye: true);
            EyePoints      leftEye   = new EyePoints(leftEye: false);
            ShapePredictor predictor = ShapePredictor.Deserialize(shapePredictorDataFile);

            //Scalar eyecolor = new Scalar(0, 0, 255);
            Array2D <byte>      gray      = ConvertMatToDlib2DArray(frame);
            FullObjectDetection landmarks = predictor.Detect(gray, ConvertToDlib(face));

            InitializeEyes(landmarks, leftEye, rightEye);
            //DrawEye(que, landmarks, leftEye);
            //DrawEye(que, landmarks, rightEye);
            Rect leftboundingBox = BoundingBoxAroundEye(leftEye, 0);

            rfaces.Add(FromOpenCvRect(leftboundingBox));
            //DrawRect(frame, leftboundingBox);
            OpenCvSharp.Point centerOfLeftEye = DetectCenterOfEye(frame, leftboundingBox);
            centerOfLeftEye.X += leftboundingBox.X;

            Rect rightboundingBox = BoundingBoxAroundEye(rightEye, 0);

            rfaces.Add(FromOpenCvRect(rightboundingBox));
            //DrawRect(frame, rightboundingBox);
            OpenCvSharp.Point centerOfRightEye = DetectCenterOfEye(frame, rightboundingBox);
            centerOfRightEye.X += rightboundingBox.X;

            EyeDirection leftEyeDirection  = leftEye.GetEyePosition(centerOfLeftEye);
            EyeDirection rightEyeDirection = rightEye.GetEyePosition(centerOfRightEye);

            //EyeDirection eyeDirection = EyeDirection.unknown;
            //if (leftEyeDirection == EyeDirection.center || rightEyeDirection == EyeDirection.center) eyeDirection = EyeDirection.center;
            //else if (leftEyeDirection == EyeDirection.left) eyeDirection = EyeDirection.left;
            //else if (rightEyeDirection == EyeDirection.right) eyeDirection = EyeDirection.right;

            //OpenCvSharp.Point position = new OpenCvSharp.Point(50, 50);
            //Cv2.PutText(img: frame, text: eyeDirection.ToDisplay(), org: position, fontFace: HersheyFonts.HersheySimplex, fontScale: 2, new Scalar(0, 0, 255));
        }
示例#2
0
        private void StartWebCam(BackgroundWorker worker = null)
        {
            if (cap == null)
            {
                cap = new VideoCapture(0);
            }
            if (!cap.Open(0))
            {
                return;
            }
            OpenCvSharp.Cv2.NamedWindow("Video", WindowMode.AutoSize);
            int       cnt      = 0;
            Mat       frame    = new Mat();
            EyePoints rightEye = new EyePoints(true);
            EyePoints leftEye  = new EyePoints(false);

            IsRunning = true;
            while (IsRunning)
            {
                bool result = cap.Read(frame);
                if (!result)
                {
                    worker.CancelAsync();
                    IsRunning = false;
                }
                if (frame != null && (frame.Rows * frame.Cols > 0))
                {
                    cnt++;
                    if (cnt % frameskip == 0)
                    {
                        FrameQueue.Enqueue(frame);
                        cnt = 0;
                    }
                }
                while (FrameQueue.Count > 0)
                {
                    Mat    que   = FrameQueue.Dequeue();
                    Rect[] faces = GetFaces(que, 1);
                    for (int i = 0; i < faces.Length; i++)
                    {
                        //GetFaceInRect(faces[i], que, i);
                        Scalar              eyecolor  = new Scalar(0, 0, 255);
                        Array2D <byte>      gray      = ConvertMatToDlib2DArray(que);
                        FullObjectDetection landmarks = predictor.Detect(gray, ConvertToDlib(faces[i]));
                        InitializeEyes(landmarks, leftEye, rightEye);
                        //DrawEye(que, landmarks, leftEye);
                        //DrawEye(que, landmarks, rightEye);
                        Rect leftboundingBox = BoundingBoxAroundEye(leftEye, 0);
                        DrawRect(que, leftboundingBox);
                        OpenCvSharp.Point centerOfLeftEye = DetectCenterOfEye(que, leftboundingBox);
                        centerOfLeftEye.X += leftboundingBox.X;

                        Rect rightboundingBox = BoundingBoxAroundEye(rightEye, 0);
                        DrawRect(que, rightboundingBox);
                        OpenCvSharp.Point centerOfRightEye = DetectCenterOfEye(que, rightboundingBox);
                        centerOfRightEye.X += rightboundingBox.X;

                        EyeDirection leftEyeDirection  = leftEye.GetEyePosition(centerOfLeftEye);
                        EyeDirection rightEyeDirection = rightEye.GetEyePosition(centerOfRightEye);

                        EyeDirection eyeDirection = EyeDirection.unknown;
                        if (leftEyeDirection == EyeDirection.center || rightEyeDirection == EyeDirection.center)
                        {
                            eyeDirection = EyeDirection.center;
                        }
                        else if (leftEyeDirection == EyeDirection.left)
                        {
                            eyeDirection = EyeDirection.left;
                        }
                        else if (rightEyeDirection == EyeDirection.right)
                        {
                            eyeDirection = EyeDirection.right;
                        }

                        OpenCvSharp.Point position = new OpenCvSharp.Point(50, 50);
                        Cv2.PutText(img: que, text: eyeDirection.ToDisplay(), org: position, fontFace: HersheyFonts.HersheySimplex, fontScale: 2, new Scalar(0, 0, 255));
                    }
                    //BitmapImage bmi = ConvertToBMI(frame, cnt, "D:/junk/TestCamImages");
                    if (worker != null)
                    {
                        //worker.ReportProgress(cnt, bmi);
                        try
                        {
                            OpenCvSharp.Cv2.ImShow("Video", que);
                            int key = Cv2.WaitKey(10);   // as in 10 milliseconds
                            if (key == 27)
                            {
                                worker.CancelAsync();
                                IsRunning = false;
                            }
                        }
                        catch (Exception ex)
                        {
                            string msg = ex.Message;
                        }
                    }
                    if (worker.CancellationPending)
                    {
                        Cv2.DestroyWindow("Video");
                        break;
                    }
                }
            }
        }
示例#3
0
        private void StartWebCam()
        {
            bool useHaarcascade = true;

            if (cap == null)
            {
                cap = new VideoCapture(0);
            }
            if (!cap.Open(0))
            {
                return;
            }
            OpenCvSharp.Cv2.NamedWindow("Video", WindowMode.AutoSize);

            EyePoints rightEye = new EyePoints(true);
            EyePoints leftEye  = new EyePoints(false);

            IsRunning = true;
            while (IsRunning)
            {
                frame = new Mat();
                bool result = cap.Read(frame);
                if (!result)
                {
                    IsRunning = false;
                    break;
                }

                Rect[] faces = useHaarcascade ? GetHaarcascadeFaces(frame, 1) : GetDnnFaces(frame, 1);
                if (faces == null)
                {
                    continue;
                }

                for (int i = 0; i < faces.Length; i++)
                {
                    //GetFaceInRect(faces[i], que, i);
                    Scalar              eyecolor  = new Scalar(0, 0, 255);
                    Array2D <byte>      gray      = ConvertMatToDlib2DArray(frame);
                    FullObjectDetection landmarks = predictor.Detect(gray, ConvertToDlib(faces[i]));
                    InitializeEyes(landmarks, leftEye, rightEye);
                    //DrawEye(que, landmarks, leftEye);
                    //DrawEye(que, landmarks, rightEye);
                    Rect leftboundingBox = BoundingBoxAroundEye(leftEye, 0);
                    DrawRect(frame, leftboundingBox);
                    OpenCvSharp.Point centerOfLeftEye = DetectCenterOfEye(frame, leftboundingBox);
                    centerOfLeftEye.X += leftboundingBox.X;

                    Rect rightboundingBox = BoundingBoxAroundEye(rightEye, 0);
                    DrawRect(frame, rightboundingBox);
                    OpenCvSharp.Point centerOfRightEye = DetectCenterOfEye(frame, rightboundingBox);
                    centerOfRightEye.X += rightboundingBox.X;

                    EyeDirection leftEyeDirection  = leftEye.GetEyePosition(centerOfLeftEye);
                    EyeDirection rightEyeDirection = rightEye.GetEyePosition(centerOfRightEye);

                    EyeDirection eyeDirection = EyeDirection.unknown;
                    if (leftEyeDirection == EyeDirection.center || rightEyeDirection == EyeDirection.center)
                    {
                        eyeDirection = EyeDirection.center;
                    }
                    else if (leftEyeDirection == EyeDirection.left)
                    {
                        eyeDirection = EyeDirection.left;
                    }
                    else if (rightEyeDirection == EyeDirection.right)
                    {
                        eyeDirection = EyeDirection.right;
                    }

                    OpenCvSharp.Point position = new OpenCvSharp.Point(50, 50);
                    Cv2.PutText(img: frame, text: eyeDirection.ToDisplay(), org: position, fontFace: HersheyFonts.HersheySimplex, fontScale: 2, new Scalar(0, 0, 255));
                }

                try
                {
                    OpenCvSharp.Cv2.ImShow("Video", frame);
                    int key = Cv2.WaitKey(10);   // as in 10 milliseconds
                    if (key == 27)
                    {
                        IsRunning = false;
                    }
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                }
                frame.Dispose();
            }
        }