
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Create an API Key
/ 30
Upload image to a bucket
/ 30
Analyzing the image's text with the Natural Language API
/ 40
In this lab you'll explore the power of machine learning by using multiple machine learning APIs together. Start with Cloud Vision API's text detection method to make use of Optical Character Recognition (OCR) to extract text from images. Then, learn how to translate that text with the Translation API and analyze it with the Natural Language API.
In this lab, you will:
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details pane.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
Click Activate Cloud Shell at the top of the Google Cloud console.
Click through the following windows:
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
Since you'll be using curl
to send a request to the Vision API, generate an API key to pass in your request URL.
To create an API key, navigate to: Navigation Menu > APIs & services > Credentials.
Click + Create Credentials.
In the drop down menu, select API key.
Next, copy the key you just generated and click Close.
Now save the API key to an environment variable to avoid having to insert the value of your API key in each request.
Run the following in Cloud Shell, replacing <your_api_key>
with the key you just copied:
Click Check my progress to verify your performed task.
There are two ways to send an image to the Vision API for image detection: by sending the API a base64 encoded image string, or passing it the URL of a file stored in Cloud Storage. For this lab you'll create a Cloud Storage bucket to store your images.
Navigate to the Navigation menu > Cloud Storage browser in the Console, then click Create bucket.
Give your bucket a unique name:
After naming your bucket, click Choose how to control access to objects.
Uncheck the box for Enforce public access prevention on this bucket.
Choose Fine-grained under Access Control and click Create.
Next you'll allow the file to be viewed publicly while keeping the access to the bucket private.
Select Edit access.
Click Add Entry and set the following:
You'll now see that the file has public access.
Now that you have the file in your bucket, you're ready to create a Vision API request, passing it the URL of this picture.
Click Check my progress to verify your performed task.
ocr-request.json
file, then add the code below to the file, replacing my-bucket-name with the name of the bucket you created. You can create the file using one of your preferred command line editors (nano
, vim
, emacs
) or click the pencil icon to open the code editor in Cloud Shell:ocr-request.json
file:You're going to use the TEXT_DETECTION feature of the Cloud Vision API. This will run optical character recognition (OCR) on the image to extract text.
curl
:The first part of your response should look like the following:
The OCR method is able to extract lots of text from the image.
The first piece of data you get back from textAnnotations
is the entire block of text the API found in the image. This includes:
Then you get an object for each word found in the text with a bounding box for that specific word.
Unless you speak French you probably don't know what this says. The next step is translation.
curl
command to save the response to an ocr-response.json
file so it can be referenced later:The Translation API can translate text into 100+ languages. It can also detect the language of the input text. To translate the French text into English, pass the text and the language code for the target language (en-US) to the Translation API.
translation-request.json
file and add the following to it:q
is where you'll pass the string to translate.
Save the file.
Run this Bash command in Cloud Shell to extract the image text from the previous step and copy it into a new translation-request.json
(all in one command):
translation-response.json
file:Now you can understand more of what the sign said!
In the response:
translatedText
contains the resulting translationdetectedSourceLanguage
is fr
, the ISO language code for French.The Translation API supports 100+ languages, all of which are listed in the Language support reference.
In addition to translating the text from our image, you might want to do more analysis on it. That's where the Natural Language API comes in handy. Onward to the next step!
The Natural Language API helps you understand text by extracting entities, analyzing sentiment and syntax, and classifying text into categories. Use the analyzeEntities
method to see what entities the Natural Language API can find in the text from your image.
nl-request.json
file with the following:In the request, you're telling the Natural Language API about the text you're sending:
type: supported type values are PLAIN_TEXT
or HTML
.
content: pass the text to send to the Natural Language API for analysis. The Natural Language API also supports sending files stored in Cloud Storage for text processing. To send a file from Cloud Storage, replace content
with gcsContentUri
and use the value of the text file's uri in Cloud Storage.
encodingType: tells the API which type of text encoding to use when processing the text. The API will use this to calculate where specific entities appear in the text.
The nl-request.json
file now contains the translated English text from the original image. Time to analyze it!
analyzeEntities
endpoint of the Natural Language API with this curl
request:If you scroll through the response you can see the entities the Natural Language API found:
For entities that have a wikipedia page, the API provides metadata including the URL of that page along with the entity's mid
. The mid
is an ID that maps to this entity in Google's Knowledge Graph. To get more information on it, you could call the Knowledge Graph API, passing it this ID. For all entities, the Natural Language API tells us the places it appeared in the text (mentions
), the type
of entity, and salience
(a [0,1] range indicating how important the entity is to the text as a whole). In addition to English, the Natural Language API also supports the languages listed in the Language Support reference.
Looking at this image it's relatively easy to pick out the important entities, but if you had a library of thousands of images this would be much more difficult. OCR, translation, and natural language processing can help to extract meaning from large datasets of images.
Click Check my progress to verify your performed task.
You've learned how to combine 3 different machine learning APIs: the Vision API's OCR method extracted text from an image, then the Translation API translated that text to English and the Natural Language API to found entities in that text. You can use these APIs together to extract meaning from large datasets of images.
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated October 22, 2024
Lab Last Tested October 22, 2024
Copyright 2025 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
此内容目前不可用
一旦可用,我们会通过电子邮件告知您
太好了!
一旦可用,我们会通过电子邮件告知您
One lab at a time
Confirm to end all existing labs and start this one