Multithreading Emgu CV人脸识别:在并行问题中对多个文件运行DetectHarCascade

Multithreading Emgu CV人脸识别:在并行问题中对多个文件运行DetectHarCascade,multithreading,emgucv,face-recognition,Multithreading,Emgucv,Face Recognition,我试图通过在多个文件上同时运行DetectShareCascade人脸检测来提高检测图像中人脸的速度。但我正在点击AccessViolationException,想知道是否有人有关于如何并行运行EMGU CV人脸检测的示例 下面是我编写的一个简单测试,其中有98个图像需要检测: [TestMethod] public void TestDetectParallel() { var face = new HaarCascade("haarcascade_f

我试图通过在多个文件上同时运行DetectShareCascade人脸检测来提高检测图像中人脸的速度。但我正在点击AccessViolationException,想知道是否有人有关于如何并行运行EMGU CV人脸检测的示例

下面是我编写的一个简单测试,其中有98个图像需要检测:

    [TestMethod]
    public void TestDetectParallel()
    {
        var face = new HaarCascade("haarcascade_frontalface_default.xml");
        var images =
            Directory.EnumerateFiles(Environment.CurrentDirectory, "*.jpg").AsParallel().Select(
                file => new Image<Gray, byte>(file));
        Parallel.ForEach(
            images, 
            image =>
            {
                image.DetectHaarCascade(face, 1.2, 10, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                        new Size(20, 20));
            });
    }
[TestMethod]
公共无效TestDetectParallel()
{
var face=new HaarCascade(“HaarCascade_frontalface_default.xml”);
var图像=
Directory.EnumerateFiles(Environment.CurrentDirectory,“*.jpg”).AsParallel()。选择(
文件=>新图像(文件));
并行ForEach(
图像,
图像=>
{
图像.DetectHarcascade(人脸,1.2,10,HAAR\u检测类型.DO\u CANNY\u修剪,
新尺寸(20,20));
});
}
因为我并行运行它,我遇到了一些多线程问题,堆栈跟踪:

System.AccessViolationException was unhandled by user code
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=Emgu.CV
  StackTrace:
       at Emgu.CV.CvInvoke.cvHaarDetectObjects(IntPtr image, IntPtr cascade, IntPtr storage, Double scaleFactor, Int32 minNeighbors, HAAR_DETECTION_TYPE flags, Size minSize)
       at Emgu.CV.Image`2.<>c__DisplayClassa.<DetectHaarCascade>b__6(IImage img, Int32 channel) in C:\Emgu\emgucv-windows-x86 2.3.0.1416\Emgu.CV\Image.cs:line 888
       at Emgu.CV.Image`2.ForEachDuplicateChannel[TReturn](Func`3 conv) in C:\Emgu\emgucv-windows-x86 2.3.0.1416\Emgu.CV\Image.cs:line 1229
       at Emgu.CV.Image`2.DetectHaarCascade(HaarCascade haarObj, Double scaleFactor, Int32 minNeighbors, HAAR_DETECTION_TYPE flag, Size minSize) in C:\Emgu\emgucv-windows-x86 2.3.0.1416\Emgu.CV\Image.cs:line 904
       at PhotosortService.UnitTest.UnitTest1.<>c__DisplayClass3.<TestDetectParallel>b__1(Image`2 image) in C:\Users\bchiu\Desktop\PhotosortService\PhotosortService.UnitTest\UnitTest1.cs:line 35
       at System.Threading.Tasks.Parallel.<>c__DisplayClass32`2.<PartitionerForEachWorker>b__30()
       at System.Threading.Tasks.Task.InnerInvoke()
       at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
       at System.Threading.Tasks.Task.<>c__DisplayClass7.<ExecuteSelfReplicating>b__6(Object )
       at System.Threading.Tasks.Task.ExecuteSelfReplicating(Task root)
       at System.Threading.Tasks.Task.Execute()
       at System.Threading.Tasks.Task.ExecutionContextCallback(Object obj)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
       at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution)
       at System.Threading.Tasks.ThreadPoolTaskScheduler.TryExecuteTaskInline(Task task, Boolean taskWasPreviouslyQueued)
       at System.Threading.Tasks.TaskScheduler.TryRunInline(Task task, Boolean taskWasPreviouslyQueued, Object threadStatics)
       at System.Threading.Tasks.Task.InternalRunSynchronously(TaskScheduler scheduler)
       at System.Threading.Tasks.Task.RunSynchronously(TaskScheduler scheduler)
       at System.Threading.Tasks.Parallel.PartitionerForEachWorker[TSource,TLocal](Partitioner`1 source, ParallelOptions parallelOptions, Action`1 simpleBody, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
       at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
       at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1 source, Action`1 body)
       at PhotosortService.UnitTest.UnitTest1.TestDetectParallel() in C:\Users\bchiu\Desktop\PhotosortService\PhotosortService.UnitTest\UnitTest1.cs:line 33
  InnerException: 
用户代码未处理System.AccessViolationException异常 消息=尝试读取或写入受保护内存。这通常表示其他内存已损坏。 来源=Emgu.CV 堆栈跟踪: 在Emgu.CV.CvInvoke.cvhaardeticobjects(IntPtr映像、IntPtr级联、IntPtr存储、双scaleFactor、Int32 minNeighbors、HAAR_检测类型标志、大小minSize) 在c:\Emgu\emgucv-windows-x86 2.3.0.1416\Emgu.CV\Image.cs:第888行中的Emgu.CV.Image`2.c\u显示ClassA.b\u 6(IImage img,Int32通道) 在C:\Emgu\emgucv-windows-x86 2.3.0.1416\Emgu.CV\Image.cs中的Emgu.CV.Image`2.ForEachDuplicateChannel[TReturn](Func`3 conv)处:第1229行 在C:\Emgu\emgucv-windows-x86 2.3.0.1416\Emgu.CV\Image.cs中的Emgu.CV.Image`2.DetectHaarCascade(HaarCascade-haarObj,双比例因子,Int32 minNeighbors,HAAR\u-DETECTION\u-TYPE标志,Size-minSize) 在PhotosortService.UnitTest.UnitTest1.c_中,以c:\Users\bchiu\Desktop\PhotosortService\PhotosortService.UnitTest\UnitTest1.cs第35行显示Class3.b_u1(图像`2图像) at System.Threading.Tasks.Parallel.c__;u DisplayClass32`2.b__u30() 在System.Threading.Tasks.Task.InnerInvoke()中 位于System.Threading.Tasks.Task.InnerInvokeWitchArg(任务子任务) 在System.Threading.Tasks.Task.c\u显示器上显示Class7.b\u 6(对象) 位于System.Threading.Tasks.Task.ExecuteSelfReplicating(任务根目录) 在System.Threading.Tasks.Task.Execute()中 位于System.Threading.Tasks.Task.ExecutionContextCallback(对象obj) 位于System.Threading.ExecutionContext.runTryCode(对象用户数据) 在System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode代码、CleanupCode backoutCode、Object userData)中运行 位于System.Threading.ExecutionContext.RunInternal(ExecutionContext ExecutionContext,ContextCallback回调,对象状态) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔ignoreSyncCtx) 位于System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task和currentTaskSlot) 在System.Threading.Tasks.Task.ExecuteEntry(布尔bPreventDoubleExecution) 位于System.Threading.Tasks.ThreadPoolTaskScheduler.TryExecuteTaskInline(任务任务,布尔任务先前已排队) 位于System.Threading.Tasks.TaskScheduler.TryRunLine(任务任务,布尔任务预先排队,对象线程静态) 在System.Threading.Tasks.Task.InternalRunSynchronously(TaskScheduler)上运行 在System.Threading.Tasks.Task.RunSynchronously(TaskScheduler)上运行 在System.Threading.Tasks.Parallel.PartitionerForEachWorker[TSource,TLocal](分区器'1 source,ParallelOptions ParallelOptions,Action'1 simpleBody,Action'2 bodyWithState,Action'3 bodyWithStateAndIndex,Func'4 bodyWithStateAndLocal,Func'5 bodyWithEverything,Func'1 localInit,Action'1 localFinally) 在System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source,ParallelOptions ParallelOptions,Action`1 body,Action`2 bodyWithState,Action`3 bodyWithStateAndIndex,Func`4 bodyWithStateAndLocal,Func`5 bodyWithEverything,Func`1 localInit,Action`1 localFinally) at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1 source,Action`1 body) 在C:\Users\bchiu\Desktop\photoportservice\photoportservice.UnitTest\UnitTest1.TestDetectParallel()中的photoportservice.UnitTest.UnitTest1.TestDetectParallel()处:第33行 内部异常:
多谢各位

通过给出一个
var face=new HaarCascade(“HaarCascade_frontalface_default.xml”)来解决对象到每个线程

[TestMethod]
public void TestDetectParallel()
{        
    var images =
        Directory.EnumerateFiles(Environment.CurrentDirectory, "*.jpg").AsParallel().Select(
            file => new Image<Gray, byte>(file));
    Parallel.ForEach(
        images, 
        image =>
        {
            var face = new HaarCascade("haarcascade_frontalface_default.xml");
            image.DetectHaarCascade(face, 1.2, 10, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                    new Size(20, 20));
        });
}
[TestMethod]
公共无效TestDetectParallel()
{        
var图像=
Directory.EnumerateFiles(Environment.CurrentDirectory,“*.jpg”).AsParallel()。选择(
文件=>新图像(文件));
并行ForEach(
图像,
图像=>
{
var face=new HaarCascade(“HaarCascade_frontalface_default.xml”);
图像.DetectHarcascade(人脸,1.2,10,HAAR\u检测类型.DO\u CANNY\u修剪,
新尺寸(20,20));
});
}

通过给出一个
var face=new HaarCascade(“HaarCascade\u frontalface\u default.xml”)来解决对象到每个线程

[TestMethod]
public void TestDetectParallel()
{        
    var images =
        Directory.EnumerateFiles(Environment.CurrentDirectory, "*.jpg").AsParallel().Select(
            file => new Image<Gray, byte>(file));
    Parallel.ForEach(
        images, 
        image =>
        {
            var face = new HaarCascade("haarcascade_frontalface_default.xml");
            image.DetectHaarCascade(face, 1.2, 10, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                    new Size(20, 20));
        });
}
[TestMethod]
公共无效TestDetectParallel()
{        
var图像=
Directory.EnumerateFiles(Environment.CurrentDirectory,“*.jpg”).AsParallel()。选择(
文件=>新图像(文件));
并行ForEach(
图像,
图像=>
{
var face=new HaarCascade(“HaarCascade_frontalface_default.xml”);
图像.DetectHarcascade(人脸,1.2,10,HAAR\u检测类型.DO\u CANNY\u修剪,
新尺寸(20,20));
});
}