public async Task <AnalyzePictureResult> AnalyzePicture(string pictureUrl) { try { var result = await _computerVisionClient.AnalyzeImageAsync(pictureUrl, details : new[] { Details.Landmarks }, visualFeatures : new[] { VisualFeatureTypes.Color, VisualFeatureTypes.Description }); // Get most likely description var description = result.Description.Captions.OrderByDescending(d => d.Confidence).FirstOrDefault()?.Text ?? "nothing! No description found"; // Get accent color var accentColor = Color.FromHex($"#{result.Color.AccentColor}"); // Determine if there are any landmarks to be seen var landmark = result.Categories.FirstOrDefault(c => c.Detail != null && c.Detail.Landmarks.Any()); var landmarkDescription = ""; landmarkDescription = landmark != null?landmark.Detail.Landmarks.OrderByDescending(l => l.Confidence).First().Name : ""; // Wrap in our result object and send along return(new AnalyzePictureResult(description, accentColor, landmarkDescription)); } catch { return(new AnalyzePictureResult()); } }
public AnalysisResult Analyze(string imageUrl, string utterance) { var rosetta = new Rosetta(RossyConfig.RosettaConfig); var intent = rosetta.GuessIntent(utterance); var analyzer = GetAnalyzer(intent); List <VisualFeatureTypes> features = analyzer.SetupAnalysisFeatures(); var client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(RossyConfig.GeordiConfig.SubscriptionKey)) { Endpoint = RossyConfig.GeordiConfig.Endpoint }; ImageAnalysis imageAnalysis = client.AnalyzeImageAsync(imageUrl, features).Result; string log = analyzer.ProduceLog(imageAnalysis); var language = rosetta.GuessLanguage(utterance); string speechText; switch (language) { case "it": speechText = analyzer.ProduceSpeechTextItalian(imageAnalysis); break; case "en": default: speechText = analyzer.ProduceSpeechTextEnglish(imageAnalysis); break; } return(new AnalysisResult(speechText, log)); }
public async Task <IActionResult> Details(string imageUrl) //string language { ViewBag.ImageUrl = imageUrl; ComputerVisionClient client = Client.Authenticate(); List <VisualFeatureTypes> features = new List <VisualFeatureTypes>() { VisualFeatureTypes.Categories, VisualFeatureTypes.Description, VisualFeatureTypes.Faces, VisualFeatureTypes.ImageType, VisualFeatureTypes.Tags, VisualFeatureTypes.Adult, VisualFeatureTypes.Color, VisualFeatureTypes.Brands, VisualFeatureTypes.Objects }; // Path.GetFileName(imageUrl); // Redirects to Invalid Image view if Computer Vision returns an error // Assumes Error is due to a bad image url - is not of a direct image e.g., google.com try { ImageAnalysis results = await client.AnalyzeImageAsync(imageUrl, features); return(View(results)); } catch (Exception e) { return(RedirectToAction("InvalidImage")); } }
public static async Task Main(string[] args) { Console.WriteLine("BirdOrNot"); Console.WriteLine("-----------------"); Console.WriteLine("Url: "); var url = Console.ReadLine(); var AzureComputerVisionSubscriptionKey = Environment.GetEnvironmentVariable("AzureComputerVisionSubscriptionKey"); var AzureComputerVisionEndpoint = Environment.GetEnvironmentVariable("AzureComputerVisionEndpoint"); var computerVisionClient = new ComputerVisionClient(new ApiKeyServiceClientCredentials(AzureComputerVisionSubscriptionKey)) { Endpoint = AzureComputerVisionEndpoint }; var imageAnalysis = await computerVisionClient.AnalyzeImageAsync(url, new List <VisualFeatureTypes> { VisualFeatureTypes.Objects }); var isBird = imageAnalysis.Objects.Any(x => x.ObjectProperty.Equals("bird") || CheckForBirdRecursive(x.Parent)); Console.WriteLine(isBird ? "It is a bird." : "It is not a bird."); }
public async Task <string> GetAnalysisAsync(string imageUrl) { List <VisualFeatureTypes> features = new List <VisualFeatureTypes>() { VisualFeatureTypes.Categories, VisualFeatureTypes.Description, VisualFeatureTypes.Faces, VisualFeatureTypes.ImageType, VisualFeatureTypes.Tags, VisualFeatureTypes.Adult, VisualFeatureTypes.Color, VisualFeatureTypes.Brands, VisualFeatureTypes.Objects }; var client = new ComputerVisionClient(new ApiKeyServiceClientCredentials("0d38d688442d4918a4b0d8f0866a0c27")) { Endpoint = "https://cvision-dicoding.cognitiveservices.azure.com/" }; var results = await client.AnalyzeImageAsync(imageUrl, features); foreach (var caption in results.Description.Captions) { return(caption.Text); } return(null); }
/// <summary> /// Analyze the image from an http based image url /// </summary> /// <param name="imageUrl">Http url of image</param> /// <returns></returns> public async Task <ImageAnalysis> AnalyzeImageFromUrlAsync(string imageUrl) { if (!imageUrl.StartsWith("http")) { throw new Exception("Image must be a publicly accessible url. Provide an image url of http or https type"); } List <VisualFeatureTypes> features = new List <VisualFeatureTypes>() { VisualFeatureTypes.Categories, VisualFeatureTypes.Brands, VisualFeatureTypes.Description, VisualFeatureTypes.Faces, VisualFeatureTypes.Tags, VisualFeatureTypes.Objects, VisualFeatureTypes.Adult }; List <Details> details = new List <Details>() { Details.Celebrities, Details.Landmarks }; ImageAnalysis result = await visionClient.AnalyzeImageAsync(imageUrl, features, details); return(result); }
private static async Task AnalyzeRemoteAsync(ComputerVisionClient computerVision, string fileURLWithSAS, CognitiveImageAnalysis cognitiveImageAnalysis) { try { if (ValidateImage(1, fileURLWithSAS)) { cognitiveImageAnalysis.imageAnalysis = await computerVision.AnalyzeImageAsync(fileURLWithSAS, features); } else { cognitiveImageAnalysis.error = "Image Validation Failed for ImageAnalysis API"; } } catch (Exception e) { if (e is ComputerVisionErrorException) { ComputerVisionErrorException ex = (ComputerVisionErrorException)e; cognitiveImageAnalysis.error = ex.Message.ToString() + " due to " + ex.Response.Content; } else { cognitiveImageAnalysis.error = e.Message.ToString(); } } }
public static async Task AnalyzeImageUrl(ComputerVisionClient client, string imageUrl) { Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("ANALYZE IMAGE - URL"); Console.WriteLine(); // Creating a list that defines the features to be extracted from the image. List <VisualFeatureTypes?> features = new List <VisualFeatureTypes?>() { VisualFeatureTypes.Tags }; Console.WriteLine($"Analyzing the image {Path.GetFileName(imageUrl)}..."); Console.WriteLine(); // Analyze the URL image ImageAnalysis results = await client.AnalyzeImageAsync(imageUrl, visualFeatures : features); // Image tags and their confidence score Console.WriteLine("Tags:"); foreach (var tag in results.Tags) { Console.WriteLine($"{tag.Name} {tag.Confidence}"); } Console.WriteLine(); }
private static async Task AnalyzeImageUrl(ComputerVisionClient client, string imageUrl) { var features = new List <VisualFeatureTypes>() { VisualFeatureTypes.Categories, VisualFeatureTypes.Description, VisualFeatureTypes.Faces, VisualFeatureTypes.ImageType, VisualFeatureTypes.Tags, VisualFeatureTypes.Adult, VisualFeatureTypes.Color, VisualFeatureTypes.Brands, VisualFeatureTypes.Objects }; Console.WriteLine($"Analyzing the image {Path.GetFileName(imageUrl)}..."); Console.WriteLine(); // Analyze the URL image var results = await client.AnalyzeImageAsync(imageUrl, features); // Summarizes the image content. Console.WriteLine("Summary:"); foreach (var caption in results.Description.Captions) { var json = JsonConvert.SerializeObject(results, Formatting.Indented); Console.WriteLine($"{caption.Text} with confidence {caption.Confidence}"); } Console.WriteLine(); }
public static async Task <ImageDetails> AnalyzeImageUrl(ComputerVisionClient client, string imageUrl) { var imageDetails = new ImageDetails(); imageDetails.ImageUrl = imageUrl; // Creating a list that defines the features to be extracted from the image. List <VisualFeatureTypes> features = new List <VisualFeatureTypes>() { VisualFeatureTypes.Description, VisualFeatureTypes.Tags }; ImageAnalysis results = await client.AnalyzeImageAsync(imageUrl, features); foreach (var caption in results.Description.Captions) { imageDetails.Description.Captions.Add( new Caption { Text = caption.Text, Confidence = caption.Confidence } ); } foreach (var tag in results.Tags) { imageDetails.Description.Tags.Add( tag.Name ); } return(imageDetails); }
/// <summary> /// Sends a URL to Cognitive Services and performs analysis. /// </summary> /// <param name="imageUrl">The URL of the image to analyze.</param> /// <returns>Awaitable image analysis result.</returns> private async Task <ImageAnalysis> AnalyzeUrlAsync(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"); // // Analyze the URL for all visual features. // Log("Calling ComputerVisionClient.AnalyzeImageAsync()..."); VisualFeatureTypes[] visualFeatures = GetSelectedVisualFeatures(); string language = (_language.SelectedItem as RecognizeLanguage).ShortCode; ImageAnalysis analysisResult = await client.AnalyzeImageAsync(imageUrl, visualFeatures, null, language); return(analysisResult); } // ----------------------------------------------------------------------- // KEY SAMPLE CODE ENDS HERE // ----------------------------------------------------------------------- }
public static async Task AnalyzeImageUrl(ComputerVisionClient client, string imageUrl) { Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("ANALYZE IMAGE - URL"); Console.WriteLine(); // 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 client.AnalyzeImageAsync(imageUrl, features); Console.WriteLine("Objects:"); foreach (var obj in results.Objects) { Console.WriteLine($"{obj.ObjectProperty} with confidence {obj.Confidence} at location {obj.Rectangle.X}, " + $"{obj.Rectangle.X + obj.Rectangle.W}, {obj.Rectangle.Y}, {obj.Rectangle.Y + obj.Rectangle.H}"); } Console.WriteLine(); }
private async void imgPhoto_ImageOpened(object sender, RoutedEventArgs e) { size_image = new Size((imgPhoto.Source as BitmapImage).PixelWidth, (imgPhoto.Source as BitmapImage).PixelHeight); var client = new ComputerVisionClient( new ApiKeyServiceClientCredentials(key), new System.Net.Http.DelegatingHandler[] { }); // need to provide and endpoint and a delegate. client.Endpoint = apiroot; var feature = new VisualFeatureTypes [] { VisualFeatureTypes.Tags, VisualFeatureTypes.Faces, VisualFeatureTypes.Description, VisualFeatureTypes.Adult, VisualFeatureTypes.Categories }; var result = await client.AnalyzeImageAsync(txtLocation.Text, feature); thisresult = result; if (result != null) { DisplayData(result); } ringLoading.IsActive = false; }
/// <summary> /// Sends a URL to Cognitive Services and performs analysis. /// </summary> /// <param name="imageUrl">The URL of the image to analyze.</param> /// <returns>Awaitable image analysis result.</returns> private async Task <string> AnalyzeUrlAsync(string imageUrl, ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken) { string key = "<SERVICE CLIENT KEY>"; var test = new ApiKeyServiceClientCredentials(key); using (var client = new ComputerVisionClient(test) { Endpoint = "https://colourfind.cognitiveservices.azure.com/" }) { VisualFeatureTypes[] visualFeatures = new List <VisualFeatureTypes>() { VisualFeatureTypes.Color, VisualFeatureTypes.Description, VisualFeatureTypes.Tags }.ToArray(); string language = "en"; ImageAnalysis analysisResult; analysisResult = await client.AnalyzeImageAsync(imageUrl, visualFeatures, null, language); string mainColour = analysisResult.Color.DominantColorForeground; return(mainColour); } }
public async Task <ImageAnalysis> Analizer(string urlAnalize) { string subscriptionKey = "eeaf71b7af4b424f8784608593261774"; string subscriptionKey2 = "90fa26632f7c49b8ae8723ae5a46ee40"; string endpoint = "https://computervisionsrg.cognitiveservices.azure.com/"; string ANALYZE_URL_IMAGE = urlAnalize; ComputerVisionClient client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(subscriptionKey)) { Endpoint = endpoint }; 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 client.AnalyzeImageAsync(ANALYZE_URL_IMAGE, visualFeatures : features); return(results); }
private async Task <ImageAnalysis> AnalyzeUrl(string imageUrl) { Log("Calling VisionServiceClient.AnalyzeImageAsync()..."); VisualFeatureTypes?[] visualFeatures = new VisualFeatureTypes?[] { VisualFeatureTypes.Adult, VisualFeatureTypes.Categories, VisualFeatureTypes.Color, VisualFeatureTypes.Description, VisualFeatureTypes.Faces, VisualFeatureTypes.ImageType, VisualFeatureTypes.Tags }; var analysisResult = await VisionServiceClient.AnalyzeImageAsync(imageUrl, visualFeatures); return(analysisResult); }
public static Task <ImageAnalysis> cvResult(string imgurl) { return(cv.AnalyzeImageAsync(imgurl, new List <VisualFeatureTypes?>() { VisualFeatureTypes.Description, VisualFeatureTypes.Objects })); }
public static async Task <ImageAnalysis> AnalyzeImage([ActivityTrigger] string name, ILogger log) { log.LogInformation($"Analyzing {name}."); ComputerVisionClient computerVision = new ComputerVisionClient(new ApiKeyServiceClientCredentials(Environment.GetEnvironmentVariable("CognitiveApiKey")), new System.Net.Http.DelegatingHandler[] { }); computerVision.Endpoint = Environment.GetEnvironmentVariable("CognitiveApiUrl"); return(await computerVision.AnalyzeImageAsync(await GetBlobAccessUrl("uploaded", name, TimeSpan.FromSeconds(60)), analysisFeatures)); }
// Analyze a remote image public Task <ImageAnalysis> AnalyzeRemoteAsync(IComputerVisionClient computerVision, string imageUrl) { if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute)) { throw new Exception($"Invalid Remote Image Url: {imageUrl}"); } return(_computerVision.AnalyzeImageAsync(imageUrl, _features)); }
// Analyze a remote image private async Task analyzeFromUrlAsync(ComputerVisionClient computerVision, string imageUrl, List <VisualFeatureTypes> features) { if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute)) { throw new Exception($"Invalid remote image url:{imageUrl}"); } ImageAnalysis analysis = await computerVision.AnalyzeImageAsync(imageUrl, features); }
public async Task <ActionResult> Upload(HttpPostedFileBase file) { //确认用户选择了图像文件 if (!file.ContentType.StartsWith("image")) { TempData["Message"] = "只有图像文件可以上传"; } else { try { //保存原文件在photos容器中 CloudStorageAccount account = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]); CloudBlobClient client = account.CreateCloudBlobClient(); CloudBlobContainer container = client.GetContainerReference("photos"); CloudBlockBlob photo = container.GetBlockBlobReference(Path.GetFileName(file.FileName)); await photo.UploadFromStreamAsync(file.InputStream); //生成缩略图并保存在 "thumbnails" 容器中 using (var outputStream = new MemoryStream()) { file.InputStream.Seek(0L, SeekOrigin.Begin); var settings = new ResizeSettings { MaxWidth = 192 }; ImageBuilder.Current.Build(file.InputStream, outputStream, settings); outputStream.Seek(0L, SeekOrigin.Begin); container = client.GetContainerReference("thumbnails"); CloudBlockBlob thumbnail = container.GetBlockBlobReference(Path.GetFileName(file.FileName)); await thumbnail.UploadFromStreamAsync(outputStream); //提交图像到azure 视觉服务api接口去分析 ComputerVisionClient vision = new ComputerVisionClient( new ApiKeyServiceClientCredentials(ConfigurationManager.AppSettings["SubscriptionKey"]), new System.Net.Http.DelegatingHandler[] { }); vision.Endpoint = ConfigurationManager.AppSettings["VisionEndpoint"]; VisualFeatureTypes[] features = new VisualFeatureTypes[] { VisualFeatureTypes.Description }; var result = await vision.AnalyzeImageAsync(photo.Uri.ToString(), features); //在blob元数据中记录图像描述和标记 photo.Metadata.Add("Caption", result.Description.Captions[0].Text); for (int i = 0; i < result.Description.Tags.Count; i++) { string key = string.Format("Tag{0}", i); photo.Metadata.Add(key, result.Description.Tags[i]); } await photo.SetMetadataAsync(); } } catch (Exception ex) { TempData["Message"] = ex.Message; } } return(RedirectToAction("Index")); }
public Task <ImageAnalysis> AnalyzeAsync(string imageUrl) { if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute)) { throw new Exception($"Invalid remoteImageUrl: {imageUrl}"); } var analysisResults = _computerVision.AnalyzeImageAsync(imageUrl, Features); return(analysisResults); }
private static async Task <ImageAnalysis> AnalyzeRemoteAsync( ComputerVisionClient computerVision, string imageUrl, ILogger log) { if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute)) { log.LogInformation("\nInvalid remoteImageUrl:\n{0} \n", imageUrl); return(null); } return(await computerVision.AnalyzeImageAsync(imageUrl, features)); }
private static async Task <ImageAnalysis> AnalyseRemoteImage(string imageUrl) { if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute)) { Console.WriteLine( "\nInvalid remoteImageUrl:\n{0} \n", imageUrl); return(null); } return(await _computerVision.AnalyzeImageAsync(imageUrl, _features)); }
public async Task <ImageAnalysis> AnalyzeImageUrl(string imageUrl) { ComputerVisionClient client = await GetClient(); List <VisualFeatureTypes> features = Enum.GetValues(typeof(VisualFeatureTypes)).Cast <VisualFeatureTypes>().ToList(); List <Details> details = Enum.GetValues(typeof(Details)).Cast <Details>().ToList(); ImageAnalysis results = await client.AnalyzeImageAsync(imageUrl, features, details); return(results); }
private static async Task AnalyzeRemoteAsync(ComputerVisionClient computerVision, string imageUrl) { if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute)) { Console.WriteLine("\nInvalid file:\n{0} \n", imageUrl); return; } ImageAnalysis analysis = await computerVision.AnalyzeImageAsync(imageUrl, features); DisplayResults(analysis, imageUrl); }
// Analyze a remote image public static async Task <ImageAnalysis> AnalyzeRemoteAsync( ComputerVisionClient computerVision, string imageUrl, IList <VisualFeatureTypes> features) { if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute)) { Console.WriteLine( "\nInvalid remoteImageUrl:\n{0} \n", imageUrl); throw new FileNotFoundException(imageUrl); } return(await computerVision.AnalyzeImageAsync(imageUrl, features)); }
static async Task CheckForAdultContentAsync(ComputerVisionClient visionClient, string url) { List <VisualFeatureTypes> features = Enum.GetValues(typeof(VisualFeatureTypes)).OfType <VisualFeatureTypes>().ToList(); Console.WriteLine($"Анализируемое изображение {Path.GetFileName(url)}"); Console.WriteLine("---------------"); ImageAnalysis analysis = await visionClient.AnalyzeImageAsync(url, features); Console.WriteLine("--------Запрещенное содержимое-----------"); Console.WriteLine($"Изображение содержит контент для взрослых: {analysis.Adult.IsAdultContent} с достоверностью {analysis.Adult.AdultScore}"); Console.WriteLine($"Изображение содержит расистский контент: {analysis.Adult.IsRacyContent} с достоверностью {analysis.Adult.RacyScore}"); }
// Analyze a remote image private async Task AnalyzeRemoteAsync(string imageUrl) { if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute)) { MessageBox.Show("Invalid remoteImageUrl:\n{0}", imageUrl); return; } ImageAnalysis analysis = await computerVision.AnalyzeImageAsync(imageUrl, features); DisplayResults(analysis, imageUrl); }
static async Task GetTagsAsync(ComputerVisionClient visionClient, string url) { List <VisualFeatureTypes> features = Enum.GetValues(typeof(VisualFeatureTypes)).OfType <VisualFeatureTypes>().ToList(); Console.WriteLine($"Анализируемое изображение {Path.GetFileName(url)}"); Console.WriteLine("---------------"); ImageAnalysis analysis = await visionClient.AnalyzeImageAsync(url, features); foreach (var tag in analysis.Tags) { Console.WriteLine($"{tag.Name} с достоверностью {tag.Confidence}"); } }