Translate your notes with Azure Translator and Python

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.

Azure Translator sketch note
Azure Translator

You can find more information and how-to-guides about Translator on Microsoft Learn and Microsoft Docs.

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.

Key and region of Azure 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.

  1. Import the following llibraries.

    1
    2
    3
    
    from dotenv import load_dotenv
    import os
    import requests
    
  2. 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

Define the detect_language function which automatically detects the language of the source text and returns the language code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

Translate text

Define the translate function which translates the given text from the source_language into the target_language and returns the translated text.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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:

1
2
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.

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 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# 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])

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.

Clean-up

If you have finished learning, you can delete the resource group from your Azure subscription:

  1. In the Azure Portal, select Resource groups on the right menu and then select the resource group that you have created.

  2. Click Delete resource group.

You May Also Like