takuya-takeuchi / FaceRecognitionDotNet

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

AccessViolationException #48

Closed pick30 closed 5 years ago

pick30 commented 5 years ago

My english is bad! When I use the lib,I often get Exception at these two places: recognition.FaceLocations(image).ToArray() recognition.FaceEncodings(image).ToArray() the Exception is : System.AccessViolationException: Attempted to read or write protected memory This is often an indication that other memory is corrupt

at DlibDotNet.NativeMethods.frontal_face_detector_matrix_operator2(IntPtr detector, MatrixElementType imgType, IntPtr img, Double adjustThreshold, IntPtr dets) at DlibDotNet.FrontalFaceDetector.Operator(MatrixBase image, IEnumerable1& detections, Double threshold) at FaceRecognitionDotNet.Dlib.Python.SimpleObjectDetector.RunDetectorWithUpscale1(FrontalFaceDetector detector, Image img, UInt32 upsamplingAmount, Double adjustThreshold, List1 detectionConfidences, List1 weightIndices) at FaceRecognitionDotNet.Dlib.Python.SimpleObjectDetector.RunDetectorWithUpscale2(FrontalFaceDetector detector, Image image, UInt32 upsamplingAmount) at FaceRecognitionDotNet.FaceRecognition.RawFaceLocations(Image faceImage, Int32 numberOfTimesToUpsample, Model model) at FaceRecognitionDotNet.FaceRecognition.<FaceLocations>d__18.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source)

My c# code is like this: lock (locker) { Bitmap bitmap = (Bitmap)bmp.Clone(); var array2d = bitmap.ToArray2D(); var image = FaceRecognition.LoadImage(array2d.ToBytes(), array2d.Rows, array2d.Columns, 3);
Location[] locations = recognition.FaceLocations(image).ToArray(); return locations; }

I don't know how it happens? Any one if know how to solve it,thank you,please help me!

takuya-takeuchi commented 5 years ago

FRDN does not support multi-thread process. This limitation is also on original face_recognition.

Check the https://github.com/takuya-takeuchi/FaceRecognitionDotNet/issues/24#issuecomment-458744002

pick30 commented 5 years ago

Yes,I use multi-thread. I try to use your DlibDotNet not the FaceRecognitionDotNet, write my code to get FaceEncodings and FaceLocations,then it's perfect,Never get the Exception again for now. The key code is here,it's where the Exception happens,use the lock to avoid the error: private Rectangle[] locations(Matrix img) { lock (locker) { Rectangle[] recList = detector.Operator(img); return recList; } } Maybe I user the FaceRecognitionDotNet by the wrong way! Thank you for reply, and thank you for share the lib!