In this article, you will learn how to use the pre-trained models of Azure Computer Vision service for image analysis.
The Computer Vision service provides pre-built, advanced algorithms that process and analyze images and extract text from photos and documents (Optical Character Recognition, OCR). In the previous article, we explored the built-in image analysis capabilities of Azure Computer Vision.
In this article, we will explore the pre-trained models of Azure Computer Vision service for optical character recognition. We will build a simple Python notebook that turns your handwritten documents into digital notes. You will learn how to:
- Provision a Computer Vision resource.
- Use a Computer Vision resource to extract text from photos.
To complete the exercise, you will need to install:
- Python 3,
- Visual Studio Code,
- Jupyter Notebook and Jupyter Extension for Visual Studio Code.
What is Optical Character Recognition?
The Azure Computer Vision OCR service can extract printed and handwritten text from photos and documents. The READ API uses the latest optical character recognition models and works asynchronously. This means that the READ operation requires a three-step process:
- Submit an image to the Computer Vision service.
- Wait for the analysis operation to complete.
- Retrieve the results of the analysis.
Study the following sketch note to learn more about Optical Character Recognition with the Azure Computer Vision READ API.
Create a Computer Vision Resource
To use the Computer Vision service, you can either create a Computer Vision resource or a Cognitive Services resource. If you plan to use Computer Vision along with other cognitive services, such as Text Analytics, you can create a Cognitive Services resource, or else you can create a Computer Vision resource.
In this exercise, you will create a Computer Vision resource.
Sign in to Azure Portal and select Create a resource.
Search for Computer Vision and then click Create.
Create a Computer Vision resource with the following settings:
- Subscription: Your Azure subscription.
- Resource group: Select an existing resource group or create a new one.
- Region: Choose any available region, for example North Europe.
- Name: This would be your custom domain name in your endpoint. Enter a unique name.
- Pricing tier: You can use the free pricing tier (F0) to try the service, and upgrade later to a paid tier.
Select Review + Create and wait for deployment to complete.
Once the deployment is complete, select Go to resource. On the Overview tab, click Manage keys. Save the Key 1 and the Endpoint. You will need the key and the endpoint to connect to your Computer Vision resource from client applications.
Install the Computer Vision library
Install the Azure Cognitive Services Computer Vision SDK for Python package with
Create a new Python Notebook
Create a new Jupyter Notebook, for example image-analysis-demo.ipynb and open it in Visual Studio Code or in your preferred editor.
Want to view the whole notebook at once? You can find it on GitHub .
Import the following llibraries.
1 2 3 4 5 6 7 8
from azure.cognitiveservices.vision.computervision import ComputerVisionClient from msrest.authentication import CognitiveServicesCredentials from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes from PIL import Image import matplotlib.pyplot as plt import matplotlib.patches as patches import time import numpy as np
Then, create variables for your Computer Vision resource. Replace
YOUR_KEYwith Key 1 and
YOUR_ENDPOINTwith your Endpoint.
key = 'YOUR_KEY' endpoint = 'YOUR_ENDPOINT'
Authenticate the client. Create a
ComputerVisionClientobject with your key and endpoint.
computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(key))
Extract handwritten text from photos
First download the images used in the following examples from my GitHub repository .
In the next cell of the notebook, add the following code, which submits the notes1.jpg image to the Computer Vision READ API, retrieves and prints the extracted text.
Display the bounding box of lines
bounding_box is a quadrangle bounding box of a line specified as a list of 8 numbers. The (x, y) coordinates are specified relative to the top-left of the original image.
Add this code to the next cell to display the bounding box of every detected line.
Print detected words with confidence score
Each word in a text line includes bounding box coordinates indicating its position on the image and a confidence value between 0 and 1 inclusive. The following code print the detected words along with their confidence score and display the bounding boxes.
Summary and next steps
In this article, you learned how to use Azure Computer Vision READ API to extract text from photos (Optical Character Recognition, OCR). For more information about using the Azure Cognitive Services Computer Vision SDK for Python package, see the computervision Package documentation .
If you have finished learning, you can delete the resource group from your Azure subscription:
In the Azure Portal , select Resource groups on the right menu and then select the resource group that you have created.
Click Delete resource group.