The Azure computer vision API can be used to retrieve information about an image including tags, descriptions and categories along with their associated confidence scores.

The Microsoft quickstart guide to using this is here and my releated code is below.

This retrieves an image file from Azure Storage and streams it to the API, this could also work by sending the URL of the image.

The NuGet package Microsoft.Azure.CognitiveServices.Vision.ComputerVision needs to be installed to allow access to the API.

public async Task<IActionResult> Score(int id)
{
	// Get user
	User user = Common.GetUser(this.User.Identity.Name, _config.GetConnectionString("DataConnection"));

	// Get bake
	Models.Bake bake = Common.GetBakeJson(id, user.UserId, _config.GetConnectionString("DataConnection"));

	ComputerVisionClient computerVision = new ComputerVisionClient(new ApiKeyServiceClientCredentials(_config["Keys:ComputerVision"]));
	List<VisualFeatureTypes> visualFeatures = new List<VisualFeatureTypes>()
	{
		VisualFeatureTypes.Adult,
		VisualFeatureTypes.Categories,
		VisualFeatureTypes.Color,
		VisualFeatureTypes.Description,
		VisualFeatureTypes.Faces,
		VisualFeatureTypes.ImageType,
		VisualFeatureTypes.Tags
	};
	computerVision.Endpoint = "https://northeurope.api.cognitive.microsoft.com";

	// Get blob stream
	// Retrieve a reference to a container.
	CloudBlobContainer container = new CloudBlobContainer(new Uri(_config.GetConnectionString("ContainerConnection")));
	CloudBlockBlob blob = container.GetBlockBlobReference(bake.ImageBlob);

	using (Stream imageStream = new MemoryStream())
	{
		await blob.DownloadToStreamAsync(imageStream);
		imageStream.Seek(0, SeekOrigin.Begin);
		ImageAnalysis imageAnalysis = await computerVision.AnalyzeImageInStreamAsync(imageStream, visualFeatures);
		ViewData["ImageAnalysis"] = imageAnalysis;
	}

	return View(bake);
}
	

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *