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

Efficient way to LoadImage from OpenCVSharp #44

Closed utarn closed 5 years ago

utarn commented 5 years ago

I got a Mat object from OpenCVSharp. How do I efficiently load the image to FaceRecognition.LoadImage or should I use DlibDotNet to load an image?

Thanks

takuya-takeuchi commented 5 years ago

You can refer https://github.com/takuya-takeuchi/FaceRecognitionDotNet/issues/40 He noted sample source code to load from OpenCVSharp.

utarn commented 5 years ago

Sorry takuya-takeuchi, I'm still not getting the reference. I got a cropped grayscale Mat from opencvsharp as I use haarcascade to do a rough detect. I also tried

   var array = new byte[source.Height * source.Width];
            source.GetArray(source.Width, source.Height, array);
            var image = FaceRecognition.LoadImage(array, source.Height, source.Width, 1);
takuya-takeuchi commented 5 years ago

Last argument of LoadImage is not 1. It is stride. stride is width channels. So it is width 1 if grayscale.

takuya-takeuchi commented 5 years ago

Oh, sorry. You are correct. Last argument is element size.

utarn commented 5 years ago

But still I got ArrayOutOfRangException. So I tried

 var array = new byte[source.Height * source.Width];            
            source.GetArray(source.Rows-1, source.Cols-1, array);
            var image = FaceRecognition.LoadImage(array, source.Height, source.Width, 1);
            var encodings = faceRecognition.FaceEncodings(image).ToArray();

But the encodings.Length is 0.

takuya-takeuchi commented 5 years ago

Could you check the following sample? I wrote sample project using OpenCVSharp. It may be helpful. https://github.com/takuya-takeuchi/FaceRecognitionDotNet/tree/develop/examples/OpenCVSharpSample

utarn commented 5 years ago

Thank you so much. It worked!