Example #1
0
        private static async Task AnalyzeFacesAsync(Func <Task <Stream> > imageStreamCallback, ImageInsights result)
        {
            var faces = await FaceServiceHelper.DetectAsync(imageStreamCallback, returnFaceId : true, returnFaceLandmarks : false, returnFaceAttributes : DefaultFaceAttributeTypes);

            List <FaceInsights> faceInsightsList = new List <FaceInsights>();

            foreach (Face detectedFace in faces)
            {
                FaceInsights faceInsights = new FaceInsights
                {
                    FaceRectangle = detectedFace.FaceRectangle,
                    Age           = detectedFace.FaceAttributes.Age,
                    Gender        = detectedFace.FaceAttributes.Gender
                };

                SimilarPersistedFace similarPersistedFace = await FaceListManager.FindSimilarPersistedFaceAsync(imageStreamCallback, detectedFace.FaceId, detectedFace);

                if (similarPersistedFace != null)
                {
                    faceInsights.UniqueFaceId = similarPersistedFace.PersistedFaceId;
                }

                faceInsightsList.Add(faceInsights);
            }

            result.FaceInsights = faceInsightsList.ToArray();
        }
        private async Task ResetStateAsync()
        {
            this.peopleListView.Children.Clear();
            this.peopleInVideo.Clear();
            await FaceListManager.ResetFaceLists();

            this.demographics = new DemographicsData
            {
                AgeGenderDistribution = new AgeGenderDistribution {
                    FemaleDistribution = new AgeDistribution(), MaleDistribution = new AgeDistribution()
                },
                Visitors = new List <Visitor>()
            };

            this.tagsInVideo.Clear();
            this.tagFilterPanel.Children.Clear();
            this.tagsListView.Children.Clear();

            this.detectedObjectsInVideo.Clear();
            this.detectedObjectFilterPanel.Children.Clear();
            this.detectedObjectsListView.Children.Clear();
            this.detectedObjectsInFrame.Clear();

            this.pendingIdentificationAttemptCount.Clear();
            this.processedFrames.Clear();
        }
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            EnterKioskMode();
            SessionID                = Guid.NewGuid().ToString();
            Constants.isDashboard    = false;
            Constants.isFireDetected = false;
            if (string.IsNullOrEmpty(SettingsHelper.Instance.EmotionApiKey) || string.IsNullOrEmpty(SettingsHelper.Instance.FaceApiKey))
            {
                //await new MessageDialog("Missing Face or Emotion API Key. Please enter a key in the Settings page.", "Missing API Key").ShowAsync();
            }
            else
            {
                FaceListManager.FaceListsUserDataFilter = SettingsHelper.Instance.WorkspaceKey + "_RealTime";
                await FaceListManager.Initialize();

                //  await ResetDemographicsData();
                //  this.UpdateDemographicsUI();

                await this.cameraControl.StartStreamAsync(isForRealTimeProcessing : true);

                this.StartProcessingLoop();
            }

            base.OnNavigatedTo(e);
        }
Example #4
0
        protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            this.isProcessingLoopInProgress              = false;
            Window.Current.Activated                    -= CurrentWindowActivationStateChanged;
            this.cameraControl.CameraAspectRatioChanged -= CameraControl_CameraAspectRatioChanged;

            await FaceListManager.ResetFaceLists();

            await this.cameraControl.StopStreamAsync();

            base.OnNavigatingFrom(e);
        }
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            if (string.IsNullOrEmpty(SettingsHelper.Instance.FaceApiKey) ||
                string.IsNullOrEmpty(SettingsHelper.Instance.VisionApiKey))
            {
                await new MessageDialog("Missing Face or Vision API Key. Please enter a key in the Settings page.", "Missing API Key").ShowAsync();
            }

            FaceListManager.FaceListsUserDataFilter = SettingsHelper.Instance.WorkspaceKey + "_RealTimeFromVideo";
            await FaceListManager.Initialize();

            base.OnNavigatedTo(e);
        }
Example #6
0
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            EnterKioskMode();

            if (string.IsNullOrEmpty(SettingsHelper.Instance.EmotionApiKey) || string.IsNullOrEmpty(SettingsHelper.Instance.FaceApiKey))
            {
                await new MessageDialog("缺少臉部或情緒分析金鑰。請至設定頁面以完成輸入。", "缺乏金鑰").ShowAsync();
                log.WriteToFile(DateTime.Now.ToString("G", tw) + " Missing Face or Emotion API Key\n");
            }
            else if (appkey != "iplanet123")
            {
                await new MessageDialog("缺少產品金鑰或產品金鑰錯誤。請至設定頁面完成輸入或聯繫系統管理者。", "缺乏金鑰").ShowAsync();
                log.WriteToFile(DateTime.Now.ToString("G", tw) + " Missing Application Key\n");
            }
            else
            {
                FaceListManager.FaceListsUserDataFilter = SettingsHelper.Instance.WorkspaceKey + "_RealTime";
                await FaceListManager.Initialize();

                await ResetDemographicsData();

                this.UpdateDemographicsUI();

                await this.saveControl.StartStreamAsync(isForRealTimeProcessing : true);

                this.StartProcessingLoop();
            }
            if (!String.IsNullOrEmpty(delay))
            {
                if (!int.TryParse(delay.Trim(), out delay_int))
                {
                    await new MessageDialog("延遲秒數需要是整數。請至設定頁面修正。", "延遲秒數錯誤").ShowAsync();
                    log.WriteToFile(DateTime.Now.ToString("G", tw) + " Delay Second Parse Error\n");
                }
                else
                {
                    delay_int *= 1000;
                }
            }
            else
            {
                delay_int = 5000;
            }
            base.OnNavigatedTo(e);
        }
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (string.IsNullOrEmpty(SettingsHelper.Instance.FaceApiKey) ||
                string.IsNullOrEmpty(SettingsHelper.Instance.VisionApiKey))
            {
                await new MessageDialog("Missing Face or Vision API Key. Please enter a key in the Settings page.", "Missing API Key").ShowAsync();
            }

            FaceListManager.FaceListsUserDataFilter = "DigitalAssetManagement";
            await FaceListManager.Initialize();

            //load files
            await FileManager.LoadFilesAsync();

            //setup Custom Vision
            await InitCustomVision();

            base.OnNavigatedTo(e);
        }
        private async Task ResetDemographicsData()
        {
            this.initializingUI.Visibility         = Visibility.Visible;
            this.initializingProgressRing.IsActive = true;

            this.demographics = new DemographicsData
            {
                StartTime             = DateTime.Now,
                AgeGenderDistribution = new AgeGenderDistribution {
                    FemaleDistribution = new AgeDistribution(), MaleDistribution = new AgeDistribution()
                },
                Visitors = new List <Visitor>()
            };

            this.visitors.Clear();
            await FaceListManager.ResetFaceLists();

            this.initializingUI.Visibility         = Visibility.Collapsed;
            this.initializingProgressRing.IsActive = false;
        }
Example #9
0
        protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            this.isProcessingLoopInProgress              = false;
            Window.Current.Activated                    -= CurrentWindowActivationStateChanged;
            this.cameraControl.CameraAspectRatioChanged -= CameraControl_CameraAspectRatioChanged;

            if (SettingsHelper.Instance.DriverMonitoringSleepingThreshold != this.SleepingApertureThreshold)
            {
                SettingsHelper.Instance.DriverMonitoringSleepingThreshold = this.SleepingApertureThreshold;
            }

            if (SettingsHelper.Instance.DriverMonitoringYawningThreshold != this.YawningApertureThreshold)
            {
                SettingsHelper.Instance.DriverMonitoringYawningThreshold = this.YawningApertureThreshold;
            }

            await FaceListManager.ResetFaceLists();

            await this.cameraControl.StopStreamAsync();

            base.OnNavigatingFrom(e);
        }
