Example #1
0
        static Bitmap SmartCropImage(Bitmap sourceImage)
        {
            VisionServiceClient VisionServiceClient = ComputerVisionService.GetClient();

            using (MemoryStream memoryStream = new MemoryStream())
            {
                sourceImage.SaveAsPng(memoryStream);
                memoryStream.Position = 0;

                int width  = sourceImage.Width;
                int height = (int)(sourceImage.Width / 16.0f * 9.0f * 1.2f); // Set it to a 16:9 with an extra 20% to increase the overall size
                Console.WriteLine("Original Width: " + sourceImage.Width + " Original Height: " + sourceImage.Height + " Cropped Height: " + height);

                if (sourceImage.Height > height)
                {
                    Console.WriteLine("Calling VisionServiceClient.GetThumbnailAsync()...");
                    byte[] bytes = VisionServiceClient.GetThumbnailAsync(memoryStream, width, height).GetAwaiter().GetResult();

                    Bitmap croppedImage = Image.Load(bytes);

                    return(croppedImage);
                }
                else
                {
                    Console.WriteLine("Image was already small. No reason to crop");
                    return(sourceImage);
                }
            }
        }
Example #2
0
        public static AnalysisResult AnalyzeImage(Bitmap sourceImage)
        {
            VisionServiceClient VisionServiceClient = ComputerVisionService.GetClient();

            using (MemoryStream memoryStream = new MemoryStream())
            {
                sourceImage.SaveAsPng(memoryStream);
                memoryStream.Position = 0;

                Console.WriteLine("Calling VisionServiceClient.AnalyzeImageAsync()...");
                VisualFeature[] visualFeatures = new VisualFeature[] {
                    VisualFeature.Adult,
                    VisualFeature.Color,
                    VisualFeature.Description,
                    VisualFeature.ImageType
                };

                try
                {
                    AnalysisResult analysisResult = VisionServiceClient.AnalyzeImageAsync(memoryStream, visualFeatures).GetAwaiter().GetResult();
                    return(analysisResult);
                }
                catch (Microsoft.ProjectOxford.Vision.ClientException exception)
                {
                    Console.WriteLine(exception.Error.Message);
                    Console.WriteLine(exception.Error.Code);

                    throw exception;
                }
            }
        }
Example #3
0
        public static AnalysisBlob GetAnalysisBlob(Bitmap image)
        {
            AnalysisBlob analysisBlob = new AnalysisBlob();

            analysisBlob.generalAnalysisResult = ComputerVisionService.AnalyzeImage(image);
            analysisBlob.ocrAnalysisResult     = ComputerVisionService.AnalyzeText(image);
            return(analysisBlob);
        }
Example #4
0
        public static OcrResults AnalyzeText(Bitmap sourceImage)
        {
            Microsoft.ProjectOxford.Vision.VisionServiceClient visionServiceClient = ComputerVisionService.GetClient();

            using (MemoryStream memoryStream = new MemoryStream())
            {
                sourceImage.SaveAsPng(memoryStream);
                memoryStream.Position = 0;

                Console.WriteLine("Calling VisionServiceClient.RecognizeTextAsyncs()...");

                return(visionServiceClient.RecognizeTextAsync(memoryStream).GetAwaiter().GetResult());
            }
        }
Example #5
0
        public static CelebrityAnalysisResult AnalyzeImageForCelebrities(Bitmap sourceImage)
        {
            VisionServiceClient VisionServiceClient = ComputerVisionService.GetClient();

            using (MemoryStream memoryStream = new MemoryStream())
            {
                sourceImage.SaveAsPng(memoryStream);
                memoryStream.Position = 0;

                Console.WriteLine("Calling VisionServiceClient.AnalyzeImageInDomainAsync()...");

                // This is how you'd recognize celebrities like Henry Clay
                Microsoft.ProjectOxford.Vision.Contract.AnalysisInDomainResult result = VisionServiceClient.AnalyzeImageInDomainAsync(memoryStream, "celebrities").GetAwaiter().GetResult();

                Newtonsoft.Json.Linq.JObject jsonObj = result.Result as Newtonsoft.Json.Linq.JObject;

                CelebrityAnalysisResult celebResult = jsonObj.ToObject <CelebrityAnalysisResult>() as CelebrityAnalysisResult;
                return(celebResult);
            }
        }
Example #6
0
        static void CreateAppraisals(PostToTwitterMode postToTwitterMode, int numberOfAppraisals)
        {
            Console.WriteLine("Getting collection listing");

            Art art = new Art(new IArtSource[] { new HarvardArtMuseum(), new MetropolitanMuseumOfArt() });

            IEnumerable <Art.Object> responseObjects = art.GetRandomObjects(numberOfAppraisals);

            Console.WriteLine("Found " + responseObjects.Count() + " results");

            int objectCounter = 0;

            foreach (Art.Object responseObject in responseObjects)
            {
                Console.WriteLine("-----------------------------------------------------------------------");

                string imageUrl = responseObject.imageURL;
                Console.WriteLine("image url: " + imageUrl);

                string fullListingURL = responseObject.listingURL;
                Console.WriteLine("Listing page: " + fullListingURL);

                Bitmap originalImage = Web.DownloadImage(imageUrl);

                if (originalImage != null)
                {
                    Bitmap resizedImage = ImageTransforms.ResizeToWithinAnalysisLimits(originalImage);

                    if (Directory.Exists("images"))
                    {
                        string originalDestinationFilePath = @"images/sourceImage" + objectCounter + ".jpg";
                        originalImage.Save(originalDestinationFilePath);

                        string resizedDestinationFilePath = @"images/resizedImage" + objectCounter + ".jpg";
                        resizedImage.Save(resizedDestinationFilePath);
                    }

                    ComputerVisionService.AnalysisBlob analysisBlob = ComputerVisionService.GetAnalysisBlob(resizedImage);

                    string tagString = "";
                    foreach (string tag in analysisBlob.generalAnalysisResult.Description.Tags)
                    {
                        tagString += tag + ", ";
                    }
                    Console.WriteLine("Tags: " + tagString);

                    string accentColor = ColorTable.GetClosestColorName(analysisBlob.generalAnalysisResult.Color.AccentColor);

                    Console.WriteLine("Foreground: " + analysisBlob.generalAnalysisResult.Color.DominantColorForeground + " Background: " + analysisBlob.generalAnalysisResult.Color.DominantColorBackground + " Accent: " + accentColor);

                    if (analysisBlob.generalAnalysisResult.Categories != null)
                    {
                        string categoryString = "";
                        foreach (Category category in analysisBlob.generalAnalysisResult.Categories)
                        {
                            categoryString += category.Name + ", ";
                        }
                        Console.WriteLine("Categories: " + categoryString);
                    }

                    Appraisal appraisal     = CreateAppraisal(originalImage, analysisBlob);
                    string    tweetableText = GetTweetableText(appraisal.comment, responseObject.listingURL, responseObject.artSourceHashTag);

                    if (Directory.Exists("images"))
                    {
                        string destinationFilePath = @"images/finalImage" + objectCounter + ".jpg";
                        appraisal.image.Save(destinationFilePath);

                        using (StreamWriter file = File.CreateText(@"images/comment" + objectCounter + ".txt"))
                        {
                            file.WriteLine(tweetableText);
                        }
                    }

                    if (postToTwitterMode == PostToTwitterMode.Yes)
                    {
                        TweetAppraisal(appraisal.image, tweetableText);
                    }
                }

                objectCounter++;
            }
        }