在'Emgu.CV.World.dll'中发生'Emgu.CV.Util.CvException' - 尝试预测时

时间:2016-09-06 04:31:42

标签: c# opencv emgucv face-detection face-recognition

我正在使用 EmguCV 3.1 开发人脸识别应用程序。我使用EigenFaceRecognizer作为识别算法。我尝试使用下面的代码训练图像。

    List<Image<Gray, byte>> trainingImages = new List<Image<Gray, byte>>();
    FaceRecognizer recognizer;
    List<Face> faceList = new List<Face>();
        ...
        ...
        recognizer = new EigenFaceRecognizer(80, double.PositiveInfinity);
        ...
        ...            
        Image<Gray, byte> image = new Image<Gray, byte>(imgBox2.DisplayedImage.Bitmap);
        trainingImages.Add(image);
        List<int> trainigLabels = new List<int>();
        recognizer.Train(trainingImages.ToArray(), trainigLabels.ToArray());
        recognizer.Save("TraningData");
        faceList.Add(new Face(image.Bytes.Length, txtName.Text));
        ...
        ...
        Image<Gray, byte> image = new Image<Gray, byte>(imgBox2.DisplayedImage.Bitmap);
            recognizer.Load("TraningData");
            try
            {
                var result = recognizer.Predict(image);
                MessageBox.Show(result.Label.ToString());
            }
            catch (Emgu.CV.Util.CvException ex)
            {
                Console.WriteLine(ex);
            }

但是当调用此代码时,它会给我以下错误。

A first chance exception of type 'Emgu.CV.Util.CvException' occurred in Emgu.CV.World.dll
The program '[1136] IPTest.vshost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.
The program '[1136] IPTest.vshost.exe: Program Trace' has exited with code 0 (0x0).

尝试使用recognizer.Train()时会发生这种情况。我做错了什么?

更新
经过一些试验和错误,我知道问题在于识别器.Predict()方法。
当我使用try / catch时,它显示以下异常

Emgu.CV.Util.CvException: OpenCV: The matrix is not continuous, thus its number of rows can not be changed

1 个答案:

答案 0 :(得分:-1)

好的,我找到了解决方案。问题不在我的代码中。当本机代码试图访问某些已损坏或无法访问的内存位置时,它会抛出。
所以我找到了一个简单易用的解决方案。

try
{
   var result = recognizer.Predict(image);
}
catch (System.AccessViolationException)
{
   recognier =  new EigenFaceRecognizer(80, double.PositiveInfinity);
}

但是你无法在运行时捕获AccessViolationException。所以你必须改变设置,以便你可以。为此,您可以关注this question和答案。