Esempio n. 1
0
        public async Task AddImage(ImageInsights insights)
        {
            // Load image from file
            BitmapImage bitmapImage = new BitmapImage();

            if (insights.ImageUri.IsFile)
            {
                await bitmapImage.SetSourceAsync((await(await StorageFile.GetFileFromPathAsync(insights.ImageUri.AbsoluteUri)).OpenStreamForReadAsync()).AsRandomAccessStream());
            }
            else
            {
                bitmapImage.UriSource = insights.ImageUri;
            }

            //load smaller image - for performace
            bitmapImage.DecodePixelHeight = 270;

            // Create the view models
            ImageInsightsViewModel insightsViewModel = new ImageInsightsViewModel()
            {
                Insights = insights, ImageSource = bitmapImage
            };

            //tags
            foreach (var entity in insights.VisionInsights?.Tags ?? Array.Empty <string>())
            {
                AddFilter(TagFilters, entity, entity, insightsViewModel);
            }

            //faces
            foreach (var entity in insights.FaceInsights ?? Array.Empty <FaceInsights>())
            {
                var key        = entity.UniqueFaceId == Guid.Empty ? Guid.NewGuid() : entity.UniqueFaceId;
                var imageScale = bitmapImage.PixelHeight < bitmapImage.DecodePixelHeight ? 1d : (double)bitmapImage.DecodePixelHeight / (double)bitmapImage.PixelHeight;
                var filter     = AddFilter(FaceFilters, entity, key, insightsViewModel, bitmapImage, entity.FaceRectangle.ToRect().Scale(imageScale).Inflate(2));

                //rescale face rect if image has been rescaled
                if (filter.Count == 1 && bitmapImage.PixelHeight == 0)
                {
                    bitmapImage.ImageOpened += (sender, e) =>
                    {
                        var bitmap = sender as BitmapImage;
                        if (bitmap.DecodePixelHeight != 0 && bitmap.DecodePixelHeight < bitmap.PixelHeight)
                        {
                            var imageFilter = filter as ImageFilterViewModel;
                            imageFilter.ImageCrop = entity.FaceRectangle.ToRect().Scale((double)bitmap.DecodePixelHeight / (double)bitmap.PixelHeight).Inflate(2);
                        }
                    };
                }
            }

            //emotions
            insightsViewModel.Emotions = insights.FaceInsights?.Select(i => Util.EmotionToRankedList(i.FaceAttributes.Emotion).First().Key).Distinct().ToArray() ?? Array.Empty <string>();
            foreach (var entity in insightsViewModel.Emotions)
            {
                AddFilter(EmotionFilters, entity, entity, insightsViewModel);
            }

            //objects
            foreach (var entity in insights.VisionInsights?.Objects ?? Array.Empty <string>())
            {
                AddFilter(ObjectFilters, entity, entity, insightsViewModel);
            }

            //landmarks
            foreach (var entity in insights.VisionInsights?.Landmarks ?? Array.Empty <string>())
            {
                AddFilter(LandmarkFilters, entity, entity, insightsViewModel);
            }

            //celebrities
            foreach (var entity in insights.VisionInsights?.Celebrities ?? Array.Empty <string>())
            {
                AddFilter(CelebrityFilters, entity, entity, insightsViewModel);
            }

            //brands
            foreach (var entity in insights.VisionInsights?.Brands ?? Array.Empty <string>())
            {
                AddFilter(BrandFilters, entity, entity, insightsViewModel);
            }

            //moderation
            insightsViewModel.Moderation = GetAdultFlags(insights.VisionInsights?.Adult).ToArray();
            foreach (var entity in insightsViewModel.Moderation)
            {
                AddFilter(ModerationFilters, entity, entity, insightsViewModel);
                insightsViewModel.BlurImage = true; //set blur flag
            }

            //words
            foreach (var entity in insights.VisionInsights?.Words ?? Array.Empty <string>())
            {
                AddFilter(WordFilters, entity, entity, insightsViewModel);
            }

            //color
            insightsViewModel.Color = GetColorFlags(insights.VisionInsights?.Color).ToArray();
            foreach (var entity in insightsViewModel.Color)
            {
                AddFilter(ColorFilters, entity, entity, insightsViewModel);
            }

            //orientation
            insightsViewModel.Orientation = GetOrientation(insights.VisionInsights?.Metadata);
            if (insightsViewModel.Orientation != null)
            {
                AddFilter(OrientationFilters, insightsViewModel.Orientation, insightsViewModel.Orientation, insightsViewModel);
            }

            //image type
            insightsViewModel.ImageType = GetImageTypeFlags(insights.VisionInsights?.ImageType).ToArray();
            foreach (var entity in insightsViewModel.ImageType)
            {
                AddFilter(ImageTypeFilters, entity, entity, insightsViewModel);
            }

            //size
            insightsViewModel.Size = GetSize(insights.VisionInsights?.Metadata);
            if (insightsViewModel.Size != null)
            {
                AddFilter(SizeFilters, insightsViewModel.Size, insightsViewModel.Size, insightsViewModel);
            }

            //People
            insightsViewModel.People = GetPeopleFlags(insights.VisionInsights?.Objects, insights.FaceInsights).ToArray();
            foreach (var entity in insightsViewModel.People)
            {
                AddFilter(PeopleFilters, entity, entity, insightsViewModel);
            }

            //face attributes
            insightsViewModel.FaceAttributes = GetFaceAttributesFlags(insights.FaceInsights).ToArray();
            foreach (var entity in insightsViewModel.FaceAttributes)
            {
                AddFilter(FaceAttributesFilters, entity, entity, insightsViewModel);
            }

            //face qualtity
            insightsViewModel.FaceQualtity = GetFaceQualityFlags(insights.FaceInsights).ToArray();
            foreach (var entity in insightsViewModel.FaceQualtity)
            {
                AddFilter(FaceQualityFilters, entity, entity, insightsViewModel);
            }

            //Custom Vision tags
            insightsViewModel.CustomVisionTags = GetCustomVisionTags(insights.CustomVisionInsights).ToArray();
            foreach (var entity in insightsViewModel.CustomVisionTags)
            {
                AddFilter(CustomVisionTagFilters, entity, entity, insightsViewModel);
            }

            //Custom Vision objects
            insightsViewModel.CustomVisionObjects = GetCustomVisionObjects(insights.CustomVisionInsights).ToArray();
            foreach (var entity in insightsViewModel.CustomVisionObjects)
            {
                AddFilter(CustomVisionObjectFilters, entity, entity, insightsViewModel);
            }

            if (SettingsHelper.Instance.ShowAgeAndGender) //only if age and gender is allowed
            {
                //Age
                insightsViewModel.Age = GetAgeFlags(insights.FaceInsights).ToArray();
                foreach (var entity in insightsViewModel.Age)
                {
                    AddFilter(AgeFilters, entity, entity, insightsViewModel);
                }

                //Gender
                insightsViewModel.Gender = GetGenderFlags(insights.FaceInsights).ToArray();
                foreach (var entity in insightsViewModel.Gender)
                {
                    AddFilter(GenderFilters, entity, entity, insightsViewModel);
                }
            }

            //add viewmodel to collection
            _allResults.Add(insightsViewModel);
            FilteredResults.Add(insightsViewModel);
        }