Example #1
0
        private void recognizeButton_Click(object sender, EventArgs e)
        {
            Webcam.Retrieve(Frame);
            var imageFrame = Frame.ToImage <Gray, byte>();

            if (imageFrame != null)
            {
                var faces = FaceDetection.DetectMultiScale(imageFrame, 1.3, 5);
                Console.WriteLine($"Faces detected: {faces.Count()}");
                if (faces.Count() != 0)
                {
                    var processedImage = imageFrame.Copy(faces[0]).Resize(ProcessedImageWidth, ProcessedImageHeight, Emgu.CV.CvEnum.Inter.Cubic);
                    try
                    {
                        var result = FaceRecognition.Predict(processedImage);
                        Console.WriteLine(CheckRecognizeResults(result, _threshold));
                    }
                    catch (Exception ex)
                    {
                        //Console.WriteLine("No faces trained, can't recognize");
                    }
                }
                else
                {
                    //Console.WriteLine("No faces found");
                }
            }
        }
        private void Timer_Tick1(object sender, EventArgs e)
        {
            Webcam.Retrieve(Frame);
            var imageFrame = Frame.ToImage <Gray, byte>();


            if (imageFrame != null)
            {
                var faces = FaceDetection.DetectMultiScale(imageFrame, 1.3, 5);

                if (faces.Count() != 0)
                {
                    Image <Gray, byte> processedImage = imageFrame.Copy(faces[0]).Resize(ProcessedImageWidth, ProcessedImageHeight, Inter.Cubic);
                    result = FaceRecognition.Predict(processedImage);

                    foreach (var id in listOfIds)
                    {
                        if (result.Label == id)
                        {
                            isUnknown   = false;
                            isPredicted = true;
                        }
                        else
                        {
                            isUnknown = true;
                        }
                    }
                }
            }
        }
        public void Webcam_ImageGrabbed(object sender, EventArgs e)
        {
            Webcam.Retrieve(Frame);
            var ImageFrame = Frame.ToImage <Bgr, byte>();

            if (ImageFrame != null)
            {
                var grayFrame = ImageFrame.Convert <Gray, byte>();
                var faces     = FaceDetection.DetectMultiScale(grayFrame, 1.3, 5); // Et array af firkanter, som holder alle ansigter den finder.
                // var eyes = EyeDetection.DetectMultiScale(grayFrame, 1.3, 5); // Samme med øjne

                foreach (var face in faces)
                {
                    ImageFrame.Draw(face, new Bgr(Color.LimeGreen), 3);

                    if (result.Label != -1 || isPredicted && doneTraining)
                    {
                        try
                        {
                            Graphics graphicImage1 = Graphics.FromImage(ImageFrame.Bitmap);
                            graphicImage1.DrawString(listOfNames[result.Label - 1],
                                                     new Font("Arial",
                                                              15, FontStyle.Bold),
                                                     new SolidBrush(Color.LimeGreen),
                                                     new Point(face.X, face.Y));
                        }
                        catch (Exception exception)
                        {
                            //No action as the error is useless, it is simply an error in
                            //no data being there to process and this occurss sporadically
                        }
                    }

                    if (result.Label == 0 || result.Label == -1)
                    {
                        Graphics graphicImage2 = Graphics.FromImage(ImageFrame.Bitmap);
                        graphicImage2.DrawString("Unknown",
                                                 new Font("Arial",
                                                          15, FontStyle.Bold),
                                                 new SolidBrush(Color.Red),
                                                 new Point(face.X, face.Y));
                    }
                }

                // ID checker
                Graphics graphicImage = Graphics.FromImage(ImageFrame.Bitmap);
                graphicImage.DrawString($"Face ID: " + result.Label.ToString(),
                                        new Font("Arial",
                                                 15, FontStyle.Bold),
                                        new SolidBrush(Color.LimeGreen),
                                        new Point(0, 50));



                WebcamBox.Image = ImageFrame.ToBitmap(); // shows frames in the UI.
            }
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            Webcam.Retrieve(Frame);
            var imageFrame = Frame.ToImage <Gray, byte>();

            if (TimerCounter < TimeLimit)
            {
                TimerCounter++;

                if (imageFrame != null)
                {
                    Rectangle[] faces = FaceDetection.DetectMultiScale(imageFrame, 1.3, 5);

                    if (faces.Any())
                    {
                        var processedImage = imageFrame.Copy(faces[0]).Resize(ProcessedImageWidth, ProcessedImageHeight, Inter.Cubic); // Will zoom into the rectangle it finds to only see that.
                        Faces.Add(processedImage.Mat);
                        IDs.Add(userId);
                        ScanCounter++;
                        OutputBox.AppendText($"{ScanCounter} Successful Scans Taken...{Environment.NewLine}");
                        OutputBox.ScrollToCaret();
                    }
                }
            }
            else
            {
                FaceRecognition.Train(new VectorOfMat(Faces.ToArray()), new VectorOfInt(IDs.ToArray())); // Here we finally train on face and ID collection we just captures and is written to the pathfile "YMLPath"
                FaceRecognition.Write(YMLPath);

                Timer.Stop();
                TimerCounter = 0;

                //IDBox.Clear();
                nameBox.Clear();

                TrainButton.Enabled = !TrainButton.Enabled;
                //IDBox.Enabled = !IDBox.Enabled;
                nameBox.Enabled = !nameBox.Enabled;

                OutputBox.AppendText($"Training Complete! {Environment.NewLine}");
                //MessageBox.Show("Training Complete");

                doneTraining = true;

                Timer          = new Timer();
                Timer.Interval = 500;         // ticks every 0.5 sec
                Timer.Tick    += Timer_Tick1; // this method gets called every time the timer fires.
                Timer.Start();
            }
        }
Example #5
0
        private void Webcam_ImageGrabbed(object sender, EventArgs e)
        {
            currentPeople = new List <Person>();

            Webcam.Retrieve(Frame);
            var imageFrame = Frame.ToImage <Bgr, byte>();

            if (imageFrame != null)
            {
                var grayFrame = imageFrame.Convert <Gray, byte>();
                var faces     = FaceDetection.DetectMultiScale(grayFrame, 1.3, 5);
                //var eyes = EyeDetection.DetectMultiScale(grayFrame, 1.3, 5);

                if ((flags & (byte)~Flags.FACE_SQUARE) != 0 && faces.Count() != 0)
                {
                    foreach (var face in faces)
                    {
                        var processedImage = grayFrame.Copy(face).Resize(ProcessedImageWidth, ProcessedImageHeight, Emgu.CV.CvEnum.Inter.Cubic);
                        try
                        {
                            var result = FaceRecognition.Predict(processedImage);
                            //var id = CheckRecognizeResults(result, _threshold);
                            var recognisedPerson  = CheckRecognizeResults(result, _threshold);
                            var personNameIfFound = recognisedPerson == null
                                        ? "Spooky ghost no. " + result.Label.ToString()
                                        : recognisedPerson.Name;
                            imageFrame.Draw(personNameIfFound, face.Location, FontFace.HersheyTriplex, 1.0, new Bgr(Color.Chartreuse));                                 //since we dont store person objects over restarts we wont have any info about the id
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Message: " + ex.Message + " Data: " + ex.Data);
                        }
                        imageFrame.Draw(face, new Bgr(Color.BurlyWood), 3);
                    }
                }

                /*if (EyeSquare)
                 * {
                 *  foreach (var eye in eyes)
                 *  {
                 *      imageFrame.Draw(eye, new Bgr(Color.Yellow), 3);
                 *  }
                 * }*/
                picLiveFeed.Image = imageFrame.ToBitmap();
            }
        }