/// <summary>
 /// Detects the faces.
 /// </summary>
 /// <param name="imagePath">The image path.</param>
 /// <returns></returns>
 public Task <Image> DetectFaces(string imagePath)
 {
     return(_faceRecognition.DetectFaces(imagePath));
 }
Пример #2
0
        private void DisplayCam()
        {
            while (videoCapture.IsOpened)
            {
                Mat frame;
                try
                {
                    frame = videoCapture.QueryFrame();
                }
                catch
                {
                    LibraryDataIO.Instance.UILogger.LogError("Camera stopped.");
                    return;
                }

                var gray = new Mat();

                //Prepares a gray frame and increases its brightness & contrast
                CvInvoke.CvtColor(frame, gray, ColorConversion.Bgr2Gray);
                CvInvoke.EqualizeHist(gray, gray);

                var square = FrameSquarePicture(frame.Bitmap, camSize.Width, camSize.Height);
                FrameGrabbed?.Invoke(this, new FrameGrabbedEventArgs {
                    Frame = square
                });

                frame.Dispose();

                Rectangle[] facesDetected = faceRecognition.DetectFaces(gray);

                //Forget the previously detected face so if no faces were detected in current iteration,
                //the same value wouldn't get stored
                detectedFace = null;

                foreach (Rectangle face in facesDetected)
                {
                    var grayFace = new Mat(gray, face);
                    detectedFace = grayFace.ToImage <Gray, byte>();
                    gray.Dispose();
                    CvInvoke.Resize(detectedFace, detectedFace, new Size(100, 100), 0, 0, Inter.Cubic);

                    if (isTrained)
                    {
                        var label = faceRecognition.Recognize(detectedFace);
                        if (label != "")
                        {
                            tokenSource.Cancel();
                            ExistingUserRecognised?.Invoke(this, new FaceRecognisedEventArgs {
                                Label = label
                            });
                            return;
                        }
                    }

                    if (saved)
                    {
                        NewUserRegistered?.Invoke(this, new FaceRecognisedEventArgs {
                            Label = userLabel
                        });
                        return;
                    }

                    break;
                }
            }
        }