private async Task LoadPersonFacesFromService() { this.progressControl.IsActive = true; this.SelectedPersonFaces.Clear(); try { Person latestVersionOfCurrentPerson = await FaceServiceHelper.GetPersonAsync(this.CurrentPersonGroup.PersonGroupId, this.SelectedPerson.PersonId); this.SelectedPerson.PersistedFaceIds = latestVersionOfCurrentPerson.PersistedFaceIds; if (this.SelectedPerson.PersistedFaceIds != null) { foreach (Guid face in this.SelectedPerson.PersistedFaceIds) { PersistedFace personFace = await FaceServiceHelper.GetPersonFaceAsync(this.CurrentPersonGroup.PersonGroupId, this.SelectedPerson.PersonId, face); this.SelectedPersonFaces.Add(personFace); } } } catch (Exception e) { await Util.GenericApiCallExceptionHandler(e, "Failure downloading person faces"); } this.progressControl.IsActive = false; }
public async Task <IList <Guid> > UploadPhotosToFaceList(string directory, int count, int skip, string faceListId, string userData) { Guard.Argument(count, nameof(count)).NotNegative(); Guard.Argument(skip, nameof(skip)).NotNegative(); Guard.Argument(directory, nameof(directory)).NotNull().NotWhiteSpace(); Guard.Argument(directory, nameof(faceListId)) .NotNull() .NotWhiteSpace() .Require(dir => this.fileSystem.Directory.Exists(dir), dir => $"Directory does not exist '{dir}'"); IEnumerable <string> files = this.fileSystem.Directory.EnumerateFiles(directory).Skip(skip).Take(count); List <Guid> faceIds = new List <Guid>(count); foreach (string imageFile in files) { using (Stream stream = this.fileSystem.File.OpenRead(imageFile)) { PersistedFace persistedFace = await this.faceClient.FaceList.AddFaceFromStreamAsync( faceListId, stream, userData).ConfigureAwait(false); this.logger.LogDebug($"Added '{imageFile}' to FaceList '{faceListId}' with PersistedFaceId {persistedFace.PersistedFaceId}"); faceIds.Add(persistedFace.PersistedFaceId); } } return(faceIds); }
private async void AcceptBtn_Click(object sender, EventArgs e) { var faceClient = FaceToWork.Form1.faceClient; try { Person person = await faceClient.PersonGroupPerson.CreateAsync(_groupId, Name_txtBox.Text.ToString()); DataSet1TableAdapters.PersonTBLTableAdapter personTBL = new DataSet1TableAdapters.PersonTBLTableAdapter(); personTBL.AddPerson(person.PersonId.ToString(), Name_txtBox.Text, _groupId); foreach (string u in ListFolderPath.Items) { Stream s = File.OpenRead(u); Console.WriteLine($"Add face to the person ({Name_txtBox.Text})."); PersistedFace face = await faceClient.PersonGroupPerson.AddFaceFromStreamAsync(_groupId, person.PersonId, s); } Close(); MessageBox.Show("Person successfully added"); } catch (APIErrorException ex) { MessageBox.Show(ex.Message); } }
private async void OnImageDataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args) { PersistedFace dataContext = sender.DataContext as PersistedFace; if (dataContext != null) { Image image = sender as Image; if (image != null) { BitmapImage bitmapImage = new BitmapImage(); image.Source = bitmapImage; try { if (Path.IsPathRooted(dataContext.UserData)) { // local file bitmapImage.SetSource(await(await StorageFile.GetFileFromPathAsync(dataContext.UserData)).OpenReadAsync()); } else { // url bitmapImage.UriSource = new Uri(dataContext.UserData); } } catch (Exception) { } } } }
public static async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context) { #region Config var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); var faceEndpoint = config["faceEndpoint"]; var subscriptionKey = config["faceSubscriptionKey"]; #endregion #region Read Body string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); string imageUrl = data?.imageUrl; string personName = data?.personName; string personId = data?.personId; #endregion #region Cognitive Services Calls FaceClient faceClient = new FaceClient(new ApiKeyServiceClientCredentials(subscriptionKey), new System.Net.Http.DelegatingHandler[] { }) { Endpoint = faceEndpoint }; //Sample Person Group is created at first run for demo purposes. //await faceClient.PersonGroup.CreateAsync(PersonGroupId, PersonGroupId); PersonGroup humanGroup = await faceClient.PersonGroup.GetAsync(PersonGroupId); Person human = null; if (string.IsNullOrEmpty(personId)) { human = await faceClient.PersonGroupPerson.CreateAsync(humanGroup.PersonGroupId, personName); } else { human = await faceClient.PersonGroupPerson.GetAsync(humanGroup.PersonGroupId, new System.Guid(personId)); } PersistedFace face = await faceClient.PersonGroupPerson.AddFaceFromUrlAsync(humanGroup.PersonGroupId, human.PersonId, imageUrl); await faceClient.PersonGroup.TrainAsync(PersonGroupId); #endregion #region Return JSON var myObj = new { faceId = face.PersistedFaceId, personId = human.PersonId }; var jsonToReturn = JsonConvert.SerializeObject(myObj); return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(jsonToReturn, Encoding.UTF8, "application/json") }); #endregion }
public static async Task IdentifyInPersonGroup(IFaceClient client, string recognitionModel, Dictionary <string, string> employees) { Console.WriteLine("========IDENTIFY FACES========"); Console.WriteLine(); EmployeesNames = new Dictionary <string, string>(employees.Count); // Create a person group. Console.WriteLine($"Create a person group ({personGroupId})."); await client.PersonGroup.CreateAsync(personGroupId, personGroupId, recognitionModel : recognitionModel); // The similar faces will be grouped into a single person group person. foreach (var employee in employees) { // Limit TPS await Task.Delay(250); Person person = await client.PersonGroupPerson.CreateAsync(personGroupId : personGroupId, name : employee.Value); Console.WriteLine($"Create a person group person '{employee.Value}'."); var directoryPath = Path.Combine(@"C:\CheckpointHSE", employee.Key); if (!Directory.Exists(directoryPath)) { //чо делать если нет папки с картинками } EmployeesNames.Add(person.PersonId.ToString(), employee.Value); // Add face to the person group person. foreach (var filePath in Directory.GetFiles(directoryPath)) { using (var fileStream = new FileStream(filePath, FileMode.Open)) { Console.WriteLine($"Add face to the person group person({employee.Value}) from image `{filePath}`"); PersistedFace face = await client.PersonGroupPerson.AddFaceFromStreamAsync(personGroupId, person.PersonId, fileStream); } } } // Start to train the person group. Console.WriteLine(); Console.WriteLine($"Train person group {personGroupId}."); await client.PersonGroup.TrainAsync(personGroupId); // Wait until the training is completed. while (true) { await Task.Delay(1000); var trainingStatus = await client.PersonGroup.GetTrainingStatusAsync(personGroupId); Console.WriteLine($"Training status: {trainingStatus.Status}."); if (trainingStatus.Status == TrainingStatusType.Succeeded) { break; } } Console.WriteLine(); IsTrained = true; }
public async Task <IAddFaceToPersonResponse> Handle(IAddFaceToPersonRequest addFaceToPersonRequest) { PersistedFace persistedFaceResult = null; using (var ms = new MemoryStream(addFaceToPersonRequest.FaceCapture)) { persistedFaceResult = await _faceClient.PersonGroupPerson.AddFaceFromStreamAsync(addFaceToPersonRequest.GroupId.ToString(), addFaceToPersonRequest.PersonId, ms); } return(new AddFaceToPersonResponse(persistedFaceResult)); }
public static async Task CreateSampleProjectAsync(IFaceClient client, string url, string personGroupId) { Dictionary <string, string[]> personDictionary = new Dictionary <string, string[]> { { "Family1-Dad", new[] { "Family1-Dad1.jpg", "Family1-Dad2.jpg" } }, { "Family1-Mom", new[] { "Family1-Mom1.jpg", "Family1-Mom2.jpg" } }, { "Family1-Son", new[] { "Family1-Son1.jpg", "Family1-Son2.jpg" } }, { "Family1-Daughter", new[] { "Family1-Daughter1.jpg", "Family1-Daughter2.jpg" } }, { "Family2-Lady", new[] { "Family2-Lady1.jpg", "Family2-Lady2.jpg" } }, { "Family2-Man", new[] { "Family2-Man1.jpg", "Family2-Man2.jpg" } } }; // string personGroupId = Guid.NewGuid().ToString(); // sourcePersonGroup = personGroupId; // This is solely for the snapshot operations example Console.WriteLine($"Create a person group ({personGroupId})."); await client.PersonGroup.CreateAsync(personGroupId, personGroupId, recognitionModel : PersonGroupSample.RECOGNITION_MODEL1); // The similar faces will be grouped into a single person group person. foreach (var groupedFace in personDictionary.Keys) { // Limit TPS await Task.Delay(250); Person person = await client.PersonGroupPerson.CreateAsync(personGroupId : personGroupId, name : groupedFace); Console.WriteLine($"Create a person group person '{groupedFace}'."); // Add face to the person group person. foreach (var similarImage in personDictionary[groupedFace]) { Console.WriteLine($"Add face to the person group person({groupedFace}) from image `{similarImage}`"); PersistedFace face = await client.PersonGroupPerson.AddFaceFromUrlAsync(personGroupId, person.PersonId, $"{url}{similarImage}", similarImage); } } Console.WriteLine(); Console.WriteLine($"Train person group {personGroupId}."); await client.PersonGroup.TrainAsync(personGroupId); // Wait until the training is completed. while (true) { await Task.Delay(1000); var trainingStatus = await client.PersonGroup.GetTrainingStatusAsync(personGroupId); Console.WriteLine($"Training status: {trainingStatus.Status}."); if (trainingStatus.Status == TrainingStatusType.Succeeded) { break; } } }
// Создает БД из фотографий, хранящихся на устройстве private static async Task IdentifyInPersonGroup(IFaceClient client, string url, string recognitionModel) { Dictionary <string, string[]> personDictionary = new Dictionary <string, string[]> { { "Ekaterina S.", new[] { "ES1.jpg", "ES2.jpg" } }, { "Polina P.", new[] { "PP1.jpg", "PP2.jpg" } }, { "Polina B.", new[] { "PB1.jpg", "PB2.jpg" } }, }; Console.WriteLine($"Создание person group ({personGroupId})."); client.PersonGroup.CreateAsync(personGroupId, personGroupId, recognitionModel: recognitionModel).Wait(); foreach (var groupedFace in personDictionary.Keys) { // Limit TPS await Task.Delay(250); Person person = await client.PersonGroupPerson.CreateAsync(personGroupId : personGroupId, name : groupedFace); Console.WriteLine($"Create a person group person '{groupedFace}'."); // Добавление лица в person group person. foreach (var similarImage in personDictionary[groupedFace]) { FileStream stream = new FileStream(url + similarImage, FileMode.Open); PersistedFace face = await client.PersonGroupPerson.AddFaceFromStreamAsync(personGroupId, person.PersonId, stream, similarImage); } } // Тренировка person group await client.PersonGroup.TrainAsync(personGroupId); // Ожидание окончания тренировки while (true) { await Task.Delay(1000); var trainingStatus = await client.PersonGroup.GetTrainingStatusAsync(personGroupId); if (trainingStatus.Status == TrainingStatusType.Succeeded) { break; } } }
public async Task <IList <PersistedFace> > AddFacesToGroup(FaceProcess faces) { IList <PersistedFace> persistedFaces = new List <PersistedFace>(); Person person = await _faceClient.FaceClicent().PersonGroupPerson.CreateAsync(personGroupId: groupId, name: faces.Matric); foreach (var similarImage in faces.Images) { using Stream fileStream = File.OpenRead(similarImage); PersistedFace face = await _faceClient.FaceClicent().PersonGroupPerson.AddFaceFromStreamAsync(groupId, person.PersonId, fileStream, similarImage); persistedFaces.Add(face); } return(persistedFaces); }
public static async Task AddGroup(IFaceClient client, List <string> urls, Person Personname, string personGroupId) { await Task.Delay(250); foreach (var url in urls) { // Limit TPS await Task.Delay(250); string userddata = url.Split('?')[0].Split('/')[4]; PersistedFace face = await client.PersonGroupPerson.AddFaceFromUrlAsync(personGroupId, Personname.PersonId, url, userddata); // Add face to the person group person. } }
private async void OnDeleteFaceClicked(object sender, RoutedEventArgs e) { try { foreach (var item in this.selectedPersonFacesGridView.SelectedItems.ToArray()) { PersistedFace personFace = (PersistedFace)item; await FaceServiceHelper.DeletePersonFaceAsync(this.CurrentPersonGroup.PersonGroupId, this.SelectedPerson.PersonId, personFace.PersistedFaceId); this.SelectedPersonFaces.Remove(personFace); this.needsTraining = true; } } catch (Exception ex) { await Util.GenericApiCallExceptionHandler(ex, "Failure deleting images"); } }
public async Task <string> FaceRegister(Guid personalId, string photo) { //string filePath = "C:\\2.jpg"; string base64 = photo.Substring(photo.IndexOf(',') + 1); byte[] data = Convert.FromBase64String(base64); try { using (Stream s = new MemoryStream(data)) { //faceClient.PersonGroup. PersistedFace persisted = await faceClient.PersonGroupPerson.AddFaceFromStreamAsync( personalGroupId, personalId, s); //persisted. return(persisted.PersistedFaceId.ToString()); } }catch (Exception) { return("Failed"); } }
public static async Task <GetRecognisedUserViewModel> IdentifyInPersonGroup(IFaceClient client, List <InPutDictlist> personDictionary, string url, string url2, string sourceImageFileName, string recognitionModel) { GetRecognisedUserViewModel getRecognisedUserViewModel = new GetRecognisedUserViewModel(); string OutPut = "Initializing..... \n Creating new Personal Model \n "; // Create a dictionary for all your images, grouping similar ones under the same key. // A group photo that includes some of the persons you seek to identify from your dictionary. // string sourceImageFileName = "identification1.jpg"; // Create a person group. // Console.WriteLine($"Create a person group ({personGroupId})."); OutPut = OutPut + "person group identity: " + personGroupId + ". \n"; await client.PersonGroup.CreateAsync(personGroupId, personGroupId, recognitionModel : recognitionModel); // The similar faces will be grouped into a single person group person. foreach (var groupedFace in personDictionary) { // Limit TPS await Task.Delay(250); Microsoft.Azure.CognitiveServices.Vision.Face.Models.Person person = await client.PersonGroupPerson.CreateAsync(personGroupId, groupedFace.Name); // Console.WriteLine($"Create a person group person '{groupedFace}'."); // Add face to the person group person. foreach (var similarImage in groupedFace.ImageName) { OutPut = OutPut + "Add face to the person group person( " + groupedFace + ") from image `" + similarImage + "` \n "; string urlzz = url + similarImage; PersistedFace face = await client.PersonGroupPerson.AddFaceFromUrlAsync(personGroupId, person.PersonId, urlzz, similarImage); } } // Start to train the person group. OutPut = OutPut + "\n \n Now training Person Group: " + personGroupId + "\n"; await client.PersonGroup.TrainAsync(personGroupId); // Wait until the training is completed. while (true) { await Task.Delay(1000); var trainingStatus = await client.PersonGroup.GetTrainingStatusAsync(personGroupId); OutPut = OutPut + "Training status: " + trainingStatus.Status + ". \n"; if (trainingStatus.Status == TrainingStatusType.Succeeded) { break; } } Console.WriteLine(); List <Guid> sourceFaceIds = new List <Guid>(); // Detect faces from source image url. List <DetectedFace> detectedFaces = await DetectFaceRecognize(client, $"{url2}{sourceImageFileName}", recognitionModel); // Add detected faceId to sourceFaceIds. foreach (var detectedFace in detectedFaces) { sourceFaceIds.Add(detectedFace.FaceId.Value); } // Identify the faces in a person group. var identifyResults = await client.Face.IdentifyAsync(sourceFaceIds, personGroupId); double numberstream = 0.0; Microsoft.Azure.CognitiveServices.Vision.Face.Models.Person outputperson = new Microsoft.Azure.CognitiveServices.Vision.Face.Models.Person(); foreach (var identifyResult in identifyResults) { Microsoft.Azure.CognitiveServices.Vision.Face.Models.Person person = await client.PersonGroupPerson.GetAsync(personGroupId, identifyResult.Candidates[0].PersonId); OutPut = OutPut + "Person: '" + person.Name + "' is identified for face in: " + sourceImageFileName + " - " + identifyResult.FaceId + " \n"; List <Guid> gruildie = new List <Guid>(); gruildie.Add(identifyResult.FaceId); if (identifyResult.Candidates[0].Confidence > numberstream) { outputperson.Name = person.Name; outputperson.PersonId = person.PersonId; outputperson.PersistedFaceIds = gruildie; getRecognisedUserViewModel.ConfidenceLevel = identifyResult.Candidates[0].Confidence; } } await DeletePersonGroup(client, personGroupId); OutPut = OutPut + "\n \n Person Id group deleted."; getRecognisedUserViewModel.Answers = OutPut; getRecognisedUserViewModel.highestperson = outputperson; // At end, delete person groups in both regions (since testing only) return(getRecognisedUserViewModel); }
public static async Task Run(string endpoint, string key) { Console.WriteLine("Sample of finding similar faces in face list."); IFaceClient client = new FaceClient(new ApiKeyServiceClientCredentials(key)) { Endpoint = endpoint }; const string ImageUrlPrefix = "https://csdx.blob.core.windows.net/resources/Face/Images/"; List <string> targetImageFileNames = new List <string> { "Family1-Dad1.jpg", "Family1-Daughter1.jpg", "Family1-Mom1.jpg", "Family1-Son1.jpg", "Family2-Lady1.jpg", "Family2-Man1.jpg", "Family3-Lady1.jpg", "Family3-Man1.jpg" }; string sourceImageFileName = "findsimilar.jpg"; // Create a face list. string faceListId = Guid.NewGuid().ToString(); Console.WriteLine($"Create FaceList {faceListId}."); await client.FaceList.CreateAsync( faceListId, "face list for FindSimilar sample", "face list for FindSimilar sample"); foreach (var targetImageFileName in targetImageFileNames) { // Add face to face list. var faces = await client.FaceList.AddFaceFromUrlAsync( faceListId, $"{ImageUrlPrefix}{targetImageFileName}", targetImageFileName); if (faces == null) { throw new Exception($"No face detected from image `{targetImageFileName}`."); } Console.WriteLine($"Face from image {targetImageFileName} is successfully added to the face list."); } // Get persisted faces from the face list. List <PersistedFace> persistedFaces = (await client.FaceList.GetAsync(faceListId)).PersistedFaces.ToList(); if (persistedFaces.Count == 0) { throw new Exception($"No persisted face in face list '{faceListId}'."); } // Detect faces from source image url. IList <DetectedFace> detectedFaces = await Common.DetectFaces( client, $"{ImageUrlPrefix}{sourceImageFileName}"); // Find similar example of faceId to face list. var similarResults = await client.Face.FindSimilarAsync(detectedFaces[0].FaceId.Value, faceListId); foreach (var similarResult in similarResults) { PersistedFace persistedFace = persistedFaces.Find(face => face.PersistedFaceId == similarResult.PersistedFaceId); if (persistedFace == null) { Console.WriteLine("Persisted face not found in similar result."); continue; } Console.WriteLine( $"Faces from {sourceImageFileName} & {persistedFace.UserData} are similar with confidence: {similarResult.Confidence}."); } // Delete the face list. await client.FaceList.DeleteAsync(faceListId); Console.WriteLine($"Delete FaceList {faceListId}."); Console.WriteLine(); }
public static async Task Run(string endpoint, string key) { Console.WriteLine("Sample of verify face to person group."); IFaceClient client = new FaceClient(new ApiKeyServiceClientCredentials(key)) { Endpoint = endpoint }; string recognitionModel = RecognitionModel.Recognition02; string detectionModel = DetectionModel.Detection02; const string ImageUrlPrefix = "https://csdx.blob.core.windows.net/resources/Face/Images/"; List <string> targetImageFileNames = new List <string> { "Family1-Dad1.jpg", "Family1-Dad2.jpg" }; string sourceImageFileName1 = "Family1-Dad3.jpg"; // Create a person group. string personGroupId = Guid.NewGuid().ToString(); Console.WriteLine($"Create a person group ({personGroupId})."); await client.PersonGroup.CreateAsync(personGroupId, personGroupId, recognitionModel : recognitionModel); // Create a person group person. Person p = new Person { Name = "Dad", UserData = "Person for sample" }; Console.WriteLine($"Create a person group person '{p.Name}'."); p.PersonId = (await client.PersonGroupPerson.CreateAsync(personGroupId, p.Name)).PersonId; foreach (var targetImageFileName in targetImageFileNames) { // Add face to the person group. Console.WriteLine($"Add face to the person group person({p.Name}) from image `{targetImageFileName}`."); PersistedFace faces = await client.PersonGroupPerson.AddFaceFromUrlAsync( personGroupId, p.PersonId, $"{ImageUrlPrefix}{targetImageFileName}", targetImageFileName, detectionModel : detectionModel); if (faces == null) { throw new Exception($"No persisted face from image `{targetImageFileName}`."); } } List <Guid> faceIds = new List <Guid>(); // Add detected faceId to faceIds. List <DetectedFace> detectedFaces = await Common.DetectFaces( client, $"{ImageUrlPrefix}{sourceImageFileName1}", recognitionModel : recognitionModel, detectionModel : detectionModel); faceIds.Add(detectedFaces[0].FaceId.Value); // Verification example for faces of the same person. VerifyResult verifyResult = await client.Face.VerifyFaceToPersonAsync(faceIds[0], p.PersonId, personGroupId); Console.WriteLine( verifyResult.IsIdentical ? $"Faces from {sourceImageFileName1} & {p.Name} are of the same (Positive) person, similarity confidence: {verifyResult.Confidence}." : $"Faces from {sourceImageFileName1} & {p.Name} are of different (Negative) persons, similarity confidence: {verifyResult.Confidence}."); // Delete the person group. Console.WriteLine($"Delete the person group ({personGroupId})."); await client.PersonGroup.DeleteAsync(personGroupId); Console.WriteLine(); }
public AddFaceToPersonResponse(PersistedFace persistedFace) { persistedFace.Validate(); _persistedFace = persistedFace; }
public static async Task davesIdentifyInPersonGroup(IFaceClient client, string url, string rcgMdl) { string nl = Environment.NewLine; StringBuilder sbd = new StringBuilder(); //Download images from internet: string imgDir = Directory.GetParent(Directory.GetParent(Directory.GetParent( Environment.CurrentDirectory).FullName).FullName).FullName + @"\identifyImages\"; await client.PersonGroup.CreateAsync(personGroupId, personGroupId, recognitionModel : rcgMdl); Console.WriteLine("The PersistedFace GUIDs: "); foreach (string prsn in personDictionary.Keys) { Person person = await client.PersonGroupPerson.CreateAsync(personGroupId : personGroupId, name : prsn); foreach (string pic in personDictionary[prsn]) { StreamReader srd = new StreamReader(imgDir + pic); PersistedFace pfc = await client.PersonGroupPerson.AddFaceFromStreamAsync( personGroupId, person.PersonId, srd.BaseStream, pic); Console.WriteLine($"{pfc.PersistedFaceId} - {pic} "); } } await client.PersonGroup.TrainAsync(personGroupId); Console.WriteLine("Training Statuses:"); while (true) { await Task.Delay(50); var trSt = await client.PersonGroup.GetTrainingStatusAsync(personGroupId); Console.WriteLine("\t" + trSt.Status.ToString()); if (trSt.Status == TrainingStatusType.Succeeded) { break; } } List <Guid?> guidsPplInFamilyPic = new List <Guid?>(); StreamReader strmFullFamily = new StreamReader(imgDir + "identification1.jpg"); IList <DetectedFace> idf = await client.Face.DetectWithStreamAsync(strmFullFamily.BaseStream, recognitionModel : RecognitionModel.Recognition03, detectionModel : DetectionModel.Detection02); List <DetectedFace> ldf = idf.ToList(); int cntFace = 0; Console.WriteLine($"The {ldf.Count} detected faces in the full family pic: "); foreach (DetectedFace dfc in ldf) { guidsPplInFamilyPic.Add(dfc.FaceId); } IList <IdentifyResult> iir = await client.Face.IdentifyAsync(guidsPplInFamilyPic, personGroupId); foreach (IdentifyResult ir in iir) { Console.WriteLine($"\tFace{++cntFace}"); foreach (IdentifyCandidate icd in ir.Candidates.OrderByDescending(c => c.Confidence)) { Person prsn = await client.PersonGroupPerson.GetAsync(personGroupId, icd.PersonId); Console.Write($"\t\t{prsn.Name}: {icd.Confidence} {nl}"); } } }
public static void UpdateFace(ObservableCollection <Models.Face> collections, string imagePath, PersistedFace face) { var renderingImage = LoadImageAppliedOrientation(imagePath); collections.Add(new Models.Face() { ImageFile = renderingImage, FaceId = face.PersistedFaceId.ToString(), }); }
/* * Identify faces * To identify faces, you need to create and define a person group. */ public static async Task IdentifyInPersonGroup(IFaceClient client, string recognitionModel1) { Console.WriteLine("========Sample of identifing faces in a person group========"); // Create a dictionary for all your images, grouping similar ones under the same key. Dictionary <string, string[]> targetImageFileDictionary = new Dictionary <string, string[]> { { "Family1-Dad", new[] { "Family1-Dad1.jpg", "Family1-Dad2.jpg" } }, { "Family1-Mom", new[] { "Family1-Mom1.jpg", "Family1-Mom2.jpg" } }, { "Family1-Son", new[] { "Family1-Son1.jpg", "Family1-Son2.jpg" } }, { "Family1-Daughter", new[] { "Family1-Daughter1.jpg", "Family1-Daughter2.jpg" } }, { "Family2-Lady", new[] { "Family2-Lady1.jpg", "Family2-Lady2.jpg" } }, { "Family2-Man", new[] { "Family2-Man1.jpg", "Family2-Man2.jpg" } } }; // A group photo that includes some of the persons you seek to identify from your dictionary. string sourceImageFileName = "identification1.jpg"; // Create a person group. string personGroupId = Guid.NewGuid().ToString(); sourcePersonGroup = personGroupId; Console.WriteLine($"Create a person group ({personGroupId})."); await client.PersonGroup.CreateAsync(personGroupId, personGroupId, recognitionModel : recognitionModel1); // The similar faces will be grouped into a single person group person. foreach (var targetImageFileDictionaryName in targetImageFileDictionary.Keys) { // Create a person group person. Person person = new Person { Name = targetImageFileDictionaryName, UserData = "Person for sample" }; // Limit TPS await Task.Delay(250); person.PersonId = (await client.PersonGroupPerson.CreateAsync(personGroupId, person.Name)).PersonId; Console.WriteLine($"Create a person group person '{person.Name}'."); // Add face to the person group person. foreach (var targetImageFileName in targetImageFileDictionary[targetImageFileDictionaryName]) { Console.WriteLine($"Add face to the person group person({targetImageFileDictionaryName}) from image `{targetImageFileName}`."); PersistedFace face = await client.PersonGroupPerson.AddFaceFromUrlAsync(personGroupId, person.PersonId, $"{IMAGE_BASE_URL}{targetImageFileName}", targetImageFileName); } } // Start to train the person group. Console.WriteLine(); Console.WriteLine($"Train person group {personGroupId}."); await client.PersonGroup.TrainAsync(personGroupId); // Wait until the training is completed. while (true) { await Task.Delay(1000); var trainingStatus = await client.PersonGroup.GetTrainingStatusAsync(personGroupId); Console.WriteLine($"Training status: {trainingStatus.Status}."); if (trainingStatus.Status == TrainingStatusType.Succeeded) { break; } } Console.WriteLine(); List <Guid> sourceFaceIds = new List <Guid>(); // Detect faces from source image url. List <DetectedFace> detectedFaces = await DetectFaceRecognize(client, $"{IMAGE_BASE_URL}{sourceImageFileName}", recognitionModel1); // Add detected faceId to sourceFaceIds. foreach (var detectedFace in detectedFaces) { sourceFaceIds.Add(detectedFace.FaceId.Value); } // Identify the faces in a person group. var identifyResults = await client.Face.IdentifyAsync(sourceFaceIds, personGroupId); foreach (var identifyResult in identifyResults) { Person person = await client.PersonGroupPerson.GetAsync(personGroupId, identifyResult.Candidates[0].PersonId); Console.WriteLine($"Person '{person.Name}' is identified for face in: {sourceImageFileName} - {identifyResult.FaceId}," + $" confidence: {identifyResult.Candidates[0].Confidence}."); } Console.WriteLine(); }
public static async Task UpdatePersonGroupsWithNewRecModelAsync(PersonGroup oldPersonGroup, string userDataFilder, IProgress <FaceIdentificationModelUpdateStatus> progress = null) { try { bool allPeopleHaveAtLeastOneFaceMigrated = true; // just make sure the person group use previous recognition model bool isOldPersonGroup = oldPersonGroup?.RecognitionModel != null && !oldPersonGroup.RecognitionModel.Equals(LatestRecognitionModelName, StringComparison.OrdinalIgnoreCase); // get persons IList <Person> personsInGroup = isOldPersonGroup ? await GetPersonsAsync(oldPersonGroup.PersonGroupId) : new List <Person>(); if (personsInGroup.Any()) { // create new person group string newPersonGroupId = Guid.NewGuid().ToString(); await CreatePersonGroupAsync(newPersonGroupId, oldPersonGroup.Name, userDataFilder); // create new persons var newPersonList = new List <Tuple <Person, Person> >(); foreach (Person oldPerson in personsInGroup) { Person newPerson = await CreatePersonAsync(newPersonGroupId, oldPerson.Name); newPersonList.Add(new Tuple <Person, Person>(oldPerson, newPerson)); } // add face images foreach (var(personItem, index) in newPersonList.Select((v, i) => (v, i))) { Person oldPerson = personItem.Item1; Person newPerson = personItem.Item2; // get face images from the old model var personFaces = new List <PersistedFace>(); foreach (Guid face in oldPerson.PersistedFaceIds) { PersistedFace personFace = await GetPersonFaceAsync(oldPersonGroup.PersonGroupId, oldPerson.PersonId, face); personFaces.Add(personFace); } bool addedAtLeastOneFaceImageForPerson = false; // add face images to the new model foreach (PersistedFace persistedFace in personFaces) { try { bool isUri = !string.IsNullOrEmpty(persistedFace.UserData) ? Uri.IsWellFormedUriString(persistedFace.UserData, UriKind.Absolute) : false; if (isUri) { await AddPersonFaceFromUrlAsync(newPersonGroupId, newPerson.PersonId, imageUrl : persistedFace.UserData, userData : persistedFace.UserData, targetFaceRect : null); } else { StorageFile localImage = await StorageFile.GetFileFromPathAsync(persistedFace.UserData); await AddPersonFaceFromStreamAsync(newPersonGroupId, newPerson.PersonId, imageStreamCallback : localImage.OpenStreamForReadAsync, userData : localImage.Path, targetFaceRect : null); } addedAtLeastOneFaceImageForPerson = true; } catch { /* Ignore the error and continue. Other images might work */ } } if (!addedAtLeastOneFaceImageForPerson) { allPeopleHaveAtLeastOneFaceMigrated = false; } progress?.Report(new FaceIdentificationModelUpdateStatus { State = FaceIdentificationModelUpdateState.Running, Count = index + 1, Total = personsInGroup.Count }); } // delete old person group await DeletePersonGroupAsync(oldPersonGroup.PersonGroupId); // train new person group await TrainPersonGroupAsync(newPersonGroupId); } progress?.Report(new FaceIdentificationModelUpdateStatus { State = allPeopleHaveAtLeastOneFaceMigrated ? FaceIdentificationModelUpdateState.Complete : FaceIdentificationModelUpdateState.CompletedWithSomeEmptyPeople }); } catch (Exception ex) { ErrorTrackingHelper.TrackException(ex, "Face API: Update PersonGroup using new recognition model error"); progress?.Report(new FaceIdentificationModelUpdateStatus { State = FaceIdentificationModelUpdateState.Error }); } }
public static async Task Run(string endpoint, string key) { Console.WriteLine("Sample of finding similar faces in large face list."); IFaceClient client = new FaceClient(new ApiKeyServiceClientCredentials(key)) { Endpoint = endpoint }; string recognitionModel = RecognitionModel.Recognition02; const string ImageUrlPrefix = "https://csdx.blob.core.windows.net/resources/Face/Images/"; List <string> targetImageFileNames = new List <string> { "Family1-Dad1.jpg", "Family1-Daughter1.jpg", "Family1-Mom1.jpg", "Family1-Son1.jpg", "Family2-Lady1.jpg", "Family2-Man1.jpg", "Family3-Lady1.jpg", "Family3-Man1.jpg" }; string sourceImageFileName = "findsimilar.jpg"; // Create a large face list. string largeFaceListId = Guid.NewGuid().ToString(); Console.WriteLine($"Create large face list {largeFaceListId}."); await client.LargeFaceList.CreateAsync( largeFaceListId, "large face list for FindSimilar sample", "large face list for FindSimilar sample", recognitionModel : recognitionModel); foreach (var targetImageFileName in targetImageFileNames) { // Add face to the large face list. var faces = await client.LargeFaceList.AddFaceFromUrlAsync( largeFaceListId, $"{ImageUrlPrefix}{targetImageFileName}", targetImageFileName); if (faces == null) { throw new Exception($"No face detected from image `{targetImageFileName}`."); } Console.WriteLine( $"Face from image {targetImageFileName} is successfully added to the large face list."); } // Start to train the large face list. Console.WriteLine($"Train large face list {largeFaceListId}."); await client.LargeFaceList.TrainAsync(largeFaceListId); // Wait until the training is completed. while (true) { await Task.Delay(1000); var trainingStatus = await client.LargeFaceList.GetTrainingStatusAsync(largeFaceListId); Console.WriteLine($"Training status is {trainingStatus.Status}."); if (trainingStatus.Status != TrainingStatusType.Running) { if (trainingStatus.Status == TrainingStatusType.Failed) { throw new Exception($"Training failed with message {trainingStatus.Message}."); } break; } } // Get persisted faces from the large face list. List <PersistedFace> persistedFaces = (await client.LargeFaceList.ListFacesAsync(largeFaceListId)).ToList(); if (persistedFaces.Count == 0) { throw new Exception($"No persisted face in large face list '{largeFaceListId}'."); } // Detect faces from source image url. IList <DetectedFace> detectedFaces = await Common.DetectFaces( client, $"{ImageUrlPrefix}{sourceImageFileName}", recognitionModel : recognitionModel); // Find similar example of faceId to large face list. var similarResults = await client.Face.FindSimilarAsync( detectedFaces[0].FaceId.Value, null, largeFaceListId); foreach (var similarResult in similarResults) { PersistedFace persistedFace = persistedFaces.Find(face => face.PersistedFaceId == similarResult.PersistedFaceId); if (persistedFace == null) { Console.WriteLine("Persisted face not found in similar result."); continue; } Console.WriteLine( $"Faces from {sourceImageFileName} & {persistedFace.UserData} are similar with confidence: {similarResult.Confidence}."); } // Delete the large face list. await client.LargeFaceList.DeleteAsync(largeFaceListId); Console.WriteLine($"Delete LargeFaceList {largeFaceListId}."); Console.WriteLine(); }
/* * END - VERIFY */ /* * IDENTIFY FACES * To identify faces, you need to create and define a person group. * The Identify operation takes one or several face IDs from DetectedFace or PersistedFace and a PersonGroup and returns * a list of Person objects that each face might belong to. Returned Person objects are wrapped as Candidate objects, * which have a prediction confidence value. */ // <snippet_persongroup_files> public static async Task IdentifyInPersonGroup(IFaceClient client, string url, string recognitionModel) { Console.WriteLine("========IDENTIFY FACES========"); Console.WriteLine(); // Create a dictionary for all your images, grouping similar ones under the same key. Dictionary <string, string[]> personDictionary = new Dictionary <string, string[]> { { "Family1-Dad", new[] { "Family1-Dad1.jpg", "Family1-Dad2.jpg" } }, { "Family1-Mom", new[] { "Family1-Mom1.jpg", "Family1-Mom2.jpg" } }, { "Family1-Son", new[] { "Family1-Son1.jpg", "Family1-Son2.jpg" } }, { "Family1-Daughter", new[] { "Family1-Daughter1.jpg", "Family1-Daughter2.jpg" } }, { "Family2-Lady", new[] { "Family2-Lady1.jpg", "Family2-Lady2.jpg" } }, { "Family2-Man", new[] { "Family2-Man1.jpg", "Family2-Man2.jpg" } } }; // A group photo that includes some of the persons you seek to identify from your dictionary. string sourceImageFileName = "identification1.jpg"; // </snippet_persongroup_files> // <snippet_persongroup_create> // Create a person group. Console.WriteLine($"Create a person group ({personGroupId})."); await client.PersonGroup.CreateAsync(personGroupId, personGroupId, recognitionModel : recognitionModel); // The similar faces will be grouped into a single person group person. foreach (var groupedFace in personDictionary.Keys) { // Limit TPS await Task.Delay(250); Person person = await client.PersonGroupPerson.CreateAsync(personGroupId : personGroupId, name : groupedFace); Console.WriteLine($"Create a person group person '{groupedFace}'."); // Add face to the person group person. foreach (var similarImage in personDictionary[groupedFace]) { Console.WriteLine($"Add face to the person group person({groupedFace}) from image `{similarImage}`"); PersistedFace face = await client.PersonGroupPerson.AddFaceFromUrlAsync(personGroupId, person.PersonId, $"{url}{similarImage}", similarImage); } } // </snippet_persongroup_create> // <snippet_persongroup_train> // Start to train the person group. Console.WriteLine(); Console.WriteLine($"Train person group {personGroupId}."); await client.PersonGroup.TrainAsync(personGroupId); // Wait until the training is completed. while (true) { await Task.Delay(1000); var trainingStatus = await client.PersonGroup.GetTrainingStatusAsync(personGroupId); Console.WriteLine($"Training status: {trainingStatus.Status}."); if (trainingStatus.Status == TrainingStatusType.Succeeded) { break; } } Console.WriteLine(); // </snippet_persongroup_train> // <snippet_identify_sources> List <Guid?> sourceFaceIds = new List <Guid?>(); // Detect faces from source image url. List <DetectedFace> detectedFaces = await DetectFaceRecognize(client, $"{url}{sourceImageFileName}", recognitionModel); // Add detected faceId to sourceFaceIds. foreach (var detectedFace in detectedFaces) { sourceFaceIds.Add(detectedFace.FaceId.Value); } // </snippet_identify_sources> // <snippet_identify> // Identify the faces in a person group. var identifyResults = await client.Face.IdentifyAsync(sourceFaceIds, personGroupId); foreach (var identifyResult in identifyResults) { Person person = await client.PersonGroupPerson.GetAsync(personGroupId, identifyResult.Candidates[0].PersonId); Console.WriteLine($"Person '{person.Name}' is identified for face in: {sourceImageFileName} - {identifyResult.FaceId}," + $" confidence: {identifyResult.Candidates[0].Confidence}."); } Console.WriteLine(); }
public async Task Register() { IsRegistrationEnabled = false; try { IEnumerable <PersonGroup> PersonGroups = null; int i = 3; //retry count // 1. Select group // If the group is full, another one needs to added manually with alphabetically preceeding name do { PersonGroups = await FaceServiceHelper.ListPersonGroupsAsync(SettingsHelper.Instance.WorkspaceKey); if (PersonGroups.Count() > 0) { CurrentPersonGroup = PersonGroups.OrderBy(pg => pg.Name).First(); } // Do not forget to create a persons group if there is not one else { await FaceServiceHelper.CreatePersonGroupAsync(Guid.NewGuid().ToString(), "DefaultGroup", SettingsHelper.Instance.WorkspaceKey); } i--; } while (PersonGroups.Count() < 1 && i >= 0); // 2. Create a person in that group CurrentPerson = await FaceServiceHelper.CreatePersonAsync(this.CurrentPersonGroup.PersonGroupId, FullName); // 3. Add face to that person CurrentFace = await FaceServiceHelper.AddPersonFaceFromStreamAsync( CurrentPersonGroup.PersonGroupId, CurrentPerson.PersonId, imageStreamCallback : customerFace.GetImageStreamCallback, userData : customerFace.LocalImagePath, targetFaceRect : null); // 4. Train model await FaceServiceHelper.TrainPersonGroupAsync(CurrentPersonGroup.PersonGroupId); bool trainingSucceeded = false; bool recordAdded = false; while (true) { TrainingStatus trainingStatus = await FaceServiceHelper.GetPersonGroupTrainingStatusAsync(CurrentPersonGroup.PersonGroupId); if (trainingStatus.Status == TrainingStatusType.Succeeded) { trainingSucceeded = true; break; } else if (trainingStatus.Status == TrainingStatusType.Failed) { break; } await Task.Delay(100); } // 5. Add record to the database if (trainingSucceeded) { customerInfo = new CustomerRegistrationInfo { CustomerFaceHash = CurrentPerson.PersonId.ToString(), CustomerName = FullName, RegistrationDate = DateTime.Now }; recordAdded = IgniteDataAccess.CreateCustomerRecord(customerInfo.CustomerFaceHash, customerInfo.CustomerName); } // 6. Update status if (trainingSucceeded && recordAdded) { StatusText = "Success!"; StatusTextColor = Util.ToBrush("green"); } else { customerInfo = null; IsRegistrationEnabled = true; StatusText = "Please try again later"; StatusTextColor = Util.ToBrush("red"); ResumeCamera?.Invoke(this, EventArgs.Empty); } } catch (Exception) { customerInfo = null; IsRegistrationEnabled = true; StatusText = "Please try again"; StatusTextColor = Util.ToBrush("red"); ResumeCamera?.Invoke(this, EventArgs.Empty); } }
/* * END - IDENTIFY FACES */ /* * LARGE PERSON GROUP * The example will create a large person group, retrieve information from it, * list the Person IDs it contains, and finally delete a large person group. * For simplicity, the same images are used for the regular-sized person group in IDENTIFY FACES of this quickstart. * A large person group is made up of person group persons. * One person group person is made up of many similar images of that person, which are each PersistedFace objects. */ public static async Task LargePersonGroup(IFaceClient client, string url, string recognitionModel) { Console.WriteLine("========LARGE PERSON GROUP========"); Console.WriteLine(); // Create a dictionary for all your images, grouping similar ones under the same key. Dictionary <string, string[]> personDictionary = new Dictionary <string, string[]> { { "Family1-Dad", new[] { "Family1-Dad1.jpg", "Family1-Dad2.jpg" } }, { "Family1-Mom", new[] { "Family1-Mom1.jpg", "Family1-Mom2.jpg" } }, { "Family1-Son", new[] { "Family1-Son1.jpg", "Family1-Son2.jpg" } }, { "Family1-Daughter", new[] { "Family1-Daughter1.jpg", "Family1-Daughter2.jpg" } }, { "Family2-Lady", new[] { "Family2-Lady1.jpg", "Family2-Lady2.jpg" } }, { "Family2-Man", new[] { "Family2-Man1.jpg", "Family2-Man2.jpg" } } }; // Create a large person group ID. string largePersonGroupId = Guid.NewGuid().ToString(); Console.WriteLine($"Create a large person group ({largePersonGroupId})."); // Create the large person group await client.LargePersonGroup.CreateAsync(largePersonGroupId : largePersonGroupId, name : largePersonGroupId, recognitionModel); // Create Person objects from images in our dictionary // We'll store their IDs in the process List <Guid> personIds = new List <Guid>(); foreach (var groupedFace in personDictionary.Keys) { // Limit TPS await Task.Delay(250); Person personLarge = await client.LargePersonGroupPerson.CreateAsync(largePersonGroupId, groupedFace); Console.WriteLine(); Console.WriteLine($"Create a large person group person '{groupedFace}' ({personLarge.PersonId})."); // Store these IDs for later retrieval personIds.Add(personLarge.PersonId); // Add face to the large person group person. foreach (var image in personDictionary[groupedFace]) { Console.WriteLine($"Add face to the person group person '{groupedFace}' from image `{image}`"); PersistedFace face = await client.LargePersonGroupPerson.AddFaceFromUrlAsync(largePersonGroupId, personLarge.PersonId, $"{url}{image}", image); } } // Start to train the large person group. Console.WriteLine(); Console.WriteLine($"Train large person group {largePersonGroupId}."); await client.LargePersonGroup.TrainAsync(largePersonGroupId); // Wait until the training is completed. while (true) { await Task.Delay(1000); var trainingStatus = await client.LargePersonGroup.GetTrainingStatusAsync(largePersonGroupId); Console.WriteLine($"Training status: {trainingStatus.Status}."); if (trainingStatus.Status == TrainingStatusType.Succeeded) { break; } } Console.WriteLine(); // Now that we have created and trained a large person group, we can retrieve data from it. // Get list of persons and retrieve data, starting at the first Person ID in previously saved list. IList <Person> persons = await client.LargePersonGroupPerson.ListAsync(largePersonGroupId, start : ""); Console.WriteLine($"Persisted Face IDs (from {persons.Count} large person group persons): "); foreach (Person person in persons) { foreach (Guid pFaceId in person.PersistedFaceIds) { Console.WriteLine($"The person '{person.Name}' has an image with ID: {pFaceId}"); } } Console.WriteLine(); // After testing, delete the large person group, PersonGroupPersons also get deleted. await client.LargePersonGroup.DeleteAsync(largePersonGroupId); Console.WriteLine($"Deleted the large person group {largePersonGroupId}."); Console.WriteLine(); }
private static async Task <SimilarFace> FindSimilarOrInsertAsync(string imageUrl, Func <Task <Stream> > imageStreamCallback, Guid faceId, DetectedFace face) { if (faceLists == null) { await Initialize(); } Tuple <SimilarFace, string> bestMatch = null; bool foundMatch = false; foreach (var faceListId in faceLists.Keys) { try { SimilarFace similarFace = (await FaceServiceHelper.FindSimilarAsync(faceId, faceListId))?.FirstOrDefault(); if (similarFace == null) { continue; } foundMatch = true; if (bestMatch != null) { // We already found a match for this face in another list. Replace the previous one if the new confidence is higher. if (bestMatch.Item1.Confidence < similarFace.Confidence) { bestMatch = new Tuple <SimilarFace, string>(similarFace, faceListId); } } else { bestMatch = new Tuple <SimilarFace, string>(similarFace, faceListId); } } catch (Exception e) { // Catch errors with individual face lists so we can continue looping through all lists. Maybe an answer will come from // another one. ErrorTrackingHelper.TrackException(e, "Face API FindSimilarAsync error"); } } if (!foundMatch) { // If we are here we didnt' find a match, so let's add the face to the first FaceList that we can add it to. We // might create a new list if none exist, and if all lists are full we will delete the oldest face list (based on when we // last match anything on) so that we can add the new one. double maxAngle = 30; if (face.FaceAttributes.HeadPose != null && (Math.Abs(face.FaceAttributes.HeadPose.Yaw) > maxAngle || Math.Abs(face.FaceAttributes.HeadPose.Pitch) > maxAngle || Math.Abs(face.FaceAttributes.HeadPose.Roll) > maxAngle)) { // This isn't a good frontal shot, so let's not use it as the primary example face for this person return(null); } if (!faceLists.Any()) { // We don't have any FaceLists yet. Create one string newFaceListId = Guid.NewGuid().ToString(); await FaceServiceHelper.CreateFaceListAsync(newFaceListId, "ManagedFaceList", FaceListsUserDataFilter); faceLists.Add(newFaceListId, new FaceListInfo { FaceListId = newFaceListId, LastMatchTimestamp = DateTime.Now }); } PersistedFace addResult = null; bool failedToAddToNonFullList = false; foreach (var faceList in faceLists) { if (faceList.Value.IsFull) { continue; } try { if (imageUrl != null) { addResult = await FaceServiceHelper.AddFaceToFaceListFromUrlAsync(faceList.Key, imageUrl, face.FaceRectangle); } else { addResult = await FaceServiceHelper.AddFaceToFaceListFromStreamAsync(faceList.Key, imageStreamCallback, face.FaceRectangle); } break; } catch (Exception ex) { if (ex is APIErrorException && ((APIErrorException)ex).Response.StatusCode == (System.Net.HttpStatusCode) 403) { // FaceList is full. Continue so we can try again with the next FaceList faceList.Value.IsFull = true; continue; } else { failedToAddToNonFullList = true; break; } } } if (addResult == null && !failedToAddToNonFullList) { // We were not able to add the face to an existing list because they were all full. // If possible, let's create a new list now and add the new face to it. If we can't (e.g. we already maxed out on list count), // let's delete an old list, create a new one and add the new face to it. if (faceLists.Count == MaxFaceListCount) { // delete oldest face list var oldestFaceList = faceLists.OrderBy(fl => fl.Value.LastMatchTimestamp).FirstOrDefault(); faceLists.Remove(oldestFaceList.Key); await FaceServiceHelper.DeleteFaceListAsync(oldestFaceList.Key); } // create new list string newFaceListId = Guid.NewGuid().ToString(); await FaceServiceHelper.CreateFaceListAsync(newFaceListId, "ManagedFaceList", FaceListsUserDataFilter); faceLists.Add(newFaceListId, new FaceListInfo { FaceListId = newFaceListId, LastMatchTimestamp = DateTime.Now }); // Add face to new list if (imageUrl != null) { addResult = await FaceServiceHelper.AddFaceToFaceListFromUrlAsync(newFaceListId, imageUrl, face.FaceRectangle); } else { addResult = await FaceServiceHelper.AddFaceToFaceListFromStreamAsync(newFaceListId, imageStreamCallback, face.FaceRectangle); } } if (addResult != null) { bestMatch = new Tuple <SimilarFace, string>(new SimilarFace { Confidence = 1, PersistedFaceId = addResult.PersistedFaceId }, null); } } return(bestMatch?.Item1); }
/* * IDENTIFY FACES * To identify faces, you need to create and define a person group. * The Identify operation takes one or several face IDs from DetectedFace or PersistedFace and a PersonGroup and returns * a list of Person objects that each face might belong to. Returned Person objects are wrapped as Candidate objects, * which have a prediction confidence value. */ // <snippet_persongroup_files> public static async Task IdentifyInPersonGroup(IFaceClient client, string url, string recognitionModel) { Console.WriteLine("========IDENTIFY FACES========"); Console.WriteLine(); // Create a dictionary for all your images, grouping similar ones under the same key. Dictionary <string, string[]> personDictionary = new Dictionary <string, string[]> { { "Family1-Dad", new[] { "Family1-Dad1.jpg", "Family1-Dad2.jpg" } }, { "Family1-Mom", new[] { "Family1-Mom1.jpg", "Family1-Mom2.jpg" } }, { "Family1-Son", new[] { "Family1-Son1.jpg", "Family1-Son2.jpg" } }, { "Family1-Daughter", new[] { "Family1-Daughter1.jpg", "Family1-Daughter2.jpg" } }, { "Family2-Lady", new[] { "Family2-Lady1.jpg", "Family2-Lady2.jpg" } }, { "Family2-Man", new[] { "Family2-Man1.jpg", "Family2-Man2.jpg" } } }; // A group photo that includes some of the persons you seek to identify from your dictionary. string sourceImageFileName = "identification1.jpg"; // </snippet_persongroup_files> // <snippet_persongroup_create> // Create a person group. Console.WriteLine($"Create a person group ({personGroupId})."); await client.PersonGroup.CreateAsync(personGroupId, personGroupId, recognitionModel : recognitionModel); // The similar faces will be grouped into a single person group person. foreach (var groupedFace in personDictionary.Keys) { // Limit TPS await Task.Delay(250); Person person = await client.PersonGroupPerson.CreateAsync(personGroupId : personGroupId, name : groupedFace); Console.WriteLine($"Create a person group person '{groupedFace}'."); // Add face to the person group person. foreach (var similarImage in personDictionary[groupedFace]) { Console.WriteLine($"Check whether image is of sufficient quality for recognition"); IList <DetectedFace> detectedFaces1 = await client.Face.DetectWithUrlAsync($"{url}{similarImage}", recognitionModel : recognitionModel, detectionModel : DetectionModel.Detection03, returnFaceAttributes : new List <FaceAttributeType> { FaceAttributeType.QualityForRecognition }); bool sufficientQuality = true; foreach (var face1 in detectedFaces1) { var faceQualityForRecognition = face1.FaceAttributes.QualityForRecognition; // Only "high" quality images are recommended for person enrollment if (faceQualityForRecognition.HasValue && (faceQualityForRecognition.Value != QualityForRecognition.High)) { sufficientQuality = false; break; } } if (!sufficientQuality) { continue; } Console.WriteLine($"Add face to the person group person({groupedFace}) from image `{similarImage}`"); PersistedFace face = await client.PersonGroupPerson.AddFaceFromUrlAsync(personGroupId, person.PersonId, $"{url}{similarImage}", similarImage); } } // Start to train the person group. Console.WriteLine(); Console.WriteLine($"Train person group {personGroupId}."); await client.PersonGroup.TrainAsync(personGroupId); // Wait until the training is completed. while (true) { await Task.Delay(1000); var trainingStatus = await client.PersonGroup.GetTrainingStatusAsync(personGroupId); Console.WriteLine($"Training status: {trainingStatus.Status}."); if (trainingStatus.Status == TrainingStatusType.Succeeded) { break; } } Console.WriteLine(); List <Guid> sourceFaceIds = new List <Guid>(); // Detect faces from source image url. List <DetectedFace> detectedFaces = await DetectFaceRecognize(client, $"{url}{sourceImageFileName}", recognitionModel); // Add detected faceId to sourceFaceIds. foreach (var detectedFace in detectedFaces) { sourceFaceIds.Add(detectedFace.FaceId.Value); } // Identify the faces in a person group. var identifyResults = await client.Face.IdentifyAsync(sourceFaceIds, personGroupId); foreach (var identifyResult in identifyResults) { if (identifyResult.Candidates.Count == 0) { Console.WriteLine($"No person is identified for the face in: {sourceImageFileName} - {identifyResult.FaceId},"); continue; } Person person = await client.PersonGroupPerson.GetAsync(personGroupId, identifyResult.Candidates[0].PersonId); Console.WriteLine($"Person '{person.Name}' is identified for the face in: {sourceImageFileName} - {identifyResult.FaceId}," + $" confidence: {identifyResult.Candidates[0].Confidence}."); } Console.WriteLine(); }
public static async Task Run(string endpoint, string key) { Console.WriteLine("Sample of identify faces in large person group."); IFaceClient client = new FaceClient(new ApiKeyServiceClientCredentials(key)) { Endpoint = endpoint }; string recognitionModel = RecognitionModel.Recognition02; const string ImageUrlPrefix = "https://csdx.blob.core.windows.net/resources/Face/Images/"; Dictionary <string, string[]> targetImageFileDictionary = new Dictionary <string, string[]> { { "Family1-Dad", new[] { "Family1-Dad1.jpg", "Family1-Dad2.jpg" } }, { "Family1-Mom", new[] { "Family1-Mom1.jpg", "Family1-Mom2.jpg" } }, { "Family1-Son", new[] { "Family1-Son1.jpg", "Family1-Son2.jpg" } }, { "Family1-Daughter", new[] { "Family1-Daughter1.jpg", "Family1-Daughter2.jpg" } }, { "Family2-Lady", new[] { "Family2-Lady1.jpg", "Family2-Lady2.jpg" } }, { "Family2-Man", new[] { "Family2-Man1.jpg", "Family2-Man2.jpg" } } }; string sourceImageFileName = "identification1.jpg"; // Create a large person group. string largePersonGroupId = Guid.NewGuid().ToString(); Console.WriteLine($"Create a large person group ({largePersonGroupId})."); await client.LargePersonGroup.CreateAsync(largePersonGroupId, largePersonGroupId, recognitionModel : recognitionModel); foreach (var targetImageFileDictionaryName in targetImageFileDictionary.Keys) { // Create a large person group person. Person person = new Person { Name = targetImageFileDictionaryName, UserData = "Person for sample" }; person.PersonId = (await client.LargePersonGroupPerson.CreateAsync(largePersonGroupId, person.Name)) .PersonId; Console.WriteLine($"Create a large person group person '{person.Name}'."); foreach (var targetImageFileName in targetImageFileDictionary[targetImageFileDictionaryName]) { // Add face to the large person group person. Console.WriteLine( $"Add face to the large person group person({targetImageFileDictionaryName}) from image `{targetImageFileName}`."); PersistedFace face = await client.LargePersonGroupPerson.AddFaceFromUrlAsync( largePersonGroupId, person.PersonId, $"{ImageUrlPrefix}{targetImageFileName}", targetImageFileName); if (face == null) { throw new Exception($"No persisted face from image `{targetImageFileName}`."); } } } // Start to train the large person group. Console.WriteLine($"Train large person group {largePersonGroupId}."); await client.LargePersonGroup.TrainAsync(largePersonGroupId); // Wait until the training is completed. while (true) { await Task.Delay(1000); var trainingStatus = await client.LargePersonGroup.GetTrainingStatusAsync(largePersonGroupId); Console.WriteLine($"Training status is {trainingStatus.Status}."); if (trainingStatus.Status != TrainingStatusType.Running) { if (trainingStatus.Status == TrainingStatusType.Failed) { throw new Exception($"Training failed with message {trainingStatus.Message}."); } break; } } List <Guid> sourceFaceIds = new List <Guid>(); // Detect faces from source image url. List <DetectedFace> detectedFaces = await Common.DetectFaces( client, $"{ImageUrlPrefix}{sourceImageFileName}", recognitionModel : recognitionModel); // Add detected faceIds to sourceFaceIds. foreach (var detectedFace in detectedFaces) { sourceFaceIds.Add(detectedFace.FaceId.Value); } // Identify example of identifying faces towards large person group. var identifyResults = await client.Face.IdentifyAsync(sourceFaceIds, null, largePersonGroupId); if (identifyResults == null) { Console.WriteLine( $"No person identified in the large person group for faces from the {sourceImageFileName}."); return; } foreach (var identifyResult in identifyResults) { Person person = await client.LargePersonGroupPerson.GetAsync( largePersonGroupId, identifyResult.Candidates[0].PersonId); Console.WriteLine( $"Person '{person.Name}' is identified for face: {sourceImageFileName} - {identifyResult.FaceId}, confidence: {identifyResult.Candidates[0].Confidence}."); } // Delete the person group. await client.LargePersonGroup.DeleteAsync(largePersonGroupId); Console.WriteLine($"Delete the large person group {largePersonGroupId}."); Console.WriteLine(); }