Example #1
0
        public async Task <ImageAnalysis> GetImageAnalysis(string imageUrl)
        {
            var analyzeImageModel = new AnalyzeImageModel();
            // Creating a list that defines the features to be extracted from the image.

            List <VisualFeatureTypes?> features = new List <VisualFeatureTypes?>()
            {
                VisualFeatureTypes.Categories, VisualFeatureTypes.Description,
                VisualFeatureTypes.Faces, VisualFeatureTypes.ImageType,
                VisualFeatureTypes.Tags, VisualFeatureTypes.Adult,
                VisualFeatureTypes.Color, VisualFeatureTypes.Brands,
                VisualFeatureTypes.Objects
            };
            ImageAnalysis results = await _computerVisionClient.AnalyzeImageAsync(imageUrl, features);

            return(results);
        }
Example #2
0
        public async Task <AnalyzeImageModel> GetImageAnalysisCustomModel(string imageUrl)
        {
            var analyzeImageModel = new AnalyzeImageModel();
            // Creating a list that defines the features to be extracted from the image.

            List <VisualFeatureTypes?> features = new List <VisualFeatureTypes?>()
            {
                VisualFeatureTypes.Categories, VisualFeatureTypes.Description,
                VisualFeatureTypes.Faces, VisualFeatureTypes.ImageType,
                VisualFeatureTypes.Tags, VisualFeatureTypes.Adult,
                VisualFeatureTypes.Color, VisualFeatureTypes.Brands,
                VisualFeatureTypes.Objects
            };
            ImageAnalysis results = await _computerVisionClient.AnalyzeImageAsync(imageUrl, features);

            // </snippet_analyze_call>

            // <snippet_describe>
            // Sunmarizes the image content.
            foreach (var caption in results.Description.Captions)
            {
                var item = new Summary()
                {
                    Name       = caption.Text,
                    Confidence = caption.Confidence
                };
                analyzeImageModel.Summary.Add(item);
            }
            // </snippet_describe>

            // <snippet_categorize>
            // Display categories the image is divided into.
            foreach (var category in results.Categories)
            {
                var item = new Category()
                {
                    Name       = category.Name,
                    Confidence = category.Score
                };
                analyzeImageModel.Categories.Add(item);
            }
            // </snippet_categorize>

            // <snippet_tags>
            // Image tags and their confidence score
            foreach (var tag in results.Tags)
            {
                var item = new Tag()
                {
                    Name       = tag.Name,
                    Confidence = tag.Confidence
                };
                analyzeImageModel.Tags.Add(item);
            }
            // </snippet_tags>

            // <snippet_objects>
            // Objects
            foreach (var obj in results.Objects)
            {
                var item = new ImageObject()
                {
                    Name       = obj.ObjectProperty,
                    Confidence = obj.Confidence,
                    LocationX1 = obj.Rectangle.X,
                    LocationX2 = obj.Rectangle.X + obj.Rectangle.W,
                    LocationY1 = obj.Rectangle.Y,
                    LocationY2 = obj.Rectangle.Y + obj.Rectangle.H
                };
                analyzeImageModel.Objects.Add(item);
            }
            // </snippet_objects>

            // <snippet_faces>
            // Faces
            foreach (var face in results.Faces)
            {
                var item = new Face()
                {
                    Gender     = face.Gender.HasValue ? face.Gender.Value.ToString() : string.Empty,
                    Age        = face.Age,
                    LocationX1 = face.FaceRectangle.Left,
                    LocationX2 = face.FaceRectangle.Left,
                    LocationY1 = face.FaceRectangle.Top + face.FaceRectangle.Width,
                    LocationY2 = face.FaceRectangle.Top + face.FaceRectangle.Height
                };
                analyzeImageModel.Faces.Add(item);
            }
            // </snippet_faces>

            // <snippet_adult>
            // Adult or racy content, if any.
            Console.WriteLine("Adult:");
            if (results.Adult != null)
            {
                var adult = new Adult()
                {
                    IsAdultContent = results.Adult.IsAdultContent,
                    AdultScore     = results.Adult.AdultScore,
                    IsRacyContent  = results.Adult.IsRacyContent,
                    RacyScore      = results.Adult.RacyScore
                };
                analyzeImageModel.Adult = adult;
            }
            // </snippet_adult>

            // <snippet_brands>
            // Well-known (or custom, if set) brands.
            foreach (var brand in results.Brands)
            {
                var item = new Brand()
                {
                    Name       = brand.Name,
                    Confidence = brand.Confidence,
                    LocationX1 = brand.Rectangle.X,
                    LocationX2 = brand.Rectangle.X + brand.Rectangle.W,
                    LocationY1 = brand.Rectangle.Y,
                    LocationY2 = brand.Rectangle.Y + brand.Rectangle.H
                };
                analyzeImageModel.Brands.Add(item);
            }
            // </snippet_brands>

            // <snippet_celebs>
            // Celebrities in image, if any.
            foreach (var category in results.Categories)
            {
                if (category.Detail?.Celebrities != null)
                {
                    foreach (var celeb in category.Detail.Celebrities)
                    {
                        var item = new Celebrity()
                        {
                            Name       = celeb.Name,
                            Confidence = celeb.Confidence,
                            LocationX1 = celeb.FaceRectangle.Left,
                            LocationX2 = celeb.FaceRectangle.Top,
                            LocationY1 = celeb.FaceRectangle.Height,
                            LocationY2 = celeb.FaceRectangle.Width
                        };
                        analyzeImageModel.Celebrities.Add(item);
                    }
                }
            }
            // </snippet_celebs>


            // <snippet_landmarks>
            // Popular landmarks in image, if any.
            foreach (var category in results.Categories)
            {
                if (category.Detail?.Landmarks != null)
                {
                    foreach (var landmark in category.Detail.Landmarks)
                    {
                        var item = new Landmark()
                        {
                            Name       = landmark.Name,
                            Confidence = landmark.Confidence,
                        };
                        analyzeImageModel.Landmarks.Add(item);
                    }
                }
            }
            // </snippet_landmarks>

            // <snippet_color>
            // Identifies the color scheme.
            if (results.Color != null)
            {
                var color = new ImageColor()
                {
                    IsBWImg                 = results.Color.IsBWImg,
                    AccentColor             = results.Color.AccentColor,
                    DominantColorBackground = results.Color.DominantColorBackground,
                    DominantColorForeground = results.Color.DominantColorForeground,
                    DominantColors          = string.Join(",", results.Color.DominantColors)
                };
                analyzeImageModel.Color = color;
            }
            // </snippet_color>

            // <snippet_type>
            // Detects the image types.
            if (results.ImageType != null)
            {
                var imagetype = new Model.ImageType()
                {
                    ClipArtType     = results.ImageType.ClipArtType,
                    LineDrawingType = results.ImageType.LineDrawingType
                };
                analyzeImageModel.ImageType = imagetype;
            }
            // </snippet_type>
            return(analyzeImageModel);
        }