Esempio n. 1
0
        internal static string GetCaption(Activity message, string key, string url)
        {
            var visionAPiClient = new VisionServiceClient(key, url);

            var image = message.Attachments?
                        .FirstOrDefault(x => x.ContentType.Contains("image"));

            if (image != null)
            {
                //emulator
                using (var stream = GetImageStream(image))
                {
                    var result = visionAPiClient.AnalyzeImageAsync(stream,
                                                                   new string[] { VisualFeature.Description.ToString() })
                                 .GetAwaiter()
                                 .GetResult();
                    return(result.Description.Captions.FirstOrDefault().Text);
                }
            }
            //Facebook messenger
            var messengerResult = visionAPiClient.AnalyzeImageAsync(message.Text,
                                                                    new string[] { VisualFeature.Description.ToString() }).GetAwaiter().GetResult();

            return(messengerResult.Description.Captions.FirstOrDefault().Text);
        }
        public async Task <string> RecognizeImageFromStream(Stream stream)
        {
            VisualFeature[] visualFeatures = new VisualFeature[] { VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description, VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags };
            AnalysisResult  x = await VisionServiceClient.AnalyzeImageAsync(stream, visualFeatures);

            var result = LogAnalysisResult(x);

            return(result);
        }
        private async Task <AnalysisResult> UploadAndAnalyzeImage(StorageFile imageFile)
        {
            var stream = await imageFile.OpenStreamForReadAsync();


            Log("Calling VisionServiceClient.AnalyzeImageAsync()...");
            VisualFeature[] visualFeatures = new VisualFeature[] { VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description, VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags };
            AnalysisResult  analysisResult = await VisionServiceClient.AnalyzeImageAsync(stream, visualFeatures);

            return(analysisResult);
        }
Esempio n. 4
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            // grab the key and URI from the portal config
            string visionKey = Environment.GetEnvironmentVariable("VisionKey");
            string visionUri = Environment.GetEnvironmentVariable("VisionUri");

            // create a client and request Tags for the image submitted
            VisionServiceClient vsc = new VisionServiceClient(visionKey, visionUri);

            VisualFeature[] vf     = { VisualFeature.Tags };
            AnalysisResult  result = null;
            string          url    = string.Empty;

            log.Info("Calling Vision API");

            // if it's a POST method, we read the content as a byte array and assume it's an image
            if (req.Method.Method == "POST")
            {
                Stream stream = await req.Content.ReadAsStreamAsync();

                try
                {
                    result = await vsc.AnalyzeImageAsync(stream, vf);
                }
                catch (ClientException e)
                {
                    log.Info("Vision client error: " + e.Error.Message);
                }
            }

            // else, if it's a GET method, we assume there's a URL on the query string, pointing to a valid image
            else if (req.Method.Method == "GET")
            {
                url = req.GetQueryNameValuePairs().FirstOrDefault(q => string.Compare(q.Key, "url", true) == 0).Value;
                try
                {
                    result = await vsc.AnalyzeImageAsync(url, vf);
                }
                catch (ClientException e)
                {
                    log.Info("Vision client error: " + e.Error.Message);
                }
            }

            // if we didn't get a result from the service, return a 400
            if (result == null)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest));
            }

            return(GetResponse(req, result.Tags));
        }
Esempio n. 5
0
        public async Task <string> GetImageDescription(string imageUrl)
        {
            try
            {
                var result = await visionClient.AnalyzeImageAsync(imageUrl, features.ToList(), null);

                return(InterpretAnalysisResult(result));
            }
            catch (Exception ex)
            {
                return("Unable to Analyze Image " + ex.Message);
            }
        }
Esempio n. 6
0
        private async Task <AnalysisResult> CheckImage(Stream stream)
        {
            AnalysisResult analysisResult;

            VisualFeature[] visualFeatures = new VisualFeature[] { VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description, VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags };

            analysisResult = await visionClient.AnalyzeImageAsync(stream, visualFeatures);

            if (analysisResult == null)
            {
                throw new Exception("Can't detect image");
            }
            return(analysisResult);
        }
Esempio n. 7
0
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "cognitiveservices/imagedescription")] HttpRequest req, ILogger log)
        {
            try
            {
                var body = await req.ReadAsStringAsync();

                var cognitiveServicesRequestItem = JsonConvert.DeserializeObject <CognitiveServicesRequestItem>(body);

                var url            = cognitiveServicesRequestItem.Url;
                var image          = cognitiveServicesRequestItem.ImageBytes;
                var apiKey         = cognitiveServicesRequestItem.ApiKey;
                var domainEndpoint = cognitiveServicesRequestItem.DomainEndpoint;

                if (string.IsNullOrEmpty(apiKey) || string.IsNullOrEmpty(domainEndpoint))
                {
                    return(new BadRequestObjectResult("Please provide an api key and a domain endpoint"));
                }

                if (string.IsNullOrEmpty(url) && image == null)
                {
                    return(new BadRequestObjectResult("Please provide an image or an url"));
                }

                // analyze image from url with the provided apikey
                var service        = new VisionServiceClient(apiKey, $"https://{domainEndpoint}.api.cognitive.microsoft.com/vision/v1.0");
                var visualFeatures = new[] { VisualFeature.Description };

                AnalysisResult result = null;

                if (string.IsNullOrEmpty(url))
                {
                    result = await service.AnalyzeImageAsync(new MemoryStream(image), visualFeatures);
                }
                else
                {
                    result = await service.AnalyzeImageAsync(url, visualFeatures);
                }

                var imageDescriptionResult = result?.Description?.Captions;

                // send the result back
                return(new OkObjectResult(imageDescriptionResult));
            }
            catch (Exception e)
            {
                return(new BadRequestObjectResult(e.Message));
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Gets the caption of an image URL.
        /// <remarks>
        /// This method calls <see cref="IVisionServiceClient.AnalyzeImageAsync(string, string[])"/> and
        /// returns the first caption from the returned <see cref="AnalysisResult.Description"/>
        /// </remarks>
        /// </summary>
        /// <param name="url">The URL to an image.</param>
        /// <returns>Description if caption found, null otherwise.</returns>
        public async Task <string> GetCaptionAsync(string url)
        {
            var client = new VisionServiceClient(ApiKey, apiRoot: "https://eastus.api.cognitive.microsoft.com/vision/v1.0");
            var result = await client.AnalyzeImageAsync(url, VisualFeatures);

            return(ProcessAnalysisResult(result));
        }
Esempio n. 9
0
        public async Task <AnalysisResult> GetImageDescription(Stream imageStream)
        {
            VisionServiceClient visionClient = new VisionServiceClient("0d2957b6055d42598f1b4588c8f7282d", "https://westcentralus.api.cognitive.microsoft.com/vision/v2.0");

            VisualFeature[] features = { VisualFeature.Tags, VisualFeature.Categories, VisualFeature.Description };
            return(await visionClient.AnalyzeImageAsync(imageStream, features.ToList(), null));
        }
Esempio n. 10
0
        private async Task AnalyzeImage(StorageFile imageFile)
        {
            resultText.Text = "Analyzing...";
            try
            {
                VisionServiceClient visionServiceClient = new VisionServiceClient("");
                using (var stream = await imageFile.OpenStreamForReadAsync())
                {
                    VisualFeature[] visualFeatures = new VisualFeature[]
                    {
                        VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description,
                        VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags
                    };

                    AnalysisResult analysisResult =
                        await visionServiceClient.AnalyzeImageAsync(stream, visualFeatures);

                    resultText.Text = string.Join("\n", analysisResult.Tags.OrderByDescending(x => x.Confidence).Select(x => $"{x.Name} ({x.Confidence:F})"));
                }
            }
            catch (Exception ex)
            {
                resultText.Text = $"Failed: {ex.Message}";
            }
        }
Esempio n. 11
0
        private async Task <string> Vision(string url)
        {
            VisionServiceClient client = new VisionServiceClient("809703eae3894ddf9e2e9b9d33b6cab1");
            var result = await client.AnalyzeImageAsync(url, new VisualFeature[] { VisualFeature.Description });

            return(result.Description.Captions.First().Text);
        }
Esempio n. 12
0
        public async Task <AnalysisResult> GetImageDescription(Stream imageStream)
        {
            VisionServiceClient visionClient = new VisionServiceClient("SEU_CODIGO_AQUI");

            VisualFeature[] features = { VisualFeature.Tags, VisualFeature.Categories, VisualFeature.Description };
            return(await visionClient.AnalyzeImageAsync(imageStream, features.ToList(), null));
        }
Esempio n. 13
0
        protected async Task MessageReceived(IDialogContext context, IAwaitable <IMessageActivity> item)
        {
            var idUser = context.Activity.From.Id;

            string[] search = new string[2] {
                "Description", "Tags"
            };

            try
            {
                var result = await _visionClient.AnalyzeImageAsync(GetStreamFromUrl(_activity.Attachments[0].ContentUrl), search);

                //return new HttpResponseMessage(System.Net.HttpStatusCode.Accepted);
                await context.PostAsync(result.Description.ToString());

                context.Wait(MessageReceived);
                //Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");
                //await connector.Conversations.ReplyToActivityAsync(reply);
            }
            catch (Microsoft.ProjectOxford.Vision.ClientException clientException)
            {
                //Trace Ecrit en mode debug ET release
                Debug.WriteLine(clientException.Error.Message);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
        public async Task <AnalysisResult> GetImageDescription(Stream stream)
        {
            VisionServiceClient visionClient = new VisionServiceClient("<<YOUR API KEY>>");

            VisualFeature[] features = { VisualFeature.Tags, VisualFeature.Categories, VisualFeature.Description };
            return(await visionClient.AnalyzeImageAsync(stream, features.ToList(), null));
        }
        /// <summary>
        /// Uploads the image to Project Oxford and performs analysis
        /// </summary>
        /// <param name="imageFilePath">The image file path.</param>
        /// <returns></returns>
        private async Task <AnalysisResult> UploadAndAnalyzeImage(string imageFilePath)
        {
            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE STARTS HERE
            // -----------------------------------------------------------------------

            //
            // Create Project Oxford Vision API Service client
            //
            VisionServiceClient VisionServiceClient = new VisionServiceClient(SubscriptionKey, "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0");

            Log("VisionServiceClient is created");

            using (Stream imageFileStream = File.OpenRead(imageFilePath))
            {
                //
                // Analyze the image for all visual features
                //
                Log("Calling VisionServiceClient.AnalyzeImageAsync()...");
                VisualFeature[] visualFeatures = new VisualFeature[] { VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description, VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags };
                AnalysisResult  analysisResult = await VisionServiceClient.AnalyzeImageAsync(imageFileStream, visualFeatures);

                return(analysisResult);
            }

            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE ENDS HERE
            // -----------------------------------------------------------------------
        }
Esempio n. 16
0
        private static async Task analyseImage(Activity reply, string url)
        {
            var client = new VisionServiceClient("2ab40d02f36e4d3d8e4120dc74022a7d");
            var result = await client.AnalyzeImageAsync(url, new VisualFeature[] { VisualFeature.Description });

            reply.Text = result.Description.Captions.First().Text;
        }
Esempio n. 17
0
        public async Task <AnalysisResult> GetImageDescription(Stream stream)
        {
            VisionServiceClient visionClient = new VisionServiceClient(Config.CognitiveClientApiKey, Config.CognitiveClientApiRoot);

            VisualFeature[] features = { VisualFeature.Tags, VisualFeature.Categories, VisualFeature.Description };
            return(await visionClient.AnalyzeImageAsync(stream, features.ToList(), null));
        }
Esempio n. 18
0
        /// <summary>
        /// API Key needed.
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public async Task <string> GetOcrTextAsync(string url)
        {
            var client = new VisionServiceClient(ApiKey);
            var result = await client.AnalyzeImageAsync(url, VisualFeatures);

            return(ProcessAnalysisResult2(result));
        }
Esempio n. 19
0
        public static async Task <AnalysisResult> SmartImageProcessing(string fname, string method)
        {
            AnalysisResult              analyzed       = null;
            VisionServiceClient         client         = new VisionServiceClient(API_key, API_location);
            IEnumerable <VisualFeature> visualFeatures = GetVisualFeatures();

            if (File.Exists(fname))
            {
                using (Stream stream = File.OpenRead(fname))
                    switch (method)
                    {
                    case "analyze":
                        analyzed = await client.AnalyzeImageAsync(stream, visualFeatures);

                        break;

                    case "describe":
                        analyzed = await client.DescribeAsync(stream);

                        break;

                    case "tag":
                        analyzed = await client.GetTagsAsync(stream);

                        break;
                    }
            }
            return(analyzed);
        }
Esempio n. 20
0
        public async Task <AnalysisResult> GetImageDescriptionAsync(Stream imageStream)
        {
            VisualFeature[] features = { VisualFeature.Tags, VisualFeature.Categories, VisualFeature.Description };
            var             result   = await visionClient.AnalyzeImageAsync(imageStream, features.ToList(), null);

            return(result);
        }
Esempio n. 21
0
        public async Task <VisionResult> Analyze(Stream stream)
        {
            var client = new VisionServiceClient(Constants.VisionApiKey, Constants.VisionApiEndpoint);

            var attributes = new List <VisionAttribute>();
            var rectangles = new List <Rectangle>();

            using (stream)
            {
                var features      = new VisualFeature[] { VisualFeature.Tags, VisualFeature.Faces };
                var visionsResult = await client.AnalyzeImageAsync(stream, features, null);

                if (visionsResult != null && visionsResult?.Tags.Length > 0)
                {
                    if (visionsResult.Faces != null)
                    {
                        foreach (var face in visionsResult.Faces)
                        {
                            rectangles.Add(face.FaceRectangle.ToRectangle());
                        }
                    }

                    foreach (var tag in visionsResult.Tags)
                    {
                        attributes.Add(new VisionAttribute(tag.Name, tag.Hint, tag.Confidence));
                    }
                }
            }
            return(new VisionResult {
                Attributes = attributes, Rectangles = rectangles
            });
        }
Esempio n. 22
0
        private static async Task <string> AnalyzeImageAsync(Stream image)
        {
            var client = new VisionServiceClient(VisionServiceApiKey);
            var result = await client.AnalyzeImageAsync(image, new VisualFeature[] { VisualFeature.Description });

            return(result.Description.Captions.FirstOrDefault().Text);
        }
Esempio n. 23
0
        //Core image processing method -> Internally, this method will be able to invoke several CV API methods, such as analyze and describe.
        //This is why, we specify a second parameter named method.
        public static async Task <AnalysisResult> SmartImageProcess(string fname, string method)
        {
            AnalysisResult      analyzed = null;
            VisionServiceClient client   = new VisionServiceClient(API_key, API_location);

            //We call GetVisualFeatures() and assign it to an internal variable, which we will pass onto API method calls.
            IEnumerable <VisualFeature> visualFeatures = GetVisualFeatures();

            //Then, we read the input image, and with a switch statement, check which CV API method we want to invoke.
            if (File.Exists(fname))
            {
                using (Stream stream = File.OpenRead(fname))
                    switch (method)
                    {
                    case "analyze":
                        analyzed = await client.AnalyzeImageAsync(stream, visualFeatures);

                        break;

                    case "describe":
                        analyzed = await client.DescribeAsync(stream);

                        break;

                    case "tag":
                        analyzed = await client.GetTagsAsync(stream);

                        break;
                    }
            }

            return(analyzed);
        }
Esempio n. 24
0
        private async Task <AnalysisResult> GetImageDescription(Stream imageStream)
        {
            VisionServiceClient visionClient = new VisionServiceClient("4d673d0f18bc44d3b519400de1a7e76d", "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0");

            VisualFeature[] features = { VisualFeature.Tags };
            return(await visionClient.AnalyzeImageAsync(imageStream, features.ToList(), null));
        }
        /// <summary>
        /// Uploads the image to Project Oxford and performs analysis
        /// </summary>
        /// <param name="imageFilePath">The image file path.</param>
        /// <returns></returns>
        private async Task<AnalysisResult> UploadAndAnalyzeImage(string imageFilePath)
        {
            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE STARTS HERE
            // -----------------------------------------------------------------------

            //
            // Create Project Oxford Vision API Service client
            //
            VisionServiceClient VisionServiceClient = new VisionServiceClient(SubscriptionKey);
            Log("VisionServiceClient is created");

            using (Stream imageFileStream = File.OpenRead(imageFilePath))
            {
                //
                // Analyze the image for all visual features
                //
                Log("Calling VisionServiceClient.AnalyzeImageAsync()...");
                VisualFeature[] visualFeatures = new VisualFeature[] { VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description, VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags };
                AnalysisResult analysisResult = await VisionServiceClient.AnalyzeImageAsync(imageFileStream, visualFeatures);
                return analysisResult;
            }

            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE ENDS HERE
            // -----------------------------------------------------------------------
        }
        async Task analyseImage(Stream imageStream)
        {
            try
            {
                VisionServiceClient visionClient = new VisionServiceClient("c19d4b8bb6c242ea99a8a998195a24f0");
                VisualFeature[]     features     = { VisualFeature.Tags, VisualFeature.Categories, VisualFeature.Description };
                var analysisResult = await visionClient.AnalyzeImageAsync(imageStream, features.ToList(), null);

                Tag[] list = analysisResult.Tags.ToArray();


                Console.Out.WriteLine("Tags:\n");
                foreach (Tag t in list)
                {
                    Console.Out.WriteLine(t.Name);
                    Console.Out.WriteLine(t.Confidence);
                }
                Console.Out.WriteLine("Cats:\n");
                foreach (Category c in analysisResult.Categories.ToArray())
                {
                    Console.Out.WriteLine(c.Name);
                    Console.Out.WriteLine(c.Score);
                }
                AnalysisLabel.Text = string.Empty;

                analysisResult.Description.Tags.ToList().ForEach(tag => AnalysisLabel.Text = AnalysisLabel.Text + tag + "\n");


                //Console.Out.WriteLine(analysisResult.Categories.t);
            }
            catch (Microsoft.ProjectOxford.Vision.ClientException ex)
            {
                AnalysisLabel.Text = ex.Error.Message;
            }
        }
Esempio n. 27
0
        private static async Task <AnalysisResult> UploadAndAnalyzeImage(string imageFilePath)
        {
            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE STARTS HERE
            // -----------------------------------------------------------------------

            //
            // Create Project Oxford Computer Vision API Service client
            //

            VisionServiceClient VisionServiceClient = new VisionServiceClient(ComKey);

            Log("VisionServiceClient is created");

            using (Stream imageFileStream = File.OpenRead(imageFilePath))
            {
                //
                // Analyze the image for all visual features
                //
                Log("Calling VisionServiceClient.AnalyzeImageAsync()...");
                VisualFeature[] visualFeatures = new VisualFeature[] { VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description, VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags };
                AnalysisResult  analysisResult = await VisionServiceClient.AnalyzeImageAsync(imageFileStream, visualFeatures);

                return(analysisResult);
            }

            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE ENDS HERE
            // -----------------------------------------------------------------------
        }
        private async Task RunTest()
        {
            output.Text = "We hebben je residu kunnen scannen, zie hieronder jouw persoonlijke toekomst beeld:";
            try
            {
                var client = new VisionServiceClient("bae2f176b113415f81bdca26eccab2e9");

                var url = "http://koffiedikkijker.azurewebsites.net" + image.ImageUrl;
                var fileReq = (HttpWebRequest)WebRequest.Create(url);
                var fileResp = (HttpWebResponse) fileReq.GetResponse();
                var stream = fileResp.GetResponseStream();
                var result = await client.AnalyzeImageAsync(stream);
                foreach (var face in result.Faces)
                {
                    var age = face.Age;
                    var gender = face.Gender == "Male" ? "man" : "vrouw";
                    output.Text = string.Format("Helaas, deze {0} jarige {1} kan (nog) niet worden gekwalificeerd als een leeg kopje koffie.", age, gender);
                    output.Visible = true;
                    containerContent.Visible = false;
                }
            }
            catch (Exception ex)
            {
                
            }
        }
Esempio n. 29
0
        /// <summary>
        /// Gets the caption of the image from an image stream.
        /// <remarks>
        /// This method calls <see cref="IVisionServiceClient.AnalyzeImageAsync(Stream, string[])"/> and
        /// returns the first caption from the returned <see cref="AnalysisResult.Description"/>
        /// </remarks>
        /// </summary>
        /// <param name="stream">The stream to an image.</param>
        /// <returns>Description if caption found, null otherwise.</returns>
        public async Task <string> GetCaptionAsync(Stream stream)
        {
            var client = new VisionServiceClient(ApiKey);
            var result = await client.AnalyzeImageAsync(stream, VisualFeatures);

            return(ProcessAnalysisResult(result));
        }
        public async Task <ActionResult> Index()
        {
            if (Request.HttpMethod == "GET")
            {
                return(View("Index"));
            }

            var model = new DescribeImageModel();

            var features = new[]
            {
                VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description,
                VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags
            };

            await RunOperationOnImage(async stream =>
            {
                model.Result = await VisionServiceClient.AnalyzeImageAsync(stream, features);
            });

            await RunOperationOnImage(async stream => {
                var bytes = new byte[stream.Length];

                stream.Read(bytes, 0, bytes.Length);

                var base64      = Convert.ToBase64String(bytes);
                model.ImageDump = String.Format("data:image/png;base64,{0}", base64);
            });

            return(View(model));
        }
Esempio n. 31
0
        public async Task <AnalysisResult> GetImageDescription(Stream imageStream)
        {
            VisionServiceClient visionClient = new VisionServiceClient("", "https://eastus.api.cognitive.microsoft.com/vision/v1.0");

            VisualFeature[] features = { VisualFeature.Tags, VisualFeature.Categories, VisualFeature.Description };
            return(await visionClient.AnalyzeImageAsync(imageStream, features.ToList(), null));
        }
Esempio n. 32
0
        private async Task <AnalysisResult> UploadAndAnalyzeImage(string imageFilePath)
        {
            //
            // Create Project Oxford Vision API Service client
            //
            Speak("Ok, I am analysing your photo!");
            VisionServiceClient VisionServiceClient = new VisionServiceClient("---------");

            Debug.WriteLine("VisionServiceClient is created");

            StorageFile file = await StorageFile.GetFileFromPathAsync(imageFilePath);

            using (Stream imageFileStream = (await file.OpenReadAsync()).AsStreamForRead())
            {
                // Analyze the image for all visual features
                Debug.WriteLine("Calling VisionServiceClient.AnalyzeImageAsync()...");
                VisualFeature[] visualFeatures = new VisualFeature[]
                {
                    VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description,
                    VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags
                };

                AnalysisResult analysisResult =
                    await VisionServiceClient.AnalyzeImageAsync(imageFileStream, visualFeatures);

                Debug.WriteLine(analysisResult);

                return(analysisResult);
            }
        }
Esempio n. 33
0
        private async Task<AnalysisResult> UploadAndAnalyzeImage(Stream imageStream)
        {
            var visionServiceClient = new VisionServiceClient(Constants.CuomputerVisionApiKey);
            var assembley = this.GetType().GetTypeInfo().Assembly;
            using (Stream imageFileStream = imageStream)
            {
                VisualFeature[] visualFeatures = new VisualFeature[]
                {
                    VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description,
                    VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags
                };
                AnalysisResult analysisResult =
                    await visionServiceClient.AnalyzeImageAsync(imageFileStream, visualFeatures);
                return analysisResult;
            }

        }
        public async Task<AnalysisResult> AnalyzeImage(string selectedFile)
        {
            IVisionServiceClient visionClient = new VisionServiceClient(_subscriptionKeyVision);
            AnalysisResult analysisResult = null;

            ErrorMesssage = string.Empty;
            try
            {
                if (File.Exists(selectedFile))
                {
                    //using (FileStream stream = File.Open(imagePathOrUrl, FileMode.Open))
                    using (var fileStreamVision = File.OpenRead(selectedFile))
                    {
                        analysisResult = await visionClient.AnalyzeImageAsync(fileStreamVision);
                    }
                }
                else if (Uri.IsWellFormedUriString(selectedFile, UriKind.RelativeOrAbsolute))
                {
                    analysisResult = await visionClient.AnalyzeImageAsync(selectedFile);
                }
                else
                {
                    ErrorMesssage = "Invalid image path or Url";
                }
            }
            catch (ClientException e)
            {
                ErrorMesssage = e.Error != null ? e.Error.Message : e.Message;
            }
            catch (Exception exception)
            {
                ErrorMesssage = exception.ToString();
            }

            return analysisResult;
        }
        /// <summary>
        /// Sends a url to Project Oxford and performs analysis
        /// </summary>
        /// <param name="imageUrl">The url of the image to analyze</param>
        /// <returns></returns>
        private async Task<AnalysisResult> AnalyzeUrl(string imageUrl)
        {
            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE STARTS HERE
            // -----------------------------------------------------------------------

            //
            // Create Project Oxford Vision API Service client
            //
            VisionServiceClient VisionServiceClient = new VisionServiceClient(SubscriptionKey);
            Log("VisionServiceClient is created");

            //
            // Analyze the url for all visual features
            //
            Log("Calling VisionServiceClient.AnalyzeImageAsync()...");
            VisualFeature[] visualFeatures = new VisualFeature[] { VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description, VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags };
            AnalysisResult analysisResult = await VisionServiceClient.AnalyzeImageAsync(imageUrl, visualFeatures);
            return analysisResult;

            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE ENDS HERE
            // -----------------------------------------------------------------------
        }
 private async Task<AnalysisResult> UploadAndAnalyzeImage(string subscriptionKey, Stream imageStream)
 {
     try
     {
         var visionServiceClient = new VisionServiceClient(subscriptionKey);
         using (var imageFileStream = imageStream)
         {
             var visualFeatures = new[]
             {
                 VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description,
                 VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags
             };
             var analysisResult = await visionServiceClient.AnalyzeImageAsync(imageFileStream, visualFeatures);
             return analysisResult;
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine($"{ex}");
         return null;
     }
 }
Esempio n. 37
0
 /// <summary>
 /// Microsoft ProjectOxford Vision API を使用して画像の解析を行います
 /// </summary>
 /// <param name="imageUri">画像URL</param>
 /// <returns>解析結果を格納したAnalysisResultオブジェクト</returns>
 public async static Task<AnalysisResult> AnalyzeImageAsync(Uri imageUri, string subscriptionKey)
 {
     var visionClient = new VisionServiceClient(subscriptionKey);
     return await visionClient.AnalyzeImageAsync(imageUri.AbsoluteUri).ConfigureAwait(false);
 }