Example #10
0
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            EnterKioskMode();

            if (string.IsNullOrEmpty(SettingsHelper.Instance.EmotionApiKey) || string.IsNullOrEmpty(SettingsHelper.Instance.FaceApiKey))
            {
                await new MessageDialog("Missing Face or Emotion API Key. Please enter a key in the Settings page.", "Missing API Key").ShowAsync();
            }
            else
            {
                await FaceListManager.Initialize();

                await ResetDemographicsData();

                this.UpdateDemographicsUI();

                await this.cameraControl.StartStreamAsync(isForRealTimeProcessing : true);

                this.StartProcessingLoop();
            }

            base.OnNavigatedTo(e);
        }
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            EnterKioskMode();

            if (string.IsNullOrEmpty(SettingsHelper.Instance.EmotionApiKey) || string.IsNullOrEmpty(SettingsHelper.Instance.FaceApiKey))
            {
                await new MessageDialog("Missing Face or Emotion API Key. Please enter a key in the Settings page.", "Missing API Key").ShowAsync();
            }
            else
            {
                FaceListManager.FaceListsUserDataFilter = SettingsHelper.Instance.WorkspaceKey + "_RealTime";
                await FaceListManager.Initialize();

                await ResetDemographicsData();

                this.UpdateDemographicsUI();

                await this.cameraControl.StartStreamAsync(isForRealTimeProcessing : true);

                this.StartProcessingLoop();
            }

            //get a reference to SenseHat
            //try
            //{
            //    senseHat = await SenseHatFactory.GetSenseHat();

            //    senseHat.Display.Clear();
            //    senseHat.Display.Update();

            //}
            //catch (Exception)
            //{
            //}

            base.OnNavigatedTo(e);
        }
        protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            await FaceListManager.ResetFaceLists();

            base.OnNavigatingFrom(e);
        }
        private async Task ProcessImagesAsync(StorageFolder rootFolder, bool forceProcessing = false)
        {
            this.progressRing.IsActive = true;

            this.landingMessage.Visibility       = Visibility.Collapsed;
            this.filterTab.Visibility            = Visibility.Visible;
            this.reprocessImagesButton.IsEnabled = true;

            this.FilteredResults.Clear();
            this.AllResults.Clear();
            this.TagFilters.Clear();
            this.EmotionFilters.Clear();
            this.FaceFilters.Clear();

            List <ImageInsights> insightsList = new List <ImageInsights>();

            if (!forceProcessing)
            {
                // see if we have pre-computed results and if so load it from the json file
                try
                {
                    StorageFile insightsResultFile = (await rootFolder.TryGetItemAsync("ImageInsights.json")) as StorageFile;
                    if (insightsResultFile != null)
                    {
                        using (StreamReader reader = new StreamReader(await insightsResultFile.OpenStreamForReadAsync()))
                        {
                            insightsList = JsonConvert.DeserializeObject <List <ImageInsights> >(await reader.ReadToEndAsync());
                            foreach (var insights in insightsList)
                            {
                                await AddImageInsightsToViewModel(rootFolder, insights);
                            }
                        }
                    }
                }
                catch
                {
                    // We will just compute everything again in case of errors
                }
            }

            if (!insightsList.Any())
            {
                // start with fresh face lists
                await FaceListManager.ResetFaceLists();

                // enumerate through the images and extract the insights
                QueryOptions           fileQueryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, new[] { ".png", ".jpg", ".bmp", ".jpeg", ".gif" });
                StorageFileQueryResult queryResult      = rootFolder.CreateFileQueryWithOptions(fileQueryOptions);
                var queryFileList = this.limitProcessingToggleButton.IsChecked.Value ? await queryResult.GetFilesAsync(0, 50) : await queryResult.GetFilesAsync();

                foreach (var item in queryFileList)
                {
                    // Resize (if needed) in order to reduce network latency. Then store the result in a temporary file.
                    StorageFile resizedFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("ImageCollectionInsights.jpg", CreationCollisionOption.GenerateUniqueName);

                    var resizeTransform = await Util.ResizePhoto(await item.OpenStreamForReadAsync(), 720, resizedFile);

                    // Send the file for processing
                    ImageInsights insights = await ImageProcessor.ProcessImageAsync(resizedFile.OpenStreamForReadAsync, item.Name);

                    // Delete resized file
                    await resizedFile.DeleteAsync();

                    // Adjust all FaceInsights coordinates based on the transform function between the original and resized photos
                    foreach (var faceInsight in insights.FaceInsights)
                    {
                        faceInsight.FaceRectangle.Left   = (int)(faceInsight.FaceRectangle.Left * resizeTransform.Item1);
                        faceInsight.FaceRectangle.Top    = (int)(faceInsight.FaceRectangle.Top * resizeTransform.Item2);
                        faceInsight.FaceRectangle.Width  = (int)(faceInsight.FaceRectangle.Width * resizeTransform.Item1);
                        faceInsight.FaceRectangle.Height = (int)(faceInsight.FaceRectangle.Height * resizeTransform.Item2);
                    }

                    insightsList.Add(insights);
                    await AddImageInsightsToViewModel(rootFolder, insights);
                }

                // save to json
                StorageFile jsonFile = await rootFolder.CreateFileAsync("ImageInsights.json", CreationCollisionOption.ReplaceExisting);

                using (StreamWriter writer = new StreamWriter(await jsonFile.OpenStreamForWriteAsync()))
                {
                    string jsonStr = JsonConvert.SerializeObject(insightsList, Formatting.Indented);
                    await writer.WriteAsync(jsonStr);
                }
            }

            List <TagFilterViewModel> tagsGroupedByCountAndSorted = new List <TagFilterViewModel>();

            foreach (var group in this.TagFilters.GroupBy(t => t.Count).OrderByDescending(g => g.Key))
            {
                tagsGroupedByCountAndSorted.AddRange(group.OrderBy(t => t.Tag));
            }
            this.TagFilters.Clear();
            this.TagFilters.AddRange(tagsGroupedByCountAndSorted);

            var sortedEmotions = this.EmotionFilters.OrderByDescending(e => e.Count).ToArray();

            this.EmotionFilters.Clear();
            this.EmotionFilters.AddRange(sortedEmotions);

            var sortedFaces = this.FaceFilters.OrderByDescending(f => f.Count).ToArray();

            this.FaceFilters.Clear();
            this.FaceFilters.AddRange(sortedFaces);

            this.progressRing.IsActive = false;
        }
