Esempio n. 1
0
        void ProcessFrame(object sender, EventArgs e)
        {
            try
            {
                fps++;
                if (videoDevice == null)
                {
                    return;
                }
                for (int i = 0; i < 5; i++)
                {
                    videoDevice.QueryFrame();
                }
                Mat currentFrame = videoDevice.QueryFrame();

                //Mat tempFrame;
                //while(true)
                //{
                //    tempFrame = videoDevice.QueryFrame();
                //    if (tempFrame != null)
                //        currentFrame = tempFrame;
                //    else
                //        break;
                //}


                //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 (isExpertMode)
                {
                    BeginInvoke((MethodInvoker) delegate { tbConsole.Text += "\n" + type; });
                }


                pbView.Image = DrawController.DrawRectangle(faces, currentFrame, Color.Green, 1).ToBitmap();



                if (faces.Length != 0)
                {
                    // Standardize the Value (hsV)
                    // Increase contrast
                    //var enhanced = currentFrame.ToImage<Hsv, byte>().Copy();
                    //enhanced._EqualizeHist();
                    //enhanced._GammaCorrect(1.5d);
                    //for (int i = 0; i < enhanced.Size.Height; i++)
                    //    for (int j = 0; j < enhanced.Size.Width; j++)
                    //        enhanced.Data[i, j, 1] = 250;


                    var image = currentFrame.ToImage <Gray, byte>();

                    //var image = currentFrame.ToImage<Gray, byte>();

                    image.ROI = faces[0];
                    var faceImage = image.Copy().Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic);


                    if (registeredImages.Count == 0)
                    {
                        registeredImages.Add(faceImage.Convert <Gray, Byte>());
                        registeredImages.Add(faceImage.Convert <Gray, Byte>());
                        PictureBox pb = new PictureBox();
                        pb.Size     = new Size(50, 50);
                        pb.Image    = faceImage.ToBitmap();
                        pb.SizeMode = PictureBoxSizeMode.StretchImage;
                        flpFaces.Controls.Add(pb);
                    }
                    else
                    {
                        bool isMatched = face.IsFaceMatched(faceImage, registeredImages.ToArray());
                        pbFace.Image = faceImage.ToBitmap();

                        //flpFaces.Controls.Clear();
                        //for(int i=1;i< registeredImages.Count;i++)
                        //{
                        //    PictureBox pb = new PictureBox();
                        //    pb.Size = new Size(50, 50);
                        //    pb.Image = registeredImages[i].ToBitmap();
                        //    pb.SizeMode = PictureBoxSizeMode.StretchImage;
                        //    flpFaces.Controls.Add(pb);
                        //}

                        //if(isExpertMode)
                        //    BeginInvoke((MethodInvoker)delegate { tbConsole.Text = isMatched.ToString() ; });

                        if (!isMatched)
                        {
                            registeredImages.Add(faceImage.Convert <Gray, Byte>());
                            PictureBox pb = new PictureBox();
                            pb.Size     = new Size(50, 50);
                            pb.Image    = faceImage.ToBitmap();
                            pb.SizeMode = PictureBoxSizeMode.StretchImage;
                            flpFaces.Controls.Add(pb);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("--> " + ex.Message);
            }
        }