Search code examples
c#asp.netemgucvface-detection

Face Detection on a video file


I want to detect face from an input video file using "haar cascade". I have converted the video into frames using this code. Please tell me how to detect face from these frames and mark it in a rectangular border.

 private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {

                memde = new MediaDetClass();
                System.IO.Directory.CreateDirectory("temp");
                memde.Filename = openFileDialog1.FileName;
                int len = (int)memde.StreamLength;
                counter = 0;
                Image img;
                memde.Filename = openFileDialog1.FileName;
                memde.CurrentStream = 0;
                float percent = 0.002f;
                Image<Gray, byte> gray;
                for (float i = 0.0f; i < len; i = i + (float)(percent * len))
                {
                    counter++;
                    string fbitname = storagepath + counter.ToString();
                    memde.WriteBitmapBits(i, 850, 480, fbitname + ".bmp");

                    }
                }
        }
    }

Solution

  • I would suggest using the capture class for your video file and base your code on this example:

    http://www.emgu.com/wiki/index.php?title=Video_Files

    Then replace relevant part of the ProcessFrame() method with:

    if (CurrentState == VideoMethod.Viewing)
    {
        frame = _Capture.RetrieveBgrFrame();
        if (frame != null)
        {
            using (Image<Gray, Byte> gray = frame.Convert<Gray, Byte>()) //Convert it to Grayscale
            {
                //normalizes brightness and increases contrast of the image
                gray._EqualizeHist();
    
                //Detect the faces  from the gray scale image and store the locations as rectangle
                //The first dimensional is the channel
                //The second dimension is the index of the rectangle in the specific channel
                Rectangle[] facesDetected = face.DetectMultiScale(
                                   gray,
                                   1.1,
                                   10,
                                   new Size(20, 20),
                                   Size.Empty);
    
                foreach (Rectangle f in facesDetected)
                {
                    //Draw the rectangle on the frame
                    frame.Draw(f, new Bgr(Color.Red), 2);
                }
            }
    
            //Show image
            DisplayImage(frame.ToBitmap());
        }
    }
    

    Cheers,

    Chris