/// <summary> /// Function to update the list of Face IDs, based on the example images added /// </summary> private async void UpdateFaceGuidsAsync() { if (!_faceListExists) { return; } try { FaceList faceList = await _faceServiceClient.GetFaceListAsync(FaceListName.ToLower()); if (faceList == null) { return; } PersistedFace[] faces = faceList.PersistedFaces; foreach (PersistedFace face in faces) { FaceIds.Add(face.PersistedFaceId); } } catch (FaceAPIException ex) { Debug.WriteLine($"Failed to get face list - {ex.ErrorMessage}"); } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
/// <summary> /// Function to add example faces to a given face list. Will loop through a folder and add all image files in that folder /// </summary> /// <param name="obj"></param> private async void AddExampleFacesToList(object obj) { string personGroupDirectory = Path.Combine(Environment.CurrentDirectory, "PersonGroup"); string[] images = GetImageFiles(personGroupDirectory); try { foreach (string image in images) { using (Stream fileStream = File.OpenRead(image)) { Face[] faces = await _faceServiceClient.DetectAsync(fileStream); FaceRectangle faceRectangle = faces[0].FaceRectangle; AddPersistedFaceResult addFacesResult = await _faceServiceClient.AddFaceToFaceListAsync( FaceListName.ToLower(), fileStream, null, faceRectangle); UpdateFaceGuidsAsync(); } } } catch (FaceAPIException ex) { Debug.WriteLine($"Failed to add faces to face list: {ex.ErrorMessage}"); } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
/// <summary> /// Function to create new face list /// </summary> /// <param name="obj"></param> private async void CreateFaceListAsync(object obj) { try { if (!_faceListExists) { await _faceServiceClient.CreateFaceListAsync(FaceListName.ToLower(), FaceListName, string.Empty); await DoesFaceListExistAsync(); } } catch (FaceAPIException ex) { Debug.WriteLine($"Failed to create face list - {ex.ErrorMessage}"); } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
/// <summary> /// Function to find similar faces. Will first detect any faces in the given photo, and use this result for the similar faces search /// </summary> /// <param name="obj"></param> private async void FindSimilarFace(object obj) { if (!_faceListExists || FaceIds.Count == 0) { SimilarResult = "Face list does not exist"; } Guid findFaceGuid = await BrowseAndDetectFaces(); if (findFaceGuid.Equals(Guid.Empty)) { return; } try { SimilarPersistedFace[] similarFaces = await _faceServiceClient.FindSimilarAsync(findFaceGuid, FaceListName.ToLower(), 3); if (similarFaces == null || similarFaces.Length == 0) { SimilarResult = "No faces were similar"; } StringBuilder result = new StringBuilder(); result.Append("Similar faces:\n"); if (similarFaces.Length == 0) { result.Append("No similar faces found"); } foreach (SimilarPersistedFace similarFace in similarFaces) { result.AppendFormat("Face ID: {0}\n", similarFace.PersistedFaceId); result.AppendFormat("Probability: {0}\n\n", similarFace.Confidence); } SimilarResult = result.ToString(); } catch (Exception ex) { Debug.WriteLine(ex.Message); } }