public async Task <bool> RemoveImageFromWhitelistAsync(StorageFile imageFile, string personName = null) { bool isSuccess = true; if (!FaceApiUtils.ValidateImageFile(imageFile)) { isSuccess = false; } else { // If personName is null use the folder name as person name if (string.IsNullOrEmpty(personName)) { personName = await FaceApiUtils.GetParentFolderNameAsync(imageFile); } var personId = _whitelist.GetPersonIdByName(personName); var faceId = _whitelist.GetFaceIdByFilePath(imageFile.Path); if (personId == Guid.Empty || faceId == Guid.Empty) { isSuccess = false; } else { await RemoveFace(personId, faceId); // train whitelist isSuccess = await TrainingWhitelistAsync(); } } return(isSuccess); }
public async Task <List <string> > FaceRecognizeAsync(StorageFile imageFile) { var recogResult = new List <string>(); if (!FaceApiUtils.ValidateImageFile(imageFile)) { throw new FaceRecognitionException(FaceRecognitionExceptionType.InvalidImage); } // detect all faces in the image var faceIds = await DetectFacesFromImage(imageFile); // try to identify all faces to person var identificationResults = await _faceApiClient.IdentifyAsync(WhitelistId, faceIds); // add identified person name to result list foreach (var result in identificationResults) { if (result.Candidates.Length > 0) { var personName = _whitelist.GetPersonNameById(result.Candidates[0].PersonId); Debug.WriteLine("Face ID Confidence: " + Math.Round(result.Candidates[0].Confidence * 100, 1) + "%"); recogResult.Add(personName); } } return(recogResult); }
public async Task <List <string> > FaceRecognizeAsync(StorageFile imageFile) { var recogResult = new List <string>(); if (!FaceApiUtils.ValidateImageFile(imageFile)) { throw new FaceRecognitionException(FaceRecognitionExceptionType.InvalidImage); } // detect all faces in the image var faceIds = await DetectFacesFromImage(imageFile); // try to identify all faces to person var identificationResults = await _faceApiClient.IdentifyAsync(WhitelistId, faceIds); // add identified person name to result list foreach (var result in identificationResults) { if (result.Candidates.Length > 0) { var personName = _whitelist.GetPersonNameById(result.Candidates[0].PersonId); //Debug.WriteLine("Face ID Confidence: " + Math.Round(result.Candidates[0].Confidence * 100, 1) + "%"); recogResult.Add(personName); confianza = (int)Math.Round(result.Candidates[0].Confidence * 100, 1); /*TestPostMessage(personName, confianza); * await Task.Run(async () => { await AzureIoTHub.SendDeviceToCloudMessageAsync(personName, confianza.ToString(), humor); });*/ } } return(recogResult); }
public async Task <bool> AddImageToWhitelistAsync(StorageFile imageFile, string personName = null) { bool isSuccess = true; // imageFile should be valid image file if (!FaceApiUtils.ValidateImageFile(imageFile)) { isSuccess = false; } else { var filePath = imageFile.Path; // If personName is null/empty, use the folder name as person name if (string.IsNullOrEmpty(personName)) { personName = await FaceApiUtils.GetParentFolderNameAsync(imageFile); } // If person name doesn't exists, add it var personId = _whitelist.GetPersonIdByName(personName); if (personId == Guid.Empty) { var folder = await imageFile.GetParentAsync(); personId = await CreatePerson(personName, folder); } // detect faces // var faceId = await DetectFaceFromImage(imageFile); // await AddFace(personId, imageFile.Path); Task task = StartAddFaceImageTask(imageFile, personId); task.ContinueWith((taskDone) => { var detectTask = taskDone as Task <Tuple <string, ClientContract.AddPersistedFaceResult> >; // Update something on UI var faceDetectionResult = detectTask.Result; if (faceDetectionResult != null && faceDetectionResult.Item2 != null) { _whitelist.AddFace(personId, faceDetectionResult.Item2.PersistedFaceId, faceDetectionResult.Item1); } else { Debug.WriteLine("Error detecting face in image " + imageFile.Path); } }).Wait(); // train whitelist isSuccess = await TrainingWhitelistAsync(); } return(isSuccess); }
/// <summary> /// Detect face and return the face id of a image file /// </summary> /// <param name="imageFile"> /// image file to detect face /// </param> /// <returns>face id</returns> private async Task <Guid[]> DetectFacesFromImage(StorageFile imageFile) { var stream = await imageFile.OpenStreamForReadAsync(); var faces = await _faceApiClient.DetectAsync(stream); if (faces == null || faces.Length < 1) { throw new FaceRecognitionException(FaceRecognitionExceptionType.NoFaceDetected); } return(FaceApiUtils.FacesToFaceIds(faces)); }
public async Task <Dictionary <HSFace, HSPerson> > FaceRecognizeAsync(StorageFile imageFile) { var recogResult = new Dictionary <HSFace, HSPerson>(); if (!FaceApiUtils.ValidateImageFile(imageFile)) { throw new FaceRecognitionException(FaceRecognitionExceptionType.InvalidImage); } // detect all faces in the image var arFaces = await DetectFacesFromImage(imageFile); // try to identify all faces to person var identificationResults = await FaceServiceHelper.IdentifyAsync(WhitelistId, FaceApiUtils.FacesToFaceIds(arFaces)); // add identified person name to result list foreach (var result in identificationResults) { if (result.Candidates.Length > 0) { var person = _whitelist.GetPersonById(result.Candidates[0].PersonId); Debug.WriteLine(String.Format("Detected person {0} face ID Confidence: {1}% image file: {2}", person.Name, Math.Round(result.Candidates[0].Confidence * 100, 1), imageFile.Path)); recogResult.Add(new HSFace(result.FaceId, imageFile.Path), person); } else { foreach (ClientContract.Face detectedFace in arFaces) { if (detectedFace.FaceId == result.FaceId) { recogResult.Add(new HSFace(result.FaceId, imageFile.Path, detectedFace.FaceAttributes), null); Debug.WriteLine(String.Format("Unknown {0} age {1} face ID {2} image file: {3}", detectedFace.FaceAttributes.Gender, detectedFace.FaceAttributes.Age, detectedFace.FaceId, imageFile.Path)); } } } } return(recogResult); }
public async Task <bool> AddImageToWhitelistAsync(StorageFile imageFile, string personName = null) { bool isSuccess = true; // imageFile should be valid image file if (!FaceApiUtils.ValidateImageFile(imageFile)) { isSuccess = false; } else { var filePath = imageFile.Path; // If personName is null/empty, use the folder name as person name if (string.IsNullOrEmpty(personName)) { personName = await FaceApiUtils.GetParentFolderNameAsync(imageFile); } // If person name doesn't exists, add it var personId = _whitelist.GetPersonIdByName(personName); if (personId == Guid.Empty) { var folder = await imageFile.GetParentAsync(); personId = await CreatePerson(personName, folder); } // detect faces var faceId = await DetectFaceFromImage(imageFile); await AddFace(personId, faceId, imageFile.Path); // train whitelist isSuccess = await TrainingWhitelistAsync(); } return(isSuccess); }
/// <summary> /// Use whitelist folder to build whitelist Database /// </summary> /// <returns></returns> private async Task BuildWhiteListAsync(IProgress <int> progress, double progressCnt) { Debug.WriteLine("Start building whitelist from " + _whitelistFolder.Path); // calc progress step var fileCnt = await FaceApiUtils.GetFileCountInWhitelist(_whitelistFolder); var progressStep = (100.0 - progressCnt) / fileCnt; var subFolders = await _whitelistFolder.GetFoldersAsync(); // Iterate all subfolders in whitelist foreach (var folder in subFolders) { var personName = folder.Name; // create new person var personId = await CreatePerson(personName, folder); // get all images in the folder var files = await folder.GetFilesAsync(); // iterate all images and add to whitelist foreach (var file in files) { Debug.WriteLine("BuildWhiteList: Processing " + file.Path); try { var faceId = await DetectFaceFromImage(file); await AddFace(personId, faceId, file.Path); Debug.WriteLine("This image added to whitelist successfully!"); } catch (FaceRecognitionException fe) { switch (fe.ExceptionType) { case FaceRecognitionExceptionType.InvalidImage: Debug.WriteLine("WARNING: This file is not a valid image!"); break; case FaceRecognitionExceptionType.NoFaceDetected: Debug.WriteLine("WARNING: No face detected in this image"); break; case FaceRecognitionExceptionType.MultipleFacesDetected: Debug.WriteLine("WARNING: Multiple faces detected, ignored this image"); break; } } // update progress progressCnt += progressStep; UpdateProgress(progress, progressCnt); } } await TrainingWhitelistAsync(); Debug.WriteLine("Whitelist created successfully!"); }
/// <summary> /// Use whitelist folder to build whitelist Database /// </summary> /// <returns></returns> private async Task BuildWhiteListAsync(IProgress <int> progress, double progressCnt) { Debug.WriteLine("Start building whitelist from " + _whitelistFolder.Path); // calc progress step var fileCnt = await FaceApiUtils.GetFileCountInWhitelist(_whitelistFolder); var progressStep = (100.0 - progressCnt) / fileCnt; var subFolders = await _whitelistFolder.GetFoldersAsync(); // Iterate all subfolders in whitelist List <Task> tasks = new List <Task>(); foreach (var folder in subFolders) { var personName = folder.Name; // create new person var personId = await CreatePerson(personName, folder); // get all images in the folder var files = await folder.GetFilesAsync(); // iterate all images and add to whitelist foreach (var img in await folder.GetFilesAsync()) { tasks.Add(StartAddFaceImageTask(img, personId).Unwrap().ContinueWith((taskDone) => { var detectTask = taskDone as Task <Tuple <string, ClientContract.AddPersistedFaceResult> >; if (detectTask == null) { return; } // Update something on UI var faceDetectionResult = detectTask.Result; if (faceDetectionResult != null && faceDetectionResult.Item2 != null && faceDetectionResult.Item1 != null) { _whitelist.AddFace(personId, faceDetectionResult.Item2.PersistedFaceId, faceDetectionResult.Item1); } else { return; } // update progress progressCnt += progressStep; UpdateProgress(progress, progressCnt); } )); } await Task.WhenAll(tasks); } await Task.WhenAll(tasks); await TrainingWhitelistAsync(); Debug.WriteLine("Whitelist created successfully!"); }