private async Task AddTrainingImages(IEnumerable <ImageAnalyzer> args, bool dismissImageCollectorFlyout = true)
        {
            this.progressControl.IsActive = true;

            if (dismissImageCollectorFlyout)
            {
                this.trainingImageCollectorFlyout.Hide();
            }

            bool      foundError = false;
            Exception lastError  = null;

            foreach (var item in args)
            {
                try
                {
                    PersistedFace addResult;
                    if (item.GetImageStreamCallback != null)
                    {
                        addResult = await FaceServiceHelper.AddPersonFaceFromStreamAsync(
                            this.CurrentPersonGroup.PersonGroupId,
                            this.SelectedPerson.PersonId,
                            imageStreamCallback : item.GetImageStreamCallback,
                            userData : item.LocalImagePath,
                            targetFaceRect : null);
                    }
                    else
                    {
                        addResult = await FaceServiceHelper.AddPersonFaceFromUrlAsync(
                            this.CurrentPersonGroup.PersonGroupId,
                            this.SelectedPerson.PersonId,
                            imageUrl : item.ImageUrl,
                            userData : item.ImageUrl,
                            targetFaceRect : null);
                    }

                    if (addResult != null)
                    {
                        this.SelectedPersonFaces.Add(new PersistedFace {
                            PersistedFaceId = addResult.PersistedFaceId, UserData = item.GetImageStreamCallback != null ? item.LocalImagePath : item.ImageUrl
                        });
                        this.needsTraining = true;
                    }
                }
                catch (Exception e)
                {
                    foundError = true;
                    lastError  = e;
                }
            }

            if (foundError)
            {
                await Util.GenericApiCallExceptionHandler(lastError, "Failure adding one or more of the faces");
            }

            this.progressControl.IsActive = false;
        }
        private async Task ImportFromFolderAndFilesAsync(StorageFolder autoTrainFolder)
        {
            this.commandBar.IsOpen = false;

            this.progressControl.IsActive = true;

            List <string> errors = new List <string>();

            try
            {
                foreach (var folder in await autoTrainFolder.GetFoldersAsync())
                {
                    string personName = Util.CapitalizeString(folder.Name.Trim());
                    if (string.IsNullOrEmpty(personName) || this.PersonsInCurrentGroup.Any(p => p.Name == personName))
                    {
                        continue;
                    }

                    Person newPerson = await FaceServiceHelper.CreatePersonAsync(this.CurrentPersonGroup.PersonGroupId, personName);

                    foreach (var photoFile in await folder.GetFilesAsync())
                    {
                        try
                        {
                            await FaceServiceHelper.AddPersonFaceFromStreamAsync(
                                this.CurrentPersonGroup.PersonGroupId,
                                newPerson.PersonId,
                                imageStreamCallback : photoFile.OpenStreamForReadAsync,
                                userData : photoFile.Path,
                                targetFaceRect : null);

                            // Force a delay to reduce the chance of hitting API call rate limits
                            await Task.Delay(250);
                        }
                        catch (Exception)
                        {
                            errors.Add(photoFile.Path);
                        }
                    }

                    this.needsTraining = true;

                    this.PersonsInCurrentGroup.Add(newPerson);
                }
            }
            catch (Exception ex)
            {
                await Util.GenericApiCallExceptionHandler(ex, "Failure processing the folder and files");
            }

            if (errors.Any())
            {
                await new MessageDialog(string.Join("\n", errors), "Failure importing the folllowing photos").ShowAsync();
            }

            this.progressControl.IsActive = false;
        }
Exemplo n.º 3
0
        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);
            }
        }
