Objectives
Pass text recognized by the Cloud Vision API to the Cloud Translation API.
Create and use Cloud Translation glossaries to personalize Cloud Translation API translations.
Create an audio representation of translated text using the Text-to-Speech API.
Costs
Each Google Cloud API uses a separate pricing structure.For pricing details, refer to the Cloud Vision pricing guide, the Cloud Translation pricing guide, and the Text-to-Speech pricing guide.
Before you begin
Make sure that you have:- A project in the Google Cloud console with the Vision API, the Cloud Translation API, and the Text-to-Speech API enabled
- A basic familiarity with Python or NodeJS programming
Setting up client libraries
This tutorial uses Vision, Translation, and Text-to-Speech client libraries.
To install the relevant client libraries, run the following commands from the terminal.
Python
pip install --upgrade google-cloud-vision pip install --upgrade google-cloud-translate pip install --upgrade google-cloud-texttospeech
Node.js
npm install @google-cloud/vision npm install @google-cloud/translate npm install @google-cloud/text-to-speech
Setting up permissions for glossary creation
Creating Translation glossaries requires using a service account key with "Cloud Translation API Editor" permissions.
To set up a service account key with Cloud Translation API Editor permissions, do the following:
Create a service account:
In the Google Cloud console, go to the Service Accounts page.
Select your project.
Click Create Service Account.
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
Optional: In the Service account description field, enter a description for the service account.
Click Create and continue.
Click the Select a role field and select Cloud Translation > Cloud Translation API Editor
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
Download a JSON key for the service account you just created:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, then click Create new key.
Click Create. A JSON key file is downloaded to your computer.
Make sure to store the key file securely, because it can be used to authenticate as your service account. You can move and rename this file however you would like.
Click Close.
In your terminal, set the GOOGLE_APPLICATION_CREDENTIALS variable using the following command. Replace path_to_key with the path to the downloaded JSON file containing your new service account key.
Linux or macOS
export GOOGLE_APPLICATION_CREDENTIALS=path_to_key
Windows
set GOOGLE_APPLICATION_CREDENTIALS=path_to_key
Importing libraries
This tutorial uses the following system imports and client library imports.
Python
Before trying this sample, follow the Python setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Python API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Node.js API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.