视频文件的人脸检测

本文关键字:检测 文件 视频 | 更新日期: 2023-09-27 17:49:18

我想使用"haar级联"从输入视频文件中检测人脸。我用这个代码将视频转换成帧。请告诉我如何从这些框架中检测人脸并在矩形边框中标记。

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

视频文件的人脸检测

我建议为您的视频文件使用捕获类,并基于以下示例编写代码:

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());
    }
}

欢呼,克里斯