Exemplo n.º 4
0
        private async void cameraControl_ImageCaptured(object sender, ImageAnalyzer e)
        {
            loadText.Text       = "Detecting faces...";
            loadGrid.Visibility = Visibility.Visible;

            // Detects and identify if there is any faces on the image.
            await e.DetectFacesAsync();

            if (!e.DetectedFaces.Any())
            {
                ContentDialog contentDialog = new ContentDialog
                {
                    Title           = "No faces detected",
                    Content         = "There are no faces detected on the image. Please stand in front of the camera and make sure that a box is drawn around your face and select the capture button.",
                    CloseButtonText = "Ok"
                };

                await contentDialog.ShowAsync();

                return;
            }

            await e.IdentifyFacesAsync();

            if (e.IdentifiedPersons.Any())
            {
                string identifiedUsername = e.IdentifiedPersons.First().Person.Name;
                if (identifiedUsername != SettingsHelper.Instance.SelectedPerson.Name)
                {
                    ContentDialog contentDialog = new ContentDialog
                    {
                        Title           = "Another person is detected in the photo",
                        Content         = "Another person is identified in the submitted photo. Make sure that you are registering your face for one account only or make sure that there is only one person captured on the image. If you believe that there is an error, please contact Chiefs or HR Functional Unit to help you register for facial recognition.",
                        CloseButtonText = "Ok"
                    };

                    await contentDialog.ShowAsync();

                    return;
                }
            }

            // Crop the primary face
            FaceRectangle rect = e.DetectedFaces.First().FaceRectangle;
            double        heightScaleFactor = 1.8;
            double        widthScaleFactor  = 1.8;
            FaceRectangle biggerRectangle   = new FaceRectangle
            {
                Height = Math.Min((int)(rect.Height * heightScaleFactor), e.DecodedImageHeight),
                Width  = Math.Min((int)(rect.Width * widthScaleFactor), e.DecodedImageWidth)
            };

            biggerRectangle.Left = Math.Max(0, rect.Left - (int)(rect.Width * ((widthScaleFactor - 1) / 2)));
            biggerRectangle.Top  = Math.Max(0, rect.Top - (int)(rect.Height * ((heightScaleFactor - 1) / 1.4)));

            StorageFile tempFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(
                "FaceRecoCameraCapture.jpg",
                CreationCollisionOption.GenerateUniqueName);

            await Util.CropBitmapAsync(e.GetImageStreamCallback, biggerRectangle, tempFile);

            var croppedImage = new ImageAnalyzer(tempFile.OpenStreamForReadAsync, tempFile.Path);

            loadText.Text = "Uploading your image...";

            try
            {
                PersistedFace addResult;

                addResult = await FaceServiceHelper.AddPersonFaceFromStreamAsync(
                    helper.CurrentPersonGroup.PersonGroupId,
                    helper.SelectedPerson.PersonId,
                    croppedImage.GetImageStreamCallback,
                    croppedImage.LocalImagePath, null);


                if (addResult != null)
                {
                    // Trains each PersonGroup
                    loadText.Text     = "Training model...";
                    IsTrainingSuccess = true;
                    foreach (var group in helper.PersonGroups)
                    {
                        await FaceServiceHelper.TrainPersonGroupAsync(group.PersonGroupId);

                        while (true)
                        {
                            TrainingStatus trainingStatus = await FaceServiceHelper.GetPersonGroupTrainingStatusAsync(group.PersonGroupId);

                            if (trainingStatus.Status != TrainingStatusType.Running)
                            {
                                if (trainingStatus.Status == TrainingStatusType.Failed)
                                {
                                    IsTrainingSuccess = false;
                                }

                                break;
                            }

                            await Task.Delay(500);
                        }
                    }

                    if (IsTrainingSuccess == true)
                    {
                        ContentDialog contentDialog = new ContentDialog
                        {
                            Title           = "Face registered",
                            Content         = "Your face has been successfully registered. You can simply stand in front of the camera the next time you login to Payroll.",
                            CloseButtonText = "Ok"
                        };

                        await contentDialog.ShowAsync();
                    }
                    else
                    {
                        ContentDialog contentDialog = new ContentDialog
                        {
                            Title           = "Training failed.",
                            Content         = "Training failed. Please try again later. If the problem persists, please contact Chiefs or HR Functional Unit.",
                            CloseButtonText = "Ok"
                        };

                        await contentDialog.ShowAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                ContentDialog contentDialog = new ContentDialog
                {
                    Title               = "Unable to register your face",
                    Content             = "There is a problem that prevents us to register your face. Please try again later. You can register you face by selecting Improve Recognition after you login into Payroll. Tap on the More info button to see what's wrong.",
                    PrimaryButtonText   = "More info",
                    SecondaryButtonText = "Ok"
                };

                ContentDialogResult result = await contentDialog.ShowAsync();

                if (result == ContentDialogResult.Primary)
                {
                    contentDialog = new ContentDialog
                    {
                        Title           = "More info",
                        Content         = ex.Message,
                        CloseButtonText = "Close"
                    };

                    await contentDialog.ShowAsync();
                }
            }
            finally
            {
                NavigateBack();
            }
        }
        public async Task <PersistedFace> AddVisitorPhotoAsync(string groupId, Guid cognitivePersonId, string photoUrl, FaceRectangle faceRect)
        {
            var persistedFace = await FaceServiceHelper.AddPersonFaceFromStreamAsync(groupId, cognitivePersonId, GetPhotoStream, photoUrl, faceRect);

            return(persistedFace);
        }