In this blog post, you will deploy an AI Flask application app to Azure App Service using Visual Studio Code.
Welcome to part 2 of the “Digitize and translate your notes with Azure Cognitive Services and Python” series. In the previous blog post, you explored the pre-trained models of Azure Computer Vision for Optical Character Recognition and extracted handwritten text from an image using the READ API and the Computer Vision SDK for Python.
In this article, you will translate the extracted text from your handwritten notes using Translator. You will:
- Understand how the Azure Translator works.
- Create a Python script to translate text.
To complete the exercise, you will need to install:
- Python 3, and
- Visual Studio Code.
What is Translator?
Translator is a cloud-based service that is part of the Azure Cognitive Services and enables you to perform language translation and other language-related operations.
Study the following sketch note to learn more about the capabilities of the Translator service.
Translate text with Azure Translator and Python
Create a configuration file
Create a configuration file and add the authentication key for your Cognitive Services resource, and the location in which the service is deployed. You can copy both values from the Keys and Endpoint page of your Cognitive Services resource.
Create a new Python script
Create a new Python script, for example translator-demo.py and open it in Visual Studio Code or in your preferred editor.
Want to view the whole code at once? You can find it on GitHub.
Import the following llibraries.
1 2 3
from dotenv import load_dotenv import os import requests
Add the following code to load the Cognitive Services key and region from the configuration file and specify the endpoint.
1 2 3 4
load_dotenv() key = os.getenv('COG_SERVICE_KEY') region = os.getenv('COG_SERVICE_REGION') endpoint = 'https://api.cognitive.microsofttranslator.com'
detect_language function which automatically detects the language of the source text and returns the language code.
translate function which translates the given text from the
source_language into the
target_language and returns the translated text.
If you don’t include the
from parameter in your translation request, the Translator will attempt to detect the language of the given text.
Add the following code and run the script:
Challenge: Modify the
translate function so that it returns the translation of the given text into two or more language. If you need some help, you can check the challenge.py script in my GitHub repo.
Extract and translate text from handwritten notes
In this section, you will build a Python app that uses the READ API to extract handwritten text from notes and then translates the extracted text using the Translator service.
First download the images used in the following examples from my GitHub repository.
Next, create a new Python script, for example read-translate.py and use the
get_text function from the previous post and the
translate functions you wrote previously to extract and translate the text of the given images.
Add the following lines of code in the
main function to test your app:
Summary and next steps
In this article, you learned how to use Azure Translator to detect the language of a given text and translate text from the source language into one or more languages. Then you built a Python app that uses the Azure Computer Vision READ API to extract text from handwritten notes and the Translator service to translate the extracted text.
In the next article, you will learn how to build and deploy a Flask AI web app that uses the READ API and the Azure Translator.
Check out the other parts of the “Digitize and translate your notes with Azure Cognitive Services and Python” series:
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.