private async Task AddImageInsightsToViewModel(StorageFolder rootFolder, ImageInsights insights) { BitmapImage bitmapImage = new BitmapImage(); await bitmapImage.SetSourceAsync((await(await rootFolder.GetFileAsync(insights.ImageId)).OpenStreamForReadAsync()).AsRandomAccessStream()); bitmapImage.DecodePixelHeight = 360; ImageInsightsViewModel insightsViewModel = new ImageInsightsViewModel(insights, bitmapImage); this.AllResults.Add(insightsViewModel); this.FilteredResults.Add(insightsViewModel); foreach (var tag in insights.VisionInsights.Tags) { TagFilterViewModel tvm = this.TagFilters.FirstOrDefault(t => t.Tag == tag); if (tvm == null) { tvm = new TagFilterViewModel(tag); this.TagFilters.Add(tvm); } tvm.Count++; } foreach (var faceInsights in insights.FaceInsights) { FaceFilterViewModel fvm = this.FaceFilters.FirstOrDefault(f => f.FaceId == faceInsights.UniqueFaceId); if (fvm == null) { StorageFile file = (await rootFolder.GetFileAsync(insights.ImageId)); ImageSource croppedFaced = await Util.GetCroppedBitmapAsync( file.OpenStreamForReadAsync, new Rectangle { Height = faceInsights.FaceRectangle.Height, Width = faceInsights.FaceRectangle.Width, Left = faceInsights.FaceRectangle.Left, Top = faceInsights.FaceRectangle.Top }); fvm = new FaceFilterViewModel(faceInsights.UniqueFaceId, croppedFaced); this.FaceFilters.Add(fvm); } fvm.Count++; } var distinctEmotions = insights.FaceInsights.Select(f => f.TopEmotion).Distinct(); foreach (var emotion in distinctEmotions) { EmotionFilterViewModel evm = this.EmotionFilters.FirstOrDefault(f => f.Emotion == emotion); if (evm == null) { evm = new EmotionFilterViewModel(emotion); this.EmotionFilters.Add(evm); } evm.Count++; } }
public static async Task <ImageInsights> ProcessImageAsync(Func <Task <Stream> > imageStream, string imageId) { ImageAnalyzer analyzer = new ImageAnalyzer(imageStream); analyzer.ShowDialogOnFaceApiErrors = true; await Task.WhenAll(analyzer.AnalyzeImageAsync(detectCelebrities: false, visualFeatures: DefaultVisualFeatureTypes), analyzer.DetectFacesAsync(detectFaceAttributes: true), analyzer.DetectEmotionAsync()); await analyzer.FindSimilarPersistedFacesAsync(); ImageInsights result = new ImageInsights { ImageId = imageId }; result.VisionInsights = new VisionInsights { Caption = analyzer.AnalysisResult.Description?.Captions[0].Text, Tags = analyzer.AnalysisResult.Tags != null?analyzer.AnalysisResult.Tags.Select(t => t.Name).ToArray() : new string[0] }; List <FaceInsights> faceInsightsList = new List <FaceInsights>(); foreach (var face in analyzer.DetectedFaces) { FaceInsights faceInsights = new FaceInsights { FaceRectangle = face.FaceRectangle, Age = face.FaceAttributes.Age, Gender = face.FaceAttributes.Gender }; SimilarFaceMatch similarFaceMatch = analyzer.SimilarFaceMatches.FirstOrDefault(s => s.Face.FaceId == face.FaceId); if (similarFaceMatch != null) { faceInsights.UniqueFaceId = similarFaceMatch.SimilarPersistedFace.PersistedFaceId; } Emotion faceEmotion = CoreUtil.FindFaceClosestToRegion(analyzer.DetectedEmotion, face.FaceRectangle); if (faceEmotion != null) { faceInsights.TopEmotion = faceEmotion.Scores.ToRankedList().First().Key; } faceInsightsList.Add(faceInsights); } result.FaceInsights = faceInsightsList.ToArray(); return(result); }
public ImageInsightsViewModel(ImageInsights insights, ImageSource imageSource) { this.Insights = insights; this.ImageSource = imageSource; }
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; }