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

Add face recognition example #22

Closed jrt324 closed 4 years ago

jrt324 commented 5 years ago

Hi , This is a magical lib ! Could you please provide this face recognition example ?

takuya-takeuchi commented 5 years ago

Sorry. I lost the above sample source code. But I was disassemble the above problem. (But not equal completely). You must add OpenCVSharp, collect image and override image path string.

private static void Main(string[] args)
    {
      FaceRecognition fr = FaceRecognition.Create("D:\\Works\\Local\\face_recognition_models\\face_recognition_models\\models");
      using (Image image1 = FaceRecognition.LoadImageFile("images\\AP_090107018039.jpg"))
      {
        KeyValuePair<string, FaceEncoding>[] array1 = new \u003C\u003Ef__AnonymousType0<string, string>[6]
        {
          new
          {
            Path = "images\\BarackObama.jpg",
            Name = "Barack Obama"
          },
          new
          {
            Path = "images\\BillClinton.jpg",
            Name = "Bill Clinton"
          },
          new
          {
            Path = "images\\GeorgeWBush.jpeg",
            Name = "George W Bush"
          },
          new
          {
            Path = "images\\GeorgeH.W.Bush.jpg",
            Name = "George H W Bush"
          },
          new
          {
            Path = "images\\JimmyCarter.jpg",
            Name = "Jimmy Carter"
          },
          new{ Path = "images\\ShinzoAbe.jpg", Name = "Shinzo Abe" }
        }.Select(arg =>
        {
          using (Image image = FaceRecognition.LoadImageFile(arg.Path))
            return new KeyValuePair<string, FaceEncoding>(arg.Name, fr.FaceEncodings(image, (IEnumerable<Location>) null, 0).FirstOrDefault<FaceEncoding>());
        }).ToArray<KeyValuePair<string, FaceEncoding>>();
        Dictionary<string, Dictionary<Location, double>> dictionary1 = new Dictionary<string, Dictionary<Location, double>>();
        foreach (Location faceLocation in fr.FaceLocations(image1, 1, Models.Hog))
        {
          FaceEncoding[] array2 = fr.FaceEncodings(image1, (IEnumerable<Location>) new Location[1]
          {
            faceLocation
          }, 0).ToArray<FaceEncoding>();
          foreach (KeyValuePair<string, FaceEncoding> keyValuePair in ((IEnumerable<KeyValuePair<string, FaceEncoding>>) array1).Where<KeyValuePair<string, FaceEncoding>>((Func<KeyValuePair<string, FaceEncoding>, bool>) (pair => pair.Value != null)))
          {
            double num = FaceRecognition.FaceDistance(((IEnumerable<FaceEncoding>) array2).First<FaceEncoding>(), keyValuePair.Value);
            if (num < 0.6)
            {
              Dictionary<Location, double> dictionary2;
              if (!dictionary1.TryGetValue(keyValuePair.Key, out dictionary2))
              {
                dictionary2 = new Dictionary<Location, double>();
                dictionary1.Add(keyValuePair.Key, dictionary2);
              }
              dictionary2.Add(faceLocation, num);
            }
          }
          foreach (FaceEncoding faceEncoding in array2)
            faceEncoding.Dispose();
        }
        using (Mat mat1 = Cv2.ImRead("images\\AP_090107018039.jpg", ImreadModes.Color))
        {
          using (Mat mat2 = mat1.Resize(Size.Zero, 1.0, 1.0, InterpolationFlags.Linear))
          {
            foreach (KeyValuePair<string, Dictionary<Location, double>> keyValuePair1 in dictionary1)
            {
              string key = keyValuePair1.Key;
              Dictionary<Location, double> dictionary2 = keyValuePair1.Value;
              Location location = (Location) null;
              double maxValue = double.MaxValue;
              foreach (KeyValuePair<Location, double> keyValuePair2 in dictionary2)
              {
                if (keyValuePair2.Value < maxValue)
                {
                  maxValue = keyValuePair2.Value;
                  location = keyValuePair2.Key;
                }
              }
              Point point = new Point((double) location.Left * 1.0, (double) location.Top * 1.0);
              Point pt2 = new Point((double) location.Right * 1.0, (double) location.Bottom * 1.0);
              mat2.Rectangle(point, pt2, Scalar.Red, 2, LineTypes.AntiAlias, 0);
              point.Y = pt2.Y - 20;
              mat2.Rectangle(point, pt2, Scalar.Red, -1, LineTypes.AntiAlias, 1);
              point.X += 3;
              point.Y = pt2.Y - 5;
              mat2.PutText(key, point, HersheyFonts.HersheySimplex, 0.4, Scalar.White, 1, LineTypes.Link8, false);
            }
            Cv2.ImShow("Result", mat2);
            Cv2.WaitKey(0);
          }
        }
      }
    }
  }
jrt324 commented 5 years ago

@takuya-takeuchi It works now , thanks!

theladyjaye commented 5 years ago

In your example, where did you acquire the model files from?

D:\\Works\\Local\\face_recognition_models\\face_recognition_models\\models

https://github.com/ageitgey/face_recognition_models? https://pypi.org/project/face_recognition_models/

takuya-takeuchi commented 5 years ago

@aventurella Exactly. We use https://github.com/ageitgey/face_recognition_models

feelsyt commented 5 years ago

This sentence can not be passed KeyValuePair<string, FaceEncoding>[] array1 = new \u003C\u003Ef__AnonymousType0<string, string>[6]

takuya-takeuchi commented 5 years ago

@feelsyt The above source was produced by disassemble. So source code has some corruption.

KeyValuePair<string, FaceEncoding>[] array1 = new \u003C\u003Ef__AnonymousType0<string, string>[6]

to

KeyValuePair<string, FaceEncoding>[] array1 = new []
feelsyt commented 5 years ago

@feelsyt以上来源是由反汇编产生的。所以源代码有一些腐败。

KeyValuePair<string, FaceEncoding>[] array1 = new \u003C\u003Ef__AnonymousType0<string, string>[6]

KeyValuePair<string, FaceEncoding>[] array1 = new []

OK ,thank you

takuya-takeuchi commented 4 years ago

It seem like to be able to close.

Webreaper commented 1 year ago

Feels like you should include this in the main repo - it's quite hard to find this issue, and for a project called FaceRecognitionDotnet to not have an actual example of facial recognition in the master branch seems somewhat counter-intuitive. :)