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.
Hi, I am Foteini Savvidou, a Gold Microsoft Learn Student Ambassador!
I am an undergraduate Electrical and Computer Engineering student at Aristotle University of Thessaloniki (Greece) interested in AI, cloud technologies and biomedical engineering. Always passionate about teaching and learning new things, I love helping people expand their technical skills through organizing workshops and sharing articles on my blog.
In this article, you will translate the extracted text from your handwritten notes using Translator. You will:
To complete the exercise, you will need to install:
You will also need an Azure subscription. If you don’t have one, you can sign up for an Azure free account. If you are a student, you can apply for an Azure for Students Subscription.
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.
You can find more information and how-to-guides about Translator on Microsoft Learn and Microsoft Docs.
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, 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.
from dotenv import load_dotenv
import os
import requests
load_dotenv()
key = os.getenv('COG_SERVICE_KEY')
region = os.getenv('COG_SERVICE_REGION')
endpoint = 'https://api.cognitive.microsofttranslator.com'
Define the detect_language
function which automatically detects the language of the source text and returns the language code.
def detect_language(text, key, region, endpoint):
# Use the Translator detect function
path = '/detect'
url = endpoint + path
# Build the request
params = {
'api-version': '3.0'
}
headers = {
'Ocp-Apim-Subscription-Key': key,
'Ocp-Apim-Subscription-Region': region,
'Content-type': 'application/json'
}
body = [{
'text': text
}]
# Send the request and get response
request = requests.post(url, params=params, headers=headers, json=body)
response = request.json()
# Get language
language = response[0]["language"]
# Return the language
return language
Define the translate
function which translates the given text from the source_language
into the target_language
and returns the translated text.
def translate(text, source_language, target_language, key, region, endpoint):
# Use the Translator translate function
url = endpoint + '/translate'
# Build the request
params = {
'api-version': '3.0',
'from': source_language,
'to': target_language
}
headers = {
'Ocp-Apim-Subscription-Key': key,
'Ocp-Apim-Subscription-Region': region,
'Content-type': 'application/json'
}
body = [{
'text': text
}]
# Send the request and get response
request = requests.post(url, params=params, headers=headers, json=body)
response = request.json()
# Get translation
translation = response[0]["translations"][0]["text"]
# Return the translation
return translation
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:
print(detect_language('Hello world!', key, region, endpoint))
print(translate('Hello world!', 'en', 'el', key, region, endpoint))
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.
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 detect_language
and 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:
# Analyze each image file in the images folder
images_folder = os.path.join (os.path.dirname(os.path.abspath(__file__)), "images")
for file_name in os.listdir(images_folder):
# Extract text
print("\n" + "="*12)
read_image_path = os.path.join (images_folder, file_name)
text = get_text(read_image_path, computervision_client)
#print(text)
# Detect the language
language = detect_language(text, key, region, endpoint)
print("Language:", language)
# Translate
target_lang = ["el"]
results = translate(text, language, target_lang, key, region, endpoint)
for i in range(len(results)):
print("\n" + "-"*12)
print(target_lang[i] + ":", results[i])
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.
If you are interested in learning more about Azure Translator, check out these Microsoft Learn modules:
In the next articles, you will learn how to build and deploy a Flask AI web app that uses the READ API and the Azure Translator.
If you have finished learning, you can delete the resource group from your Azure subscription:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.