Example #14
0
 private async void DeleteSimilarFacesButton_Click(object sender, RoutedEventArgs e)
 {
     await FaceListManager.ResetFaceLists();
 }
Example #15
0
        private async Task ProcessImagesAsync(StorageFolder rootFolder)
        {
            this.progressRing.IsActive = true;

            this.AllResults.Clear();
            this.FilteredResults.Clear();
            this.TagFilters.Clear();
            this.EmotionFilters.Clear();
            this.FaceFilters.Clear();

            List <ImageInsights> insightsList = new List <ImageInsights>();

            // see if we have pre-computed results and if so load it from the json file
            try
            {
                StorageFile insightsResultFile = (await rootFolder.TryGetItemAsync("ImageInsights.json")) as StorageFile;
                if (insightsResultFile != null)
                {
                    using (StreamReader reader = new StreamReader(await insightsResultFile.OpenStreamForReadAsync()))
                    {
                        insightsList = JsonConvert.DeserializeObject <List <ImageInsights> >(await reader.ReadToEndAsync());
                        foreach (var insights in insightsList)
                        {
                            await AddImageInsightsToViewModel(rootFolder, insights);
                        }
                    }
                }
            }
            catch
            {
                // We will just compute everything again in case of errors
            }

            if (!insightsList.Any())
            {
                // start with fresh face lists
                await FaceListManager.ResetFaceLists();

                // enumerate through the first 50 images and extract the insights
                QueryOptions           fileQueryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, new[] { ".png", ".jpg", ".bmp", ".jpeg", ".gif" });
                StorageFileQueryResult queryResult      = rootFolder.CreateFileQueryWithOptions(fileQueryOptions);
                foreach (var item in (await queryResult.GetFilesAsync(0, 50)))
                {
                    try
                    {
                        // Resize (if needed) in order to reduce network latency and errors due to large files. Then store the result in a temporary file.
                        StorageFile resizedFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("ImageCollectionInsights.jpg", CreationCollisionOption.GenerateUniqueName);

                        var resizeTransform = await Util.ResizePhoto(await item.OpenStreamForReadAsync(), 720, resizedFile);

                        // Send the file for processing
                        ImageInsights insights = await ImageProcessor.ProcessImageAsync(resizedFile.OpenStreamForReadAsync, item.Name);

                        // Delete resized file
                        await resizedFile.DeleteAsync();

                        // Adjust all FaceInsights coordinates based on the transform function between the original and resized photos
                        foreach (var faceInsight in insights.FaceInsights)
                        {
                            faceInsight.FaceRectangle.Left   = (int)(faceInsight.FaceRectangle.Left * resizeTransform.Item1);
                            faceInsight.FaceRectangle.Top    = (int)(faceInsight.FaceRectangle.Top * resizeTransform.Item2);
                            faceInsight.FaceRectangle.Width  = (int)(faceInsight.FaceRectangle.Width * resizeTransform.Item1);
                            faceInsight.FaceRectangle.Height = (int)(faceInsight.FaceRectangle.Height * resizeTransform.Item2);
                        }

                        insightsList.Add(insights);
                        await AddImageInsightsToViewModel(rootFolder, insights);
                    }
                    catch (Exception ex)
                    {
                        await Util.GenericApiCallExceptionHandler(ex, "Error processing image.");
                    }
                }

                // save to json
                StorageFile jsonFile = await rootFolder.CreateFileAsync("ImageInsights.json", CreationCollisionOption.ReplaceExisting);

                using (StreamWriter writer = new StreamWriter(await jsonFile.OpenStreamForWriteAsync()))
                {
                    string jsonStr = JsonConvert.SerializeObject(insightsList, Formatting.Indented);
                    await writer.WriteAsync(jsonStr);
                }
            }

            var sortedTags = this.TagFilters.OrderBy(t => t.Tag).ToArray();

            this.TagFilters.Clear();
            this.TagFilters.AddRange(sortedTags);

            var sortedEmotions = this.EmotionFilters.OrderBy(t => t.Emotion).ToArray();

            this.EmotionFilters.Clear();
            this.EmotionFilters.AddRange(sortedEmotions);

            this.progressRing.IsActive = false;
        }
