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

Better way to compare two faces? #118

Closed pvictorlv closed 4 years ago

pvictorlv commented 4 years ago

Hey, i need to compare two faces (without storing them, both in base64) and display the similarity level of them. What's the better way to do it? And how i can increase the precision level? Here is my code now:

    public async Task<double> CompareFaces(string sourceImageBase, string targetImageBase)
        {
            string directory = "C:\\models";

            using (FaceRecognition fr = FaceRecognition.Create(directory))
            {
                var bitmapData = GetImageBytes(FixBase64ForImage(sourceImageBase));
                var targetBitmapData = GetImageBytes(FixBase64ForImage(targetImageBase));

                await using (MemoryStream streamTargetBitmap = new MemoryStream(targetBitmapData))
                {
                    using (Bitmap bitTargetImage = new Bitmap(streamTargetBitmap))
                    {
                        await using (MemoryStream streamSourceBitmap = new MemoryStream(bitmapData))
                        {
                            using (Bitmap bitSourceImage = new Bitmap(streamSourceBitmap))
                            {
                                using (var imageA = FaceRecognition.LoadImage(bitSourceImage))
                                {
                                    using (var imageB = FaceRecognition.LoadImage(bitTargetImage))
                                    {
                                        IEnumerable<Location> locationsA = fr.FaceLocations(imageA);
                                        IEnumerable<Location> locationsB = fr.FaceLocations(imageB);

                                        var encodingA = fr.FaceEncodings(imageA, locationsA);
                                        IEnumerable<FaceEncoding> encodingB = fr.FaceEncodings(imageB, locationsB);

                                        return FaceRecognition.FaceDistance(encodingA.First(), encodingB.First());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

Thanks!

takuya-takeuchi commented 4 years ago

To increase accuracy

What's the better way to do it?

And you need not to re-create FaceRecognition object. You can share it on 1-thread. If you want to use multithread, you must create N FaceRecognition object for N threads.

pvictorlv commented 4 years ago

I've tried with 100x100 grayscale face cut out image and with normal images in 520x360 with same results, and it's bad. it can't identify the same person... btw, how the distance is calculated? it's from 0 to 100?

pvictorlv commented 4 years ago

@takuya-takeuchi How can i contact you?

takuya-takeuchi commented 4 years ago

100x100 grayscale

You mean that you compare with encoding from grayscale and encoding from rgb image, right? It could be no matter but I'm not sure.

btw, how the distance is calculated? it's from 0 to 100?

dittance is from 0 to 1.0.

How can i contact you?

No. I am very very busy and I can not contribute to my projects.

pvictorlv commented 4 years ago

Btw, there's any way to improve the recognition speed?

takuya-takeuchi commented 4 years ago

use gpu or intel MKL