Learn how to do Image Recognition with Cognitive Services and ASP.NET

Angelos Petropoulos

With all the talk about artificial intelligence (AI) and machine learning (ML) doing crazy things, it’s easy to be left wondering, “what are practical ways I can use this today?” It turns out there are some extremely easy ways to try this today. In this post, I’ll walk through how to detect faces, gender, ages, and hair color in photos, by adding only a few lines of code to an ASP.NET app. Images will be uploaded and shown in an image gallery built with ASP.NET, images will be hosted in Azure Storage, and Azure Cognitive Services will be used to analyze the images. The full application is available on GitHub. To begin, clone the repository on your machine.

What we’ll build Here’s what the recognized photos can look like when displayed in a web browser. Note how the image and metadata generated by Azure Cognitive Services is displayed alongside it.

A sample image of the application running, showing a woman whose age and gender have been estimated by Cognitive Services.

Set up prerequisites with Visual Studio and Azure To begin, make sure you’ve installed Visual Studio 2017 with the ASP.NET and web workload. This will provide everything you need to build and run the app yourself. Next, set up the Azure prerequisites. First, ensure you have an Azure account. If not, you can sign up for an

Azure free account, which will give you a $200 credit towards anything. Next, create a Storage account, through the Azure Portal: You’ll need to create the Storage resource: An image of the Azure Portal, showing how to create a Storage resource. After creating the resource, you’ll need to create the storage account for your resource with the default settings: An image of the Create Storage Account page in the Azure Portal with default settings selected. Finally, create a Cognitive Services resource through the Azure portal: An image showing how to create a Cognitive Services resource. Once you’ve set that up, you’re ready to start hacking away at the sample app!

Explore the codebase Open the project in Visual Studio 2017 if you haven’t already. The application is an ASP.NET MVC app. It does three major things: The first major operation is uploading an image to Azure Blob storage, analyzing the image using Azure Cognitive Services, and uploading image metadata generated from Cognitive Services back to Blob Storage.

The second major operation is to snag images and their associated metadata from Blob Storage. The UI simply wires up these images to a page with an upload button.

Add your API keys Modify the Web.config file to include your Cognitive Services URL and Cognitive Services API key. Look for this file:

Your Cognitive Services URL and API keys can be found in the dashboard for your Cognitive Services resource in the Azure Portal here: The connection strings for your Azure Storage resource can be found in the Azure Portal under Access Keys: An image showing how to access the Azure Storage Access Keys in the Azure Portal. Once you have entered your information in your Web.Config file, you’ll be good to go! To learn more about how to best work with keys and other information in a development, see Best Practices for Deploying Passwords and other Sensitive Data to ASP.NET and Azure.

Run the application and add some images Now that everything is set up and configuration has been set up locally, you can run the application on your machine! Press

F5 to debug and see how everything works. I recommend that you set a breakpoint in the Upload controller action (HomeController.cs, line 32), so that you can step through each operation as you upload a new image. In the opened browser, upload an image to see what happens! If you want to see images show up in Azure blobs when running the app, you can do so with Cloud Explorer (View -> Cloud Explorer). You may need to log in first, but after that, you can navigate to your created Storage Account and see all of your Blobs under Blob Containers: An image showing Visual Studio Cloud Explorer and browsing live Blobs in Azure. In this example, I’ve uploaded three images to my container called “images”. The web app also uploaded a json file with image metadata for each image.

Publish to Azure and impress your friends with your use of AI You can publish the entire application to Azure App Service. Right-click on your project and select “Publish”. Next, select App Service and continue. You can create one right in the Visual Studio UI:

An image showing how you can publish to Azure from Visual Studio 2017. Finally, click “Create” and it will create all the Azure resources you need and publish your app! After that process completes (it should take a minute or two), your browser will open with your application running entirely in Azure.

Next steps And that’s it! Try exploring other, interesting things you can do with Cognitive services. Some fun things to try, without needing to add support for any services or read other tutorials:


Discussion is closed.

Feedback usabilla icon