Example #16
0
        private async Task ProcessImagesAsync(StorageFolder rootFolder, bool forceProcessing = false)
        {
            this.progressRing.IsActive = true;

            this.landingMessage.Visibility       = Visibility.Collapsed;
            this.filterTab.Visibility            = Visibility.Visible;
            this.reprocessImagesButton.IsEnabled = true;

            this.FilteredResults.Clear();
            this.AllResults.Clear();
            this.TagFilters.Clear();
            this.EmotionFilters.Clear();
            this.FaceFilters.Clear();

            List <ImageInsights> insightsList = new List <ImageInsights>();

            if (!forceProcessing)
            {
                try
                {
                    StorageFile insightsResultFile = (await rootFolder.TryGetItemAsync("ImageInsights.json")) as StorageFile;
                    if (insightsResultFile != null)
                    {
                        using (StreamReader reader = new StreamReader(await insightsResultFile.OpenStreamForReadAsync()))
                        {
                            insightsList = JsonConvert.DeserializeObject <List <ImageInsights> >(await reader.ReadToEndAsync());
                            foreach (var insights in insightsList)
                            {
                                await AddImageInsightsToViewModel(rootFolder, insights);
                            }
                        }
                    }
                }
                catch
                {
                }
            }

            if (!insightsList.Any())
            {
                await FaceListManager.ResetFaceLists();

                QueryOptions           fileQueryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, new[] { ".png", ".jpg", ".bmp", ".jpeg", ".gif" });
                StorageFileQueryResult queryResult      = rootFolder.CreateFileQueryWithOptions(fileQueryOptions);
                var queryFileList = this.limitProcessingToggleButton.IsChecked.Value ? await queryResult.GetFilesAsync(0, 50) : await queryResult.GetFilesAsync();

                foreach (var item in queryFileList)
                {
                    StorageFile resizedFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("ImageCollectionInsights.jpg", CreationCollisionOption.GenerateUniqueName);

                    var resizeTransform = await Util.ResizePhoto(await item.OpenStreamForReadAsync(), 720, resizedFile);

                    ImageInsights insights = await ImageProcessor.ProcessImageAsync(resizedFile.OpenStreamForReadAsync, item.Name);

                    await resizedFile.DeleteAsync();

                    foreach (var faceInsight in insights.FaceInsights)
                    {
                        faceInsight.FaceRectangle.Left   = (int)(faceInsight.FaceRectangle.Left * resizeTransform.Item1);
                        faceInsight.FaceRectangle.Top    = (int)(faceInsight.FaceRectangle.Top * resizeTransform.Item2);
                        faceInsight.FaceRectangle.Width  = (int)(faceInsight.FaceRectangle.Width * resizeTransform.Item1);
                        faceInsight.FaceRectangle.Height = (int)(faceInsight.FaceRectangle.Height * resizeTransform.Item2);
                    }

                    insightsList.Add(insights);
                    await AddImageInsightsToViewModel(rootFolder, insights);
                }

                StorageFile jsonFile = await rootFolder.CreateFileAsync("ImageInsights.json", CreationCollisionOption.ReplaceExisting);

                using (StreamWriter writer = new StreamWriter(await jsonFile.OpenStreamForWriteAsync()))
                {
                    string jsonStr = JsonConvert.SerializeObject(insightsList, Formatting.Indented);
                    await writer.WriteAsync(jsonStr);
                }
            }

            List <TagFilterViewModel> tagsGroupedByCountAndSorted = new List <TagFilterViewModel>();

            foreach (var group in this.TagFilters.GroupBy(t => t.Count).OrderByDescending(g => g.Key))
            {
                tagsGroupedByCountAndSorted.AddRange(group.OrderBy(t => t.Tag));
            }
            this.TagFilters.Clear();
            this.TagFilters.AddRange(tagsGroupedByCountAndSorted);

            var sortedEmotions = this.EmotionFilters.OrderByDescending(e => e.Count).ToArray();

            this.EmotionFilters.Clear();
            this.EmotionFilters.AddRange(sortedEmotions);

            var sortedFaces = this.FaceFilters.OrderByDescending(f => f.Count).ToArray();

            this.FaceFilters.Clear();
            this.FaceFilters.AddRange(sortedFaces);

            this.progressRing.IsActive = false;
        }