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

Get face encodings #6

Closed gerrymcjr closed 6 years ago

gerrymcjr commented 6 years ago

Hello, i am trying to get the face encoding with this codes var bidenFile = "480px-Biden_2013.jpg"; var path1 = Path.Combine(Directory.GetCurrentDirectory(), bidenFile); var image = FaceRecognition.LoadImageFile(path1); FaceEncoding[] encodings = _FaceRecognition.FaceEncodings(image).ToArray(); what i am trying to do here is to get the actual array of the face encoding but on the foreach loop foreach (var enc in encodings) { Console.WriteLine(enc); } it displays this FaceRecognitionDotNet.FaceEncoding i am looking to get the actual face encoding such as { -0.33578} and so on and i will actually save it on my sql lite as a form of enrollment for facial recognition and pull the encodings when its time to perform facial recognition

can you guide me on how to get the necessary value thank you

takuya-takeuchi commented 6 years ago

i will actually save it on my sql lite as a form of enrollment for facial recognition and pull the encodings when its time to perform facial recognition

It means that you want to serialize FaceEncoding data, right? If so, I will be able to provide the following aporaches

gerrymcjr commented 6 years ago

i cloned your project. and added SerializableAttribute to FaceEncoding class then after i added a console app to test the serialization but ran to an error capture

please help thank you

takuya-takeuchi commented 6 years ago

You must deploy DlibDotNet.Native.dll to execution directory. This library is downloaded in packages directiry if you use nuget.

Last night, I added serialize function to FaceEncoding class. So it will be published to soon.

takuya-takeuchi commented 6 years ago

Ah, did you build DlibDotNet at FaceRecignitionl.sln? If so, you must build DlibDotNet.Native and DlibDotNet.Dnn manually by cmake.

gerrymcjr commented 6 years ago

Cool i guess i just have to wait for the update

takuya-takeuchi commented 6 years ago

I released latest version which support serialize/deserialize for FaceEncoding class.

https://www.nuget.org/packages/FaceRecognitionDotNet/1.2.3.1 https://www.nuget.org/packages/FaceRecognitionDotNet-WithCUDA/1.2.3.1

Sampe code is below

[TestMethod]
public void SerializeDeserializeBinaryFormatter()
{
    const string testName = nameof(this.SerializeDeserializeBinaryFormatter);
    var directory = Path.Combine(ResultDirectory, testName);
    Directory.CreateDirectory(directory);

    var path = Path.Combine(ImageDirectory, TwoPersonFile);
    if (!File.Exists(path))
    {
        var binary = new HttpClient().GetByteArrayAsync($"{TwoPersonUrl}/{TwoPersonFile}").Result;

        Directory.CreateDirectory(ImageDirectory);
        File.WriteAllBytes(path, binary);
    }

    using (var image = FaceRecognition.LoadImageFile(path))
    {
        var encodings = this._FaceRecognition.FaceEncodings(image).ToArray();
        Assert.IsTrue(encodings.Length > 1, "");

        var dest = $"{path}.dat";
        if (File.Exists(dest))
            File.Delete(dest);

        var bf = new BinaryFormatter();
        using (var fs = new FileStream(dest, FileMode.OpenOrCreate))
            bf.Serialize(fs, encodings.First());

        using (var fs = new FileStream(dest, FileMode.OpenOrCreate))
        {
            var encoding = (FaceEncoding)bf.Deserialize(fs);
            var distance = FaceRecognition.FaceDistance(encodings.First(), encoding);
            Assert.IsTrue(Math.Abs(distance) < double.Epsilon);
        }

        foreach (var encoding in encodings)
            encoding.Dispose();

        foreach (var encoding in encodings)
            Assert.IsTrue(encoding.IsDisposed, $"{typeof(FaceEncoding)} should be already disposed.");
    }
}
takuya-takeuchi commented 6 years ago

Sorry, please use 1.2.3.2.

https://www.nuget.org/packages/FaceRecognitionDotNet/1.2.3.2 https://www.nuget.org/packages/FaceRecognitionDotNet-WithCUDA/1.2.3.2

gerrymcjr commented 6 years ago

first of all thank you for accommodating my request i tested the serialization and it worked but when getting the distance it is way off i used the same photo for serialization and compare face "480px-Biden_2013.jpg" on the compareface it returned false and the distance is way off 1.40253678451802 while i was using the same photo capture

takuya-takeuchi commented 6 years ago

thank you for your reporting. oh, my unit test code may have something wrong.

now, i work in office. so plz wait until night.

gerrymcjr commented 6 years ago

thank you for your reporting. oh, my unit test code may have something wrong.

now, i work in office. so plz wait until night.

No problem mate i was waiting for this port i was using the python version months ago glad that you ported it to c#

takuya-takeuchi commented 6 years ago

I uploaded wrong DlibDotNet package. 19.15.0.20180911 contains old DlibDotNet.dll. Please update DlibDotNet to 19.15.0.20180913. But if you use old one's, app should crash. Do you face crash issue?

And I tested your reported source code and I could not reproduce.

Could you test the following binary and code?

takuya-takeuchi commented 6 years ago

image

gerrymcjr commented 6 years ago

Nope no crashes at all. I will try and test it tomorrow tnx

gerrymcjr commented 6 years ago

How come you could use it on a full dot net framework?

takuya-takeuchi commented 6 years ago

What does it mean? This sample uses .NET 4.7.1.

gerrymcjr commented 6 years ago

Well i was trying to install the nuget on a non dotnet core application but it says it won't work on .net framework 4.7.1

yuzifu commented 6 years ago

FaceRecognitionDotNet can running on .net 4.7

takuya-takeuchi commented 6 years ago

FaceRecognitionDotNet and DlibDotNet are built as .NET Standard 2.0 project. It is supported by .NET Framework 4.6.1, .NET Core 2.0 and more. It means that legacy framework can not use it.

Please refer the following document. https://github.com/dotnet/standard/blob/master/docs/versions.md

gerrymcjr commented 6 years ago

capture1 here is the result it's still way off on .net core but using your app it says true and distance is 0 should i just use .net framework rather than dot net core? here is your's capture

Edit: i re ran the encoding again and this time it's showing the right result capture