public async Task SignIn() { try { // 1. Try to identify the person await customerFace.DetectFacesAsync(); await customerFace.IdentifyFacesAsync(); // TODO: consider looping over identified persons to find matching DB record customerInfo = new CustomerRegistrationInfo(); customerInfo.CustomerFaceHash = customerFace.IdentifiedPersons.First().Person.PersonId.ToString(); customerInfo.CustomerName = customerFace.IdentifiedPersons.First().Person.Name; // 2. Try to obtain customer info from DB CustomerInfo dbCustomerInfo = IgniteDataAccess.GetCustomerInfo(customerInfo.CustomerFaceHash); customerInfo.RegistrationDate = dbCustomerInfo.PreviousVisitDate; // 3. Update status StatusText = $"Welcome, {customerInfo.CustomerName}!"; StatusTextColor = Util.ToBrush("green"); PauseCamera?.Invoke(this, EventArgs.Empty); } catch (Exception) { customerInfo = null; StatusText = "Please try again."; StatusTextColor = Util.ToBrush("red"); ResumeCamera?.Invoke(this, EventArgs.Empty); } }
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); } }