Esempio n. 1
0
        void ProcessFrame(object sender, EventArgs e)
        {
            //FindAverageFrameTime();

            fps++;
            if (videoDevice == null)
            {
                return;
            }
            try
            {
                for (int i = 0; i < 10; i++)
                {
                    videoDevice.QueryFrame();
                }

                Mat currentFrame = videoDevice.QueryFrame();
                //Emgu.CV.CvInvoke.Resize(currentFrame, currentFrame, new Size(), 0.5, 0.5,Emgu.CV.CvEnum.Inter.Cubic);
                //CvInvoke.GaussianBlur(currentFrame, currentFrame,new Size(5,5),2);


                int         type  = 0;
                Rectangle[] faces = face.FindAllFaces(currentFrame, ref type);


                if (faces.Length != 0)
                {
                    var image     = currentFrame.ToImage <Gray, Byte>();
                    var colorFace = currentFrame.ToImage <Bgr, byte>();
                    image.ROI     = faces[0];
                    colorFace.ROI = faces[0];
                    Image <Gray, Byte> faceImage = image.Copy().Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic);

                    double distance = 0;
                    int    personId = face.MatchedFace(faceImage, ref distance);

                    //if (personId != -1)
                    //{


                    int guess = GuessFace(personId);
                    if (guess == -1)
                    {
                        // Unauthorized
                        pbView.Image = DrawController.DrawRectangle(faces, currentFrame, Color.Red, 2).ToBitmap();

                        for (int i = 0; i < unauthorized.Count; i++)
                        {
                            if (unauthorized[i].id == personId)
                            {
                                return;
                            }
                        }
                        unauthorized.Add(new PersonInfo(personId, "unknown", colorFace.Copy().Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic).ToBitmap()));
                        MessageBox.Show("Unauthorized Person Detected", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (guess == -2)
                    {
                        pbView.Image = DrawController.DrawRectangle(faces[0], "Working...", currentFrame.ToImage <Bgr, byte>(), Color.Yellow, 2).ToBitmap();
                    }
                    else
                    {
                        // Authorized

                        string personName = face.GetPersonName(personId);
                        pbView.Image = DrawController.DrawRectangle(faces[0], personName, currentFrame.ToImage <Bgr, Byte>(), Color.Green, 2).ToBitmap();
                        //BeginInvoke((MethodInvoker)delegate { tbConsole.Text += "\n" + pr.Label.ToString()+" Detected"; });

                        for (int i = 0; i < authorized.Count; i++)
                        {
                            if (authorized[i].id == personId)
                            {
                                return;
                            }
                        }

                        authorized.Add(new PersonInfo(personId, personName, colorFace.Copy().Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic).ToBitmap()));
                    }
                    //}
                    //else
                    //{
                    //    // Face Mismatched
                    //    pbView.Image = DrawController.DrawRectangle(faces, currentFrame, Color.Red, 2).ToBitmap();
                    //}

                    BeginInvoke((MethodInvoker) delegate { tbConsole.Text = distance + " " + personId.ToString(); });
                }
                else
                {
                    pbView.Image = currentFrame.Bitmap;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.InnerException);
            }
        }