The Computer Vision cognitive service uses pre-trained machine learning models to analyze images and extract information about them.
For example, suppose the fictitious retailer Northwind Traders has decided to implement a “smart store”, in which AI services monitor the store to identify customers requiring assistance, and direct employees to help them. By using the Computer Vision service, images taken by cameras throughout the store can be analyzed to provide meaningful descriptions of what they depict.
In this lab, you’ll use a simple command-line application to see the Computer Vision service in action. The same principles and functionality apply in real-world solutions, such as web sites or phone apps.
Create a Cognitive Services resource
You can use the Computer Vision service by creating either a Computer Vision resource or a Cognitive Services resource.
If you haven’t already done so, create a Cognitive Services resource in your Azure subscription.
- In another browser tab, open the Azure portal at https://portal.azure.com, signing in with your Microsoft account.
- Click the +Create a resource button, search for Cognitive Services, and create a Cognitive Services resource with the following settings:
- Subscription: Your Azure subscription.
- Resource group: Select or create a resource group with a unique name.
- Region: Choose any available region:
- Name: Enter a unique name.
- Pricing tier: S0
- I confirm I have read and understood the notices: Selected.
- Review and create the resource, and wait for deployment to complete. Then go to the deployed resource.
- View the Keys and Endpoint page for your Cognitive Services resource. You will need the endpoint and keys to connect from client applications.
Run Cloud Shell
To test the capabilities of the Computer Vision service, we’ll use a simple command-line application that runs in the Cloud Shell on Azure.
-
In the Azure portal, select the [>_] (Cloud Shell) button at the top of the page to the left of the search box. This opens a Cloud Shell pane at the bottom of the portal, as shown here.
-
The first time you open the cloud shell, you will be prompted to choose the type of shell you want to use (Bash or PowerShell). Select PowerShell.
-
If you are prompted to create storage for your cloud shell, ensure your subscription is specified and select Create storage. Then wait a minute or so for the storage to be created. Eventually, the cloud shell pane will display a command prompt like this:
Note: If you selected Bash, or you had previously opened a Bash cloud shell, you can switch to PowerShell by using the drop-down menu at the top left of the cloud shell pane.
Configure and run a client application
Now that you have a Cloud sShell environment, you can run a simple application that uses the Computer Vision service to analyze an image.
-
In the command shell, enter the following command to download the sample application and save it to a folder called ai-900.
git clone https://github.com/MicrosoftLearning/AI-900-AIFundamentals ai-900
-
The files are downloaded to a folder named ai-900. Now we want to see all of the files in your Cloud Shell storage and work with them. Type the following command into the shell:
code .
Notice how this opens up an editor.
-
In the Files pane on the left, expand ai-900 and select analyze-image.ps1. This file contains some code that uses the Computer Vision service to analyze an image, as shown here:
-
Don’t worry too much about the code, the important thing is that it needs the endpoint URL and either of the keys for your Cognitive Services resource. Copy these from the Keys and Endpoints page for your resource from the Azure portal and paste them into the code editor, replacing the YOUR_ENDPOINT and YOUR_KEY placeholder values respectively.
[!TIP] You may need to use the separator bar to adjust the screen area as you work with the Keys and Endpoint and Editor panes.
After pasting the endpoint and key values, the first two lines of code should look similar to this:
$endpoint="https://resource.cognitiveservices.azure.com/" $key="1a2b3c4d5e6f7g8h9i0j...."
-
At the top right of the editor pane, use the … button to open the menu and select Save to save your changes.
The sample client application will use your Computer Vision service to analyze the following image, taken by a camera in the Northwind Traders store:
-
In the PowerShell pane, enter the following commands to run the code:
cd ai-900 ./analyze-image.ps1 store-cam1.jpg
- Review the results of the image analysis, which include:
- A suggested caption that describes the image.
- A list of objects identified in the image.
- A list of “tags” that are relevant to the image.
-
Now let’s try another image:
To analyze the second image, enter the following command:
./analyze-image.ps1 store-cam2.jpg
-
Review the results of the image analysis for the second image.
-
Let’s try one more:
To analyze the third image, enter the following command:
./analyze-image.ps1 store-cam3.jpg
- Review the results of the image analysis for the third image.
Learn more
This simple app shows only some of the capabilities of the Computer Vision service. To learn more about what you can do with this service, see the Computer Vision page.