/* * 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> /// Sends a URL to Cognitive Services and performs analysis against a given domain. /// </summary> /// <param name="imageUrl">The URL of the image to analyze.</param> /// <param name="domainModel">The domain model to analyze against.</param> /// <returns>Awaitable domain-specific analysis result.</returns> private async Task <DomainModelResults> AnalyzeInDomainUrlAsync(string imageUrl, 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"); // // Analyze the URL against the given domain. // Log("Calling ComputerVisionClient.AnalyzeImageByDomainAsync()..."); string language = (_language.SelectedItem as RecognizeLanguage).ShortCode; DomainModelResults analysisResult = await client.AnalyzeImageByDomainAsync(domainModel.Name, imageUrl, language); return(analysisResult); } // ----------------------------------------------------------------------- // KEY SAMPLE CODE ENDS HERE // ----------------------------------------------------------------------- }
// Analyze a remote image private static async Task RecognizeDomainSpecificContentFromUrlAsync(ComputerVisionClient computerVision, string imageUrl, string specificDomain) { if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute)) { Console.WriteLine("\nInvalid remote image url:\n{0} \n", imageUrl); return; } DomainModelResults analysis = await computerVision.AnalyzeImageByDomainAsync(specificDomain, imageUrl); //change the first parameter to "landmarks" if that is the domain you are interested in Console.WriteLine(imageUrl); 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); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { string cognitive_service_key = Environment.GetEnvironmentVariable("cognitive_service_key"); string cognitive_service_endpoint = Environment.GetEnvironmentVariable("cognitive_service_endpoint"); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); string imageURL = data.imageurl; //imageURL = "https://www.thehansindia.com/assets/9583_rahul-modi.jpg"; dynamic result = new JObject(); var credentials = new ApiKeyServiceClientCredentials(cognitive_service_key); ComputerVisionClient computerVision = new ComputerVisionClient(credentials, new System.Net.Http.DelegatingHandler[] { }); // Specify the Azure region computerVision.Endpoint = cognitive_service_endpoint; // Analyzing image from remote URL if (!Uri.IsWellFormedUriString(imageURL, UriKind.Absolute)) { log.LogError( "\nInvalid remoteImageUrl:\n{0} \n", imageURL); log.LogInformation("invalid image URL provided."); } else { ImageAnalysis analysis = new ImageAnalysis(); try { analysis = await computerVision.AnalyzeImageAsync(imageURL, features); // Getting caption result.caption = ""; if (analysis.Description.Captions.Count != 0) { result.caption = analysis.Description.Captions[0].Text; } // Getting faces dynamic faces = new JArray(); dynamic celebrities = new JArray(); if (analysis.Faces.Count != 0) { foreach (var face in analysis.Faces) { dynamic faceObject = new JObject(); faceObject.rectangle = $"({face.FaceRectangle.Left.ToString()}, " + $"{face.FaceRectangle.Top.ToString()}, " + $"{face.FaceRectangle.Height.ToString()}, " + $"{face.FaceRectangle.Width.ToString()})"; faceObject.age = face.Age; faceObject.gender = face.Gender.ToString(); faces.Add(faceObject); } var celebRecognition = await computerVision.AnalyzeImageByDomainAsync("celebrities", imageURL); dynamic celebResult = JsonConvert.DeserializeObject(celebRecognition.Result.ToString()); if (celebResult.celebrities.Count > 1) { foreach (var celeb in celebResult.celebrities) { celebrities.Add(celeb); } } } result.faces = faces; result.celebrities = celebrities; // Getting categories dynamic categories = new JArray(); if (analysis.Categories.Count != 0) { foreach (var category in analysis.Categories) { categories.Add(category.Name); } } result.categories = categories; // Getting brands dynamic brands = new JArray(); if (analysis.Brands.Count != 0) { foreach (var brand in analysis.Brands) { brands.Add(brand.Name); } } result.brands = brands; // Getting objects dynamic objects = new JArray(); if (analysis.Objects.Count != 0) { foreach (var objectItem in analysis.Objects) { dynamic objObject = new JObject(); objObject.name = objectItem.ObjectProperty; objObject.rectangle = $"({objectItem.Rectangle.X.ToString()}, " + $"{objectItem.Rectangle.Y.ToString()}, " + $"{objectItem.Rectangle.H.ToString()}, " + $"{objectItem.Rectangle.W.ToString()})"; objects.Add(objObject); } } result.objects = objects; // Getting tags dynamic tags = new JArray(); if (analysis.Tags.Count != 0) { foreach (var tag in analysis.Tags) { tags.Add(tag.Name); } } result.tags = tags; } catch (Exception ex) { string exception = ex.Message; } } return(imageURL != null ? (ActionResult) new OkObjectResult($"{result.ToString()}") : new BadRequestObjectResult("{ \"error\": \"Please pass a valid image URL in the request\"")); }