EMgucv保存从网络摄像头检测到的多个脸部

时间:2017-07-18 06:22:10

标签: c# emgucv face-recognition

我有一个窗口应用程序,它将检测多个面部并捕获面部并保存为图像。我使用EMGU CV进行人脸检测,我从示例代码和教程中学习。当需要将面部捕获为经过训练的数据集时,它只能存储一个面部,尽管它检测到两个面部。我尝试修改代码,但似乎没有按预期工作。以下是代码:

 gray = grabber.QueryGrayFrame().Resize(320, 240, 
 Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);

MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
            face,
            1.2,
            10,
            Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
            new Size(20, 20));

foreach (MCvAvgComp f in facesDetected[0])
{
    TrainedFace = currentFrame.Copy(f.rect).Convert<Gray, byte>();

    TrainedFace = result.Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
    trainingImages.Add(TrainedFace);

    for (int i = 1; i < trainingImages.ToArray().Length + 1; i++)
    {
        trainingImages.ToArray()[i - 1].Save(Application.StartupPath + "/TrainedFaces/face" + i + ".bmp");

    }
}

当检测到2个面时,foreach循环将循环两次,但是面部保存是相同的。知道出了什么问题吗?

1 个答案:

答案 0 :(得分:0)

请尝试以下方式

对于检测到的每个脸部,您必须将其添加到训练图像中 并且你需要将它循环到每个循环的一侧

gray = grabber.QueryGrayFrame().Resize(320, 240, 
Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);

MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
        face,
        1.2,
        10,
        Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
        new Size(20, 20));

foreach (MCvAvgComp f in facesDetected[0])
{
TrainedFace = currentFrame.Copy(f.rect).Convert<Gray, byte>();

TrainedFace = result.Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
trainingImages.Add(TrainedFace);
}

 for (int i = 1; i < trainingImages.ToArray().Length + 1; i++)
{
    trainingImages.ToArray()[i - 1].Save(Application.StartupPath + "/TrainedFaces/face" + i + ".bmp");

 }

我认为for循环可以改为

int i=1;
foreach(TrainedFace Tface in trainingImages)
  {
   Tface.Save(Application.StartupPath + "/TrainedFaces/face" + i + ".bmp")
    i++;
   }

请尝试这种方式,它可能会有效..