System.AccessViolationException 在 VideoCapture.Retrieve() - EmguCV / OpenCV

System.AccessViolationException on VideoCapture.Retrieve() - EmguCV / OpenCV

我是打开简历的新手,我正在尝试使用人脸识别功能,但 运行 有一次遇到了麻烦。在三种情况之一中,调用 VideoCapture 对象的 Retrieve() 方法会抛出 System.AccessViolationException。我发现了很多关于这个问题的主题,但没有解决方案。

这是我得到的 StackTrace:

bei Emgu.CV.CvInvoke.cveVideoCaptureRetrieve(IntPtr capture, IntPtr image, Int32 flag)

bei Emgu.CV.VideoCapture.Retrieve(IOutputArray image, Int32 channel)

bei OpenCVGenericAssembly.OpenCVGenericAssembly.Capture(String sensorSerialNo, FeatureType feature, Boolean compressed, Int32 timeOut) in C:\Users\sl\Documents\Source\Share\OpenCVGenericAssembly\OpenCVGenericAssembly\OpenCVGenericAssembly.cs:Zeile 81.

bei OpenCVGenericAssembly.OpenCVGenericAssembly.Enroll(String sensorSerialNo, String firstName, String lastName, String company, FeatureType feature, Int32 templateDestination, Boolean compressed, Int32 timeOut, String connectionString, String templateQuery) in C:\Users\sl\Documents\Source\Share\OpenCVGenericAssembly\OpenCVGenericAssembly\OpenCVGenericAssembly.cs:Zeile 125.

bei Testing.Program.Main(String[] args) in C:\Users\sl\Documents\Source\Share\OpenCVGenericAssembly\Testing\Program.cs:Zeile 20.

我正在调用一个 Enroll 方法,它除了调用一个 Capture-Method 并等待其响应外什么都不做。 Capture-Method 应 运行,直到它恰好检测到一张脸,然后返回。这就是 Capture-Method 的样子:

public DResponse Capture(string sensorSerialNo, FeatureType feature, bool compressed = false, int timeOut = 0)
{
    capture = new VideoCapture(); 
    DResponse rsp = DResponse();

    while(string.IsNullOrWhiteSpace(rsp.templateData))
    {
        using (Mat mat = new Mat())
        {
            capture.Retrieve(mat);
            Image<Bgr, Byte> currentFrame = mat.ToImage<Bgr, Byte>();

            if (currentFrame != null)
            {
                Image<Gray, Byte> grayFrame = currentFrame.Convert<Gray, Byte>();

                Rectangle[] detectedFaces = cascadeClassifier.DetectMultiScale(grayFrame, DMS_SCALE_FACTORS, DMS_MIN_NEIGHBORS);

                if (detectedFaces.Length == 1)
                {
                    Image<Gray, byte> result = currentFrame.Copy(detectedFaces[0]).Convert<Gray, byte>().Resize(IMG_WIDTH, IMG_HEIGHT, Emgu.CV.CvEnum.Inter.Cubic);
                    result._EqualizeHist();
                    rsp.templateData = Convert.ToBase64String(result.Bytes);
                    break;
                }
                Thread.Sleep(100);
            }
        }
    }

    return rsp;
}

我先尝试了一个教程。这是一个 wpf 应用程序,显示视频流和检测到的面部周围的框架(如果识别出一个人,则加上一个名字)。它非常相似,但他们在教程中使用了 DispatcherTimer,我不能使用它,因为我的代码应该用作程序集。不管怎样,这段代码不会抛出这个错误,所以也许这会帮助别人在我上面的源代码中捕获问题。

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    capture = new VideoCapture();
    haarCascade = new CascadeClassifier(System.AppDomain.CurrentDomain.BaseDirectory + "haarcascade_frontalface_alt_tree.xml");
    timer = new DispatcherTimer();
    timer.Tick += new EventHandler(timer_Tick);
    timer.Interval = new TimeSpan(0, 0, 0, 0, 1);
    timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
    Mat mat = new Mat();
    capture.Retrieve(mat);
    Image<Bgr, Byte> currentFrame = mat.ToImage<Bgr, Byte>();

    if (currentFrame != null)
    {
        Image<Gray, Byte> grayFrame = currentFrame.Convert<Gray, Byte>();
        Rectangle[] detectedFaces = haarCascade.DetectMultiScale(grayFrame, 1.1, 1);

        for (int i = 0; i < detectedFaces.Length; i++)
        {
            result = currentFrame.Copy(detectedFaces[i]).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic);
            result._EqualizeHist();

            currentFrame.Draw(detectedFaces[i], new Bgr(System.Drawing.Color.Green), 3);

            if (eigenRecog.IsTrained)
            {
                // do some stuff
            }
        }

        image1.Source = ToBitmapSource(currentFrame);
    }
}

有什么提示吗?任何问题?我感谢每一个输入! stl

必须处理 VideoCapture 对象(手动或将其放入 using 块中)。这解决了问题。