Exemplo n.º 1
0
        /// <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!");
        }