/* * END - DETECT OBJECTS - LOCAL IMAGE */ /* * DETECT DOMAIN-SPECIFIC CONTENT * Recognizes landmarks or celebrities in an image. */ public static async Task DetectDomainSpecific(ComputerVisionClient client, string urlImage, string localImage) { Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("DETECT DOMAIN-SPECIFIC CONTENT - URL & LOCAL IMAGE"); Console.WriteLine(); // Detect the domain-specific content in a URL image. DomainModelResults resultsUrl = await client.AnalyzeImageByDomainAsync("landmarks", urlImage); // Display results. Console.WriteLine($"Detecting landmarks in the URL image {Path.GetFileName(urlImage)}..."); var jsonUrl = JsonConvert.SerializeObject(resultsUrl.Result); JObject resultJsonUrl = JObject.Parse(jsonUrl); Console.WriteLine($"Landmark detected: {resultJsonUrl["landmarks"][0]["name"]} " + $"with confidence {resultJsonUrl["landmarks"][0]["confidence"]}."); Console.WriteLine(); // Detect the domain-specific content in a local image. using (Stream imageStream = File.OpenRead(localImage)) { // Change "celebrities" to "landmarks" if that is the domain you are interested in. DomainModelResults resultsLocal = await client.AnalyzeImageByDomainInStreamAsync("celebrities", imageStream); Console.WriteLine($"Detecting celebrities in the local image {Path.GetFileName(localImage)}..."); // Display results. var jsonLocal = JsonConvert.SerializeObject(resultsLocal.Result); JObject resultJsonLocal = JObject.Parse(jsonLocal); Console.WriteLine($"Celebrity detected: {resultJsonLocal["celebrities"][2]["name"]} " + $"with confidence {resultJsonLocal["celebrities"][2]["confidence"]}"); } Console.WriteLine(); }
/// <summary> /// Uploads the image to Cognitive Services and performs analysis against a given domain. /// </summary> /// <param name="imageFilePath">The image file path.</param> /// <param name="domainModel">The domain model for analyzing an image.</param> /// <returns>Awaitable domain-specific analysis result.</returns> private async Task <DomainModelResults> UploadAndAnalyzeInDomainImageAsync(string imageFilePath, ModelDescription domainModel) { // ----------------------------------------------------------------------- // KEY SAMPLE CODE STARTS HERE // ----------------------------------------------------------------------- // // Create Cognitive Services Vision API Service client. // using (var client = new ComputerVisionClient(Credentials) { Endpoint = Endpoint }) { Log("ComputerVisionClient is created"); using (Stream imageFileStream = File.OpenRead(imageFilePath)) { // // Analyze the image for the given domain. // Log("Calling ComputerVisionClient.AnalyzeImageByDomainInStreamAsync()..."); string language = ((RecognizeLanguage)_language.SelectedItem).ShortCode; DomainModelResults analysisResult = await client.AnalyzeImageByDomainInStreamAsync(domainModel.Name, imageFileStream, language); return(analysisResult); } } // ----------------------------------------------------------------------- // KEY SAMPLE CODE ENDS HERE // ----------------------------------------------------------------------- }
// Analyze a local image private static async Task RecognizeDomainSpecificContentFromStreamAsync(ComputerVisionClient computerVision, string imagePath, string specificDomain) { if (!File.Exists(imagePath)) { Console.WriteLine("\nUnable to open or read local image path:\n{0} \n", imagePath); return; } using (Stream imageStream = File.OpenRead(imagePath)) { DomainModelResults analysis = await computerVision.AnalyzeImageByDomainInStreamAsync(specificDomain, imageStream); //change "celebrities" to "landmarks" if that is the domain you are interested in Console.WriteLine(imagePath); DisplayResults(analysis); } }
/* * DETECT DOMAIN-SPECIFIC CONTENT * Recognizes landmarks or celebrities in an image. */ // supported domains "landmarks" and "celebrities" public static async Task <JObject> DetectDomainSpecific(ComputerVisionClient client, Uri imageUri, string domains) { Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("DETECT DOMAIN-SPECIFIC CONTENT"); Console.WriteLine(); JObject resultJsonUrl = null; // Http Urls if (imageUri.Scheme == Uri.UriSchemeHttps || imageUri.Scheme == Uri.UriSchemeHttp) { // Detect the domain-specific content in a URL image. DomainModelResults resultsUrl = await client.AnalyzeImageByDomainAsync(domains, imageUri.ToString()); // Display results. Console.WriteLine($"Detecting {domains} in the URL image {Path.GetFileName(imageUri.AbsolutePath)}..."); var jsonUrl = JsonConvert.SerializeObject(resultsUrl.Result); resultJsonUrl = JObject.Parse(jsonUrl); } else if (imageUri.Scheme == Uri.UriSchemeFile) { // Detect the domain-specific content in a local image. using (Stream imageStream = File.OpenRead(imageUri.AbsolutePath)) { Console.WriteLine($"Detecting {domains} in the local file image {Path.GetFileName(imageUri.AbsolutePath)}..."); DomainModelResults resultsLocal = await client.AnalyzeImageByDomainInStreamAsync(domains, imageStream); // return results. var jsonLocal = JsonConvert.SerializeObject(resultsLocal.Result); resultJsonUrl = JObject.Parse(jsonLocal); } } Console.WriteLine(); return(resultJsonUrl); }