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

Resize CroppedFaceImage #139

Closed atabora1 closed 3 years ago

atabora1 commented 4 years ago

Hi @takuya-takeuchi : Hope all is well. I continue to test different methods of making the recognition faster for a large number of images. I find that cropping the faces and subsequently comparing the cropped image is more efficient than loading the full size image.

  1. Is it possible to add a resize capability to the "croppedFaces[1].Save()" function?
  2. How do you recommend converting FaceRecognitionDotNet.Image to a JPG?

Psedo code. var croppedFaces = FaceRecognition.CropFaces(image1, faceLocations).ToArray(); Bitmap resizedFace= ResizeImage(croppedFaces[x],width,height) //function I need guidance on
resizedFace.Save(strNewfilenameHD, ImageFormat.Jpeg);

As always, appreciate all the work

takuya-takeuchi commented 4 years ago

@atabora1

Is it possible to add a resize capability to the "croppedFaces[1].Save()" function?

I think extraction face encoding of FRDB (dlib) is not slow. Because cropped image is enough small. If you can not be satisfied with performance of turn of around. You should resize input image.

However, I can understand what you hope. Large size image is high quality and is low possibility to miss finding face.

So I can add new function

// return new resized image
public static Image Image.Resize(Image image, double scale);
public static Image Image.Resize(Image image, uint width, uint height);

But I cannot add function to resize to Save method. Because Save method depends on dlib save_jpg method and this does not have resize option.

How do you recommend converting FaceRecognitionDotNet.Image to a JPG?

What does it mean? FaceRecognitionDotNet.Image.Save can create jpeg file. You want to save jpeg data to memory, right? If so, it is not possible due to dlib.save_jpg method.

atabora1 commented 4 years ago

Thanks for the quick response. the new function will help. Here is more detail of my goal.

//Step #1--------------- Check and resize original image originalImg = new Bitmap(path); if( originalImg.Width>800) { resizedImg=resizeImg(originalImg,scaleFactor); resizedImg.Save(resizedImgURL, ImageFormat.Jpeg);

}

  //Step #2--------------- Detect face locations and save to harddrive, could also save to database
      using (var image = FaceRecognition.LoadImage(resizedImgURL))
  {
          var faceLocations = _FaceRecognition1.FaceLocations(image).ToArray();
          var croppedFaces = FaceRecognition.CropFaces(image, faceLocations).ToArray();

            for (int x = 0; x < croppedFaces.Length; x++)
            {
                croppedFaces[x].Save(strNewfilePath, ImageFormat.Jpeg);
            }

  }

  //Step #3--------------- Check each face against all other faces
lstStrFullPathNested = getListCroppedHDFaces("DESC");//get cropped face files saved on harddrive DESC order
lstStrFullPathOutter= getListCroppedHDFaces("ASC");//get cropped face files saved on harddrive ASC order

        foreach (string strOutterPath in lstStrFullPathOutter)
        {

            for (int x = 0; x < lstStrFullPathNested.Count; x++)
            {

                CompareFacesStoreinDatabase(lstStrFullPathNested[x], strOutterPath, _FaceRecognition);//use og URL

            }

        }

    public Boolean CompareFacesStoreinDatabase(string strFullpath1Nested, string strFullpath2, FaceRecognition _FaceRecognition)//compare all the faces with other faces
    {

        using (var image1 = FaceRecognition.LoadImageFile(strNewNestedPath1))
        using (var image2 = FaceRecognition.LoadImageFile(strNewPath2))
        {
            var encodings1 = _FaceRecognition.FaceEncodings(image1).ToArray();
            var encodings2 = _FaceRecognition.FaceEncodings(image2).ToArray();
            if (encodings1.Length >= 1 && encodings2.Length >= 1)
            {

                bolFoundFace = true;
                for (int y = 0; y < encodings2.Length; y++)

                {

                    for (int x = 0; x < encodings1.Length; x++)
                    {
                        var dblDistance = FaceRecognition.FaceDistance(encodings1[x], encodings2[y]);

                        saveDatabaseFaceDistance(strFullpath1Nested, strFullpath2, dblDistance);//saves the distance between the two faces in the database

                    }

                }

            }
        }
    }           
takuya-takeuchi commented 3 years ago

Close because 1.3.0.4 support ToBitmap and it can resize