Ejemplo n.º 1
0
        static async Task Main(string[] args)
        {
            string host            = "https://api.cognitive.microsofttranslator.com";
            string route           = "/translate?api-version=3.0&to=de&to=sv";
            string subscriptionKey = "xxxxxxxxxxxxxxxxxxx";

            // Prompts you for text to translate. If you'd prefer, you can
            // provide a string as textToTranslate.
            Console.Write("Type the phrase you'd like to translate? ");
            string textToTranslate = Console.ReadLine();

            await TranslateTextRequest(subscriptionKey, host, route, textToTranslate);

            Console.WriteLine("Please input image url or locate a local image file. If input is empty, example image will be used.");
            var imagePath = Console.ReadLine().Trim();

            if (string.IsNullOrWhiteSpace(imagePath))
            {
                Console.WriteLine($"No url or file specified, use the example {DefaultImageUrl}");
                imagePath = DefaultImageUrl;
            }
            System.Diagnostics.Process.Start(imagePath);

            var result = await client.DescribeImageAsync(imagePath);

            Console.WriteLine(Environment.NewLine + $"Analyze result of {imagePath} is");
            Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Analyze the image and return a description
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public async Task <string> AnalyzeImage(string url)
        {
            if (!_availableServices.Contains(AzureServiceType.ComputerVision))
            {
                return(null);
            }

            _computerVisionSemaphore.Wait();
            try
            {
                ImageDescription imageDescription = await _computerVisionClient.DescribeImageAsync(url, 1);

                if (!imageDescription.Captions.Any())
                {
                    return("I have no idea.");
                }
                else
                {
                    return(imageDescription.Captions.First().Text);
                }
            }
            catch (Exception ex)
            {
                string message = "Failed processing image.";
                _logger.Log(message, ex);
                return(message);
            }
            finally
            {
                _computerVisionSemaphore.Release();
            }
        }
        /// <summary>
        /// Sends a URL to Cognitive Services and performs description.
        /// </summary>
        /// <param name="imageUrl">The URL of the image to describe.</param>
        /// <returns>Awaitable image description.</returns>
        private async Task <ImageDescription> DescribeUrlAsync(string imageUrl)
        {
            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE STARTS HERE
            // -----------------------------------------------------------------------

            //
            // Create Cognitive Services Vision API Service client.
            //
            using (var client = new ComputerVisionClient(Credentials)
            {
                Endpoint = Endpoint
            })
            {
                Log("ComputerVisionClient is created");

                //
                // Describe the URL and ask for three captions.
                //
                Log("Calling ComputerVisionClient.DescribeImageAsync()...");
                string           language       = (_language.SelectedItem as RecognizeLanguage).ShortCode;
                ImageDescription analysisResult = await client.DescribeImageAsync(imageUrl, 3, language);

                return(analysisResult);
            }

            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE ENDS HERE
            // -----------------------------------------------------------------------
        }
        // Analyze a remote image
        private static async Task DescribeImageFromUrlAsync(ComputerVisionClient computerVision, string imageUrl)
        {
            if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute))
            {
                Console.WriteLine("\nInvalid remote image url:\n{0} \n", imageUrl);
                return;
            }

            ImageDescription descriptions = await computerVision.DescribeImageAsync(imageUrl);

            Console.WriteLine(imageUrl);
            DisplayDescriptions(descriptions);
        }
Ejemplo n.º 5
0
        static async Task MainAsync()
        {
            try
            {
                Console.WriteLine("Please input image url or locate a local image file. If input is empty, example image will be used.");
                var imagePath = Console.ReadLine().Trim();
                if (string.IsNullOrWhiteSpace(imagePath))
                {
                    Console.WriteLine($"No url or file specified, use the example {DefaultImageUrl}");
                    imagePath = DefaultImageUrl;
                }
                System.Diagnostics.Process.Start(imagePath);

                var result = await client.DescribeImageAsync(imagePath);

                Console.WriteLine(Environment.NewLine + $"Analyze result of {imagePath} is");
                Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }