视频文件上的人脸检测

时间:2014-03-09 09:57:20

标签: c# asp.net emgucv face-detection

我想使用“haar cascade”从输入视频文件中检测脸部。我已使用此代码将视频转换为帧。请告诉我如何从这些框架中检测面部并将其标记为矩形边框。

 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");

                    }
                }
        }
    }

1 个答案:

答案 0 :(得分:3)

我建议您为您的视频文件使用捕获类,并根据此示例创建代码:

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

然后用:

替换ProcessFrame()方法的相关部分
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());
    }
}

干杯,

克里斯