takuya-takeuchi / FaceRecognitionDotNet

The world's simplest facial recognition api for .NET on Windows, MacOS and Linux
MIT License
1.27k stars 309 forks source link

FaceLocations with Model.Cnn caused "External component has thrown an exception." #108

Closed programatix closed 3 years ago

programatix commented 4 years ago

Hi,

Following FaceDetection example project, I am able to call FaceLocations with Model.Hog. However when I tried Model.Cnn, I get the following exception,

External component has thrown an exception.
   at DlibDotNet.NativeMethods.LossMmod_operator_matrixs(Int32 id, IntPtr obj, MatrixElementType element_type, IntPtr[] matrix_array, Int32 matrix_array_len, Int32 templateRows, Int32 templateColumns, UInt32 batch_size, IntPtr& ret)
   at DlibDotNet.Dnn.LossMmod.Operator[T](IEnumerable`1 images, UInt64 batchSize)
   at DlibDotNet.Dnn.LossMmod.Operator[T](Matrix`1 image, UInt64 batchSize)
   at FaceRecognitionDotNet.Dlib.Python.CnnFaceDetectionModelV1.Detect(LossMmod net, Image image, Int32 upsampleNumTimes)
   at FaceRecognitionDotNet.FaceRecognition.RawFaceLocations(Image faceImage, Int32 numberOfTimesToUpsample, Model model)
   at FaceRecognitionDotNet.FaceRecognition.<FaceLocations>d__39.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at MCS.Biometrics.Engine.TakuyaTakeuchi.Face.TakuyaFaceBiometricImage.<>c__DisplayClass15_0.<DetectFace>b__0() in C:\Users\tspoh\SVN\Tools\MOS Personalization Tool\trunk\src\Shared\MCS.Biometrics\MCS.Biometrics.Engine.TakuyaTakeuchi.Face\TakuyaFaceBiometricImage.cs:line 87
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()

I'm using the models found in https://github.com/ageitgey/face_recognition_models/. Am I missing any components?

takuya-takeuchi commented 4 years ago

@programatix Did you use Cnn model in multi thread?

Cnn model does not support in multi thread. Please check this comment. https://github.com/takuya-takeuchi/FaceRecognitionDotNet/issues/24#issuecomment-458744002

programatix commented 4 years ago

I'm using AForge.Video to capture image, so the event CameraNewFrame is running on a different thread from the UI thread. So I tried running it in the main thread by doing something like this as below and it still crashed with the same exception.

if (mut.WaitOne(0))
{
  Application.Current.Dispatcher.Invoke(()=>
  {
    try
    {
      _FaceRecognition.FaceLocations(FaceImage, 0, Model.Cnn).ToArray();
    }
    finally
    {
      mut.ReleaseMutex();
    }
  }
}

The link you provided seems to indicate that it would crash if multiple thread running it at the same time. In the case above, I already placed a mutex to avoid that.

programatix commented 4 years ago

I tried putting it in Loaded event of a Window and it crashed too. Does this mean that it cannot be used inside WPF window? It runs fine in WPF Application_Startup event.

[update] I created a new project file and test it. It runs fine. It can already runs inside a Task. However, in my main project, it crashed. I tried removing some threads and I am able to get it to run without crashing. I'm still looking at it. Most of the threads are doing things such as discovering hardware (smartcard readers, fingerprint readers, iris camera and etc) and doesn't involve FaceRecognition. So, I'm stumped.