gpt4 book ai didi

c# - VideoCapture.Retrieve()上的System.AccessViolationException-EmguCV/OpenCV

转载 作者:行者123 更新时间:2023-12-02 17:38:05 28 4
gpt4 key购买 nike

我是刚打开简历的新人,我正在尝试使用面部识别功能,但有一次遇到麻烦。在以下三种情况之一中,在VideoCapture-Object上调用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的样子:
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-Object(手动或将其放入using块中)。那解决了问题。

关于c# - VideoCapture.Retrieve()上的System.AccessViolationException-EmguCV/OpenCV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48223930/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com