2012-03-05 2 views
1

여러 파일에서 DetectHaarCascade 얼굴 검색을 동시에 실행하여 이미지에서 얼굴을 감지하는 속도를 향상시키고 있습니다. 하지만 AccessViolationException을 치는 중입니다. 누구든지 EMGU CV 얼굴 검색을 병렬로 실행하는 방법에 대한 예제가 있는지 궁금합니다. 여기 Emgu CV 얼굴 인식 : 여러 파일에 대해 DetectHaarCascade를 병렬로 실행하는 중

내가 98 개 이미지가 내가 쓴 간단한 테스트를 감지하는 것입니다

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: 
:

[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)); 
      }); 
    } 

내가 병렬로 그것을 실행하고 있기 때문에, 나는 스택 추적과 일부 멀티 스레드 문제를 명중

대단히 감사합니다!

답변

1

각 스레드에 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)); 
     }); 
}