Azure Computer Vision Service — An AI Cognitive Service

Amir Mustafa
10 min readJan 9, 2023

→ In this article we will understand the first Artificial Intelligence (AI) and Machine Learning (ML) related service provided by Microsoft Azure.

What is Artificial Intelligence?

The theory and development of computer systems able to perform tasks normally like — human intelligence such as

  • Visual perception,
  • Speech Recognition,
  • Decision making,
  • Translation between languages

Understanding Types of AI:

Strong Artificial Intelligence (i.e. General AI):

→ Intelligence of Human = Intelligence of Machine

→ Predictions say this will take a few decades to be impossible

Narrow Artificial Intelligence (or Weak AI):

→ This one, we will be more focussed now.

→ AI doing specific sets of tasks eg. Self Driving Cars, Programs that can process an image and detect what is there in an image

Eg 1: Google Lens — Detecting the below image

→ For example, this image has a lake, a sky, a skyscraper, etc

→ We also see that each of these images is detected with a certain level of confidence i.e. Water 100% confidence, lake — 95% confidence

→ We also see some descriptions generated.

eg. A city skyline with water — 27% confidence.

Eg 2: Machine Learning:

 → This is also an example of Narrow AI.

 → It focuses on learning from Data (an example).

 → If we want to build a model to predict from an image. The approach we will take is to use thousands of examples with images and tags.

 → Millions of examples with pre-defined tags, we train a model using Azure services.

 → Later we will use the same model to predict what is there in an image.

 → This approach is called Machine Learning i.e. we are learning from data.

Cognitive Services:

→ The goal of Cognitive Service is to make the developer's life easy.

→ We need not have Machine Learning expertise.

→ These services are exposed as simple APIs and Web services

Computer Vision — Azure’s First AI Service:

→ This is a Vision-based API.

→ Vision — Get intelligence from images and videos.

What is Azure’s Cognitive — Computer Vision Service?

→ We provide images and Azure Computer Vision Service will fetch information from the image

→ For example men in suits, goggles, sea, sky, and even landmarks like Eiffel Tower.

Pre-requisites:

→ An Azure Account. We can get our free account here.

→ Video content will also help step by step in addition.

Realtime Demo — Computer Vision API:

→ Open Azure Cloud Portal by clicking here.

→ We will do our hands dirty using Computer Vision API.

→ Search Cognitive Service

→ Now Cognitive Service has many Artificial Intelligence Features such as

a. Vision: Computer Vision, Customer Vision, Face API

b. Decision: Anamoly Detector, Content moderator, Personalizer

c. Azure OpenAI

d. Multipurpose

→ Today we will be focussed on First Vision Service — Computer Vision

→ The first thing we need to know:

Single Purpose:

→ Speech, Language, Vision, Decision, Azure OpenAI,

→ Multiple services use multiple API Keys

Multi Purpose: This feature helps us to add multiple cognitive services to our app

→ So to use Computer Vision API, we can either go to Computer Vision API

→ Multiple Services use Single API Key

→ So to access Computer Vision API, there are two ways:

a. Single Purpose API

Single Purpose API

b. Multi-Purpose API:

→ We will go with Multipurpose, as one API can make use of multiple features eg. Computer Vision, Face API, etc.

→ Click on Create button

→ The first step is to choose your subscription

→ At present in 2023, mostly there are three subscriptions — Free Trial, Pay-as-you-go and the third one is student subscription

→ So for my case subscription is mapped to pay-as-you-go

→ Click Create new Resource Group

→ We can write any resource group name. We should enter any name based on your toolkit eg, machine learning, AI, or Cognitive services

eg. cognitive-service-rg

→ Next we write the name of the instance of the Computer. This should be unique

eg. cognitive-services-happylearnings

→ One important thing to know is this name will appear on the API.

→ We should choose standard Pricing Plan Standard 0. If we are in the free plan we use — Free plan

→ Click on Next: Network button, accepting policy

→ At present, we will ignore other steps as a default setting and keep clicking the Next buttons

→ Click Next: Tags

→ Click Next: Review + create

→ We will reach the Review + Create button page.

→ Click on Create button. It will take some seconds

→ Click on Go to resource

→ Finally we have created our first AI service — Computer

Using Computer Vision:

→ To access the API, we need Endpoint and Keys like sensitive information for the API.

→ This can be found under Keys and Endpoints

→ We get four important — Key1, Key2, Region, and Endpoint

NOTE:

If we feel our keys are compromised — Click Regenerate Key1 and Regenerate Key2 as shown in the screenshot.

How to use it?

→ Now we have Endpoints, and keys. The next question is how can we send an image and fetch Details of the image using Computer Vision API

→ Click here for the API generator's official Microsoft site

→ Here we search our Machine Learning Service — Computer

→ We will click on the latest Computer Vision Service

i.e. Computer Vision API (v3.2) at present. If we see a later version chose that one.

→ Analyse Image is one of the Main APIs that does most of the work

i.e. Category, Brand, Adult, etc

→ The first important thing to know is the region (i.e. location) where API is created.

→ Click Overview, in the location we find eg. East US

→ This is important because we API response will be faster if we fetch from the same region.

→ Now we are going back to the API page. We chose the location in the Azure portal i.e. East US

→ In the name, chose the cognitive service URL.

→ In the Wild Card, we will paste the name of the cognitive service, we created.

Pasting Key:

→ Copy the key from Keys and Endpoint.

→ Let us paste the endpoints and key in a notepad:

→ We paste the key in Ocp-Apim-Subscription-Key key

→ We have to basically add an image URL. Any URL from the internet will be fine.

→ Let us Copy Eiffel Tower from the internet:

→ Hitting the URL:

→ If we paste the URL and click Send button

→ We get a response back with Categories named key.

→ There are more options to know from here — whether the image has known faces, adult content, color, etc

→ All will be comma separated with no space

Endpoint:

https://cognitive-services-happylearnings.cognitiveservices.azure.com/vision/v3.2/analyze?visualFeatures=Categories,Brands,Adult,Color,Description,Faces,ImageType,Objects,Tags&language=en&model-version=latest

Postman:

→ Let us create a request in Postman:

cURL:

curl --location --request POST 'https://cognitive-services-happylearnings.cognitiveservices.azure.com/vision/v3.2/analyze?visualFeatures=Categories,Brands,Adult,Color,Description,Faces,ImageType,Objects,Tags&language=en&model-version=latest' \
--header 'Content-Type: application/json' \
--header 'Ocp-Apim-Subscription-Key: 0cf42672331346b083c554a2330f022e' \
--data-raw '{"url":"https://i.natgeofe.com/k/c41b4f59-181c-4747-ad20-ef69987c8d59/eiffel-tower-night_3x4.jpg"}'

Endpoint:

https://cognitive-services-happylearnings.cognitiveservices.azure.com/vision/v3.2/analyze?visualFeatures=Categories,Brands,Adult,Color,Description,Faces,ImageType,Objects,Tags&language=en&model-version=latest

Body:

{"url":"https://upload.wikimedia.org/wikipedia/commons/5/5e/UBtowers.jpg"}

→ Headers:

Eg 2: Building image:

Image URL:

https://upload.wikimedia.org/wikipedia/commons/5/5e/UBtowers.jpg

Bangalore Building

cURL:

curl --location --request POST 'https://cognitive-services-happylearnings.cognitiveservices.azure.com/vision/v3.2/analyze?visualFeatures=Categories,Brands,Adult,Color,Description,Faces,ImageType,Objects,Tags&language=en&model-version=latest' \
--header 'Content-Type: application/json' \
--header 'Ocp-Apim-Subscription-Key: 0cf42672331346b083c554a2330f022e' \
--data-raw '{"url":"https://upload.wikimedia.org/wikipedia/commons/5/5e/UBtowers.jpg"}'

Eg 3: Get Thumbnail API:

→ We chose our region i.e. East US

→ The output of this URL is a thumbnail, so it asks for the size of the image as output.

→ We get the image in return. The Output of the image is binary i.e. some file.

→ Postman is good with this. Image is parsing

Video:

Closing Thoughts:

In this article, we have learned about Azure’s Computer Vision service. This is the first AI service of Azure that helps with the Vision part. We pass the image URL as an input, and Computer Vision predicts and extracts information from the image.

In the next few articles, we will explore Artificial Intelligence and Machine Learning related to Azure services

Thank you for reading till the end 🙌 . If you enjoyed this article or learned something new, support me by clicking the share button below to reach more people and/or give me a follow on Twitter and subscribe Happy Learnings !! to see some other tips, articles, and things I learn about and share there.

--

--

Amir Mustafa

JavaScript Specialist | Consultant | YouTuber 🎬. | AWS ☁️ | Docker 🐳 | Digital Nomad | Human. Connect with me on https://www.linkedin.com/in/amirmustafa1/