Example #1
0
 public ComputerVisionMain(string key, AzureRegions region)
 {
     _VisionKey          = key;
     _Region             = region;
     _Client             = new ComputerVisionAPI(new ApiKeyServiceClientCredentials(_VisionKey));
     _Client.AzureRegion = _Region;
 }
Example #2
0
        public static async Task <string> GetImageDetailsAsync(string key, AzureRegions region, string imageUrl)
        {
            StringBuilder      imageDetails = new StringBuilder();
            IComputerVisionAPI client       = new ComputerVisionAPI(new ApiKeyServiceClientCredentials(key));

            client.AzureRegion = region;
            var requiredFeatures = new List <VisualFeatureTypes> {
                VisualFeatureTypes.Adult,
                VisualFeatureTypes.Categories,
                VisualFeatureTypes.Color,
                VisualFeatureTypes.ImageType,
            };

            ImageAnalysis imageAnalysis = await client.AnalyzeImageAsync(imageUrl, requiredFeatures);

            imageDetails.AppendLine($"Dominant Background Color: {imageAnalysis.Color.DominantColorBackground} <br />");
            imageDetails.AppendLine($"Dominant Forground Color: {imageAnalysis.Color.DominantColorForeground} <br />");
            imageDetails.AppendLine($"Is Black & White: {imageAnalysis.Color.IsBWImg} <br />");

            string isLineDraw = imageAnalysis.ImageType.LineDrawingType == 1 ? "Yes" : "No";

            imageDetails.AppendLine($"Is Line drawing: {isLineDraw} <br />");
            imageDetails.AppendLine($"Is Adult content: {imageAnalysis.Adult.IsAdultContent} <br />");

            List <string> categoryList = new List <string>();

            foreach (var category in imageAnalysis.Categories)
            {
                categoryList.Add(category.Name);
            }
            imageDetails.AppendLine($"Categories: {string.Join(", ", categoryList)}");

            return(imageDetails.ToString());
        }
 public AzureMachineLearningService(ILogger <AzureMachineLearningService> logger, IConfiguration configuration)
 {
     _logger              = logger ?? throw new ArgumentNullException(nameof(logger));
     _azureMLApiKey       = configuration["AzureMLApiKey"];
     _basePathImageFolder = configuration["BasePathImageFolder"];
     _computerVisionAPI   = new ComputerVisionAPI(new ApiKeyServiceClientCredentials(_azureMLApiKey),
                                                  new System.Net.Http.DelegatingHandler[] { });
     _computerVisionAPI.AzureRegion = Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models.AzureRegions.Westcentralus;
 }
Example #4
0
        public static async Task <List <OCRTextInfo> > ReadHandwrittenText(string urlOfImage)
        {
            try
            {
                var scc    = new ApiKeyServiceClientCredentials(APIKeys.ComputerVisionAPIKey);
                var vision = new ComputerVisionAPI(scc);

                // Set this to whatever region you created your service in the portal
                vision.AzureRegion = AzureRegions.Southcentralus;

                var headers = await vision.RecognizeTextAsync(urlOfImage, true).ConfigureAwait(false);

                var allSplits   = headers.OperationLocation.Split(new string[] { @"/" }, StringSplitOptions.None);
                var operationId = allSplits[allSplits.GetUpperBound(0)];

                var opResult = await vision.GetTextOperationResultAsync(operationId).ConfigureAwait(false);

                while (opResult.Status != TextOperationStatusCodes.Succeeded && opResult.Status != TextOperationStatusCodes.Failed)
                {
                    opResult = await vision.GetTextOperationResultAsync(operationId).ConfigureAwait(false);
                }

                if (opResult.Status == TextOperationStatusCodes.Failed)
                {
                    return(null);
                }

                var ocrInfo = new List <OCRTextInfo>();
                foreach (var line in opResult.RecognitionResult.Lines)
                {
                    ocrInfo.Add(new OCRTextInfo
                    {
                        LineText = line.Text,
                        LeftTop  = new ImageCoordinate {
                            X = line.BoundingBox[0], Y = line.BoundingBox[1]
                        },
                        RightTop = new ImageCoordinate {
                            X = line.BoundingBox[2], Y = line.BoundingBox[3]
                        },
                        RightBottom = new ImageCoordinate {
                            X = line.BoundingBox[4], Y = line.BoundingBox[5]
                        },
                        LeftBottom = new ImageCoordinate {
                            X = line.BoundingBox[6], Y = line.BoundingBox[7]
                        }
                    });
                }

                return(ocrInfo);
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"*** ERROR: {ex.Message}");
                return(null);
            }
        }
Example #5
0
        public static async Task <string> GetImageDescriptionAsync(string key, AzureRegions region, string imageUrl)
        {
            IComputerVisionAPI client = new ComputerVisionAPI(new ApiKeyServiceClientCredentials(key));

            client.AzureRegion = region;

            var result = await client.DescribeImageAsync(imageUrl);

            return(result.Captions[0].Text);
        }
Example #6
0
        public MainViewModel()
        {
            TakePhotoCommand = new Command(async() => await TakePhoto());

            var creds = new ApiKeyServiceClientCredentials("<your key here>");

            _visionApi = new ComputerVisionAPI(creds)
            {
                AzureRegion = AzureRegions.Westeurope
            };
        }
Example #7
0
        // Analyze a remote image
        private static async Task AnalyzeRemoteAsync(
            ComputerVisionAPI computerVision, string imageUrl)
        {
            if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute))
            {
                Console.WriteLine(
                    "\nInvalid remoteImageUrl:\n{0} \n", imageUrl);
                return;
            }

            ImageAnalysis analysis =
                await computerVision.AnalyzeImageAsync(imageUrl, features);

            DisplayResults(analysis, imageUrl);
        }
Example #8
0
        static void Main(string[] args)
        {
            ComputerVisionAPI computerVision = new ComputerVisionAPI(
                new ApiKeyServiceClientCredentials(subscriptionKey),
                new System.Net.Http.DelegatingHandler[] { });

            computerVision.AzureRegion = AzureRegions.Westeurope;

            Console.WriteLine("Images being analyzed ...");
            var t1 = AnalyzeRemoteAsync(computerVision, remoteImageUrl);
            var t2 = AnalyzeLocalAsync(computerVision, localImagePath);

            Task.WhenAll(t1, t2).Wait(5000);
            Console.WriteLine("Press any key to exit");
            Console.ReadLine();
        }
 public SpotifyController(ILogger <SpotifyController> logger,
                          HttpClient httpClient,
                          SpotifyClient spotifyClient,
                          HelperMethods helperMethods,
                          ComputerVisionAPI computerVisionAPI,
                          CosmosAPI cosmosAPI,
                          SearchAPI searchAPI)
 {
     this.logger            = logger;
     this.httpClient        = httpClient;
     this.spotifyClient     = spotifyClient;
     this.helperMethods     = helperMethods;
     this.computerVisionAPI = computerVisionAPI;
     this.cosmosAPI         = cosmosAPI;
     this.searchAPI         = searchAPI;
 }
Example #10
0
        public async Task <List <string> > AnalyseVideo(int fps)
        {
            Console.WriteLine($"Analysing video {Bytes.Length} bytes");

            WriteVideoFile();
            ExtractFrames(fps);

            List <string> tags = new List <string>();


            //Console.WriteLine("Connecting to Azure");

            ApiKeyServiceClientCredentials creds = new ApiKeyServiceClientCredentials(Environment.GetEnvironmentVariable("computerVisionApiKey"));
            IComputerVisionAPI             azure = new ComputerVisionAPI(creds, new HttpThrottleHandler(new HttpClientHandler()));

            azure.AzureRegion = AzureRegions.Westeurope;

            string[] frameFiles = Directory.GetFiles(BaseDirectory, $"*{ImageFileExt}")
                                  .Select(Path.GetFileName).ToArray();

            // So that the latest ones are more likely to get scanned before 429 errors happen
            frameFiles.Reverse();

            Console.WriteLine($"Processing {frameFiles.Length} frames");

            foreach (string frameFile in frameFiles)
            {
                try
                {
                    Stream    stream    = new FileStream(BaseDirectory + "/" + frameFile, FileMode.Open);
                    TagResult tagResult = await azure.TagImageInStreamAsync(stream);

                    tags.AddRange(tagResult.Tags.Select(tag => tag.Name));
                }
                catch (Exception ex)
                {
                    //Console.WriteLine($"Error: {ex.Message} {ex.InnerException?.Message}");
                }
            }

            tags = tags.Distinct().ToList();

            Console.WriteLine($"Tags retreived: {string.Join(",", tags)}");

            return(tags);
        }
Example #11
0
        public static async Task <string> GetTagsForImageAsync(string key, AzureRegions region, string imageUrl)
        {
            StringBuilder      tagsDetails = new StringBuilder();
            IComputerVisionAPI client      = new ComputerVisionAPI(new ApiKeyServiceClientCredentials(key));

            client.AzureRegion = region;
            var requiredFeatures = new List <VisualFeatureTypes> {
                VisualFeatureTypes.Tags
            };

            ImageAnalysis imageAnalysis = await client.AnalyzeImageAsync(imageUrl, requiredFeatures);

            foreach (var tag in imageAnalysis.Tags)
            {
                tagsDetails.AppendLine($"{tag.Name} ({tag.Confidence}), ");
            }
            return(tagsDetails.ToString().TrimEnd(", ".ToCharArray()));
        }
Example #12
0
        // Analyze a local image
        private static async Task AnalyzeLocalAsync(
            ComputerVisionAPI computerVision, string imagePath)
        {
            if (!File.Exists(imagePath))
            {
                Console.WriteLine(
                    "\nUnable to open or read localImagePath:\n{0} \n",
                    imagePath);
                return;
            }

            using (Stream imageStream = File.OpenRead(imagePath))
            {
                ImageAnalysis analysis = await computerVision.AnalyzeImageInStreamAsync(
                    imageStream, features);

                DisplayResults(analysis, imagePath);
            }
        }
Example #13
0
        public static async Task <string> GetFaceWithInImageAsync(string key, AzureRegions region, string imageUrl)
        {
            IComputerVisionAPI client = new ComputerVisionAPI(new ApiKeyServiceClientCredentials(key));

            client.AzureRegion = region;
            var requiredFeatures = new List <VisualFeatureTypes> {
                VisualFeatureTypes.Faces
            };

            ImageAnalysis imageAnalysis = await client.AnalyzeImageAsync(imageUrl, requiredFeatures);

            StringBuilder faceDetails = new StringBuilder();

            faceDetails.Append($"Found total **{imageAnalysis.Faces.Count}** faces in image.");

            foreach (var face in imageAnalysis.Faces)
            {
                faceDetails.Append($"{Environment.NewLine} - Gender: {face.Gender} | Age: {face.Age}");
            }
            return(faceDetails.ToString());
        }
Example #14
0
        static void Main(string[] args)
        {
            // Create a client.
            string             apiKey = "ENTER YOUR KEY HERE";
            IComputerVisionAPI client = new ComputerVisionAPI(new ApiKeyServiceClientCredentials(apiKey));

            client.AzureRegion = AzureRegions.Westcentralus;

            // Read image file.
            using (FileStream stream = new FileStream(Path.Combine("Images", "house.jpg"), FileMode.Open))
            {
                // Analyze the image.
                ImageAnalysis result = client.AnalyzeImageInStreamAsync(
                    stream,
                    new List <VisualFeatureTypes>()
                {
                    VisualFeatureTypes.Description,
                    VisualFeatureTypes.Categories,
                    VisualFeatureTypes.Color,
                    VisualFeatureTypes.Faces,
                    VisualFeatureTypes.ImageType,
                    VisualFeatureTypes.Tags
                }).Result;

                Console.WriteLine("The image can be described as: {0}\n", result.Description.Captions.FirstOrDefault()?.Text);

                Console.WriteLine("Tags associated with this image:\nTag\t\tConfidence");
                foreach (var tag in result.Tags)
                {
                    Console.WriteLine("{0}\t\t{1}", tag.Name, tag.Confidence);
                }

                Console.WriteLine("\nThe primary colors of this image are: {0}", string.Join(", ", result.Color.DominantColors));
            }

            Console.WriteLine("\nPress ENTER to exit.");
            Console.ReadLine();
        }