This recipe shows you how to use the Cloud Vision API together with the Google Translate API using Cloud Pub/Sub as a message bus.
View the source code or the tutorial.
- Image is uploaded to Cloud Storage with text in any language (text in the image itself).
- Cloud Function is triggered, uses the Vision API to extract the text, and the Translate API to detect the language.
- For all languages we're translating into (except the language of the text), publish a message to ther translate topic.
- For the language that matches the language of the text, bypass translation and publish to the save topic.
- Cloud Function is triggered and uses the Translate API to translate the message into various languages, then publishes each translation to the save topic.
- Cloud Function is triggered and saves text to Cloud Storage.
- Translated text from the original source image is downloaded.
-
Follow the Cloud Functions quickstart guide to setup Cloud Functions for your project.
-
Enable the Vision API and the Translate API.
-
Clone this repository:
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git cd nodejs-docs-samples/functions/ocr -
Create a file in the
appfolder calledconfig.jsonwith the following contents:{ "TRANSLATE_API_KEY": "[YOUR_API_KEY]", "RESULT_TOPIC": "[RESULT_TOPIC_NAME]", "RESULT_BUCKET": "[RESULT_BUCKET_NAME]", "TRANSLATE_TOPIC": "[TRANSLATE_TOPIC_NAME]", "TRANSLATE": true, "TO_LANG": ["en", "fr", "es", "ja", "ru"] }- Replace
[YOUR_API_KEY]with your Translate API key. - Replace
[RESULT_TOPIC_NAME]with a topic name used for saving results. - Replace
[RESULT_BUCKET_NAME]with a bucket name used for saving results. - Replace
[TRANSLATE_TOPIC_NAME]with a topic name used for translating results.
- Replace
-
Deploy the
processImagefunction with a GCS trigger:gcloud alpha functions deploy ocr-extract --bucket [YOUR_BUCKET_NAME] --trigger-gs-uri [YOUR_UPLOAD_BUCKET_NAME] --source app --entry-point processImage- Replace
[YOUR_BUCKET_NAME]with the name of your staging Cloud Storage Bucket. - Replace
[YOUR_UPLOAD_BUCKET_NAME]with the name of your Cloud Storage Bucket where you will be uploading images.
- Replace
-
Deploy the
translateTextfunction with a Pub/Sub trigger:gcloud alpha functions deploy ocr-translate --bucket [YOUR_BUCKET_NAME] --trigger-topic [YOUR_TRANSLATE_TOPIC_NAME] --source app --entry-point translateText- Replace
[YOUR_BUCKET_NAME]with the name of your staging Cloud Storage Bucket. - Replace
[YOUR_TRANSLATE_TOPIC_NAME]with the name of your Pub/Sub topic with which translations will be triggered.
- Replace
-
Deploy the
saveResultfunction with a Pub/Sub trigger:gcloud alpha functions deploy ocr-save --bucket [YOUR_BUCKET_NAME] --trigger-topic [YOUR_RESULT_TOPIC_NAME] --source app --entry-point saveResult- Replace
[YOUR_BUCKET_NAME]with the name of your staging Cloud Storage Bucket. - Replace
[YOUR_RESULT_TOPIC_NAME]with the name of your Pub/Sub topic with which saving of results will be triggered.
- Replace
-
Upload a sample image:
gsutil cp images/menu.jpg gs://[YOUR_UPLOAD_BUCKET_NAME]- Replace
[YOUR_UPLOAD_BUCKET_NAME]with the name of your Cloud Storage Bucket where you will be uploading images.
- Replace
-
Watch the logs to make sure the executions have completed
gcloud alpha functions get-logs --limit 100 -
Pull the extracted text from the bucket and pipe to standard out
gsutil cat gs://[YOUR_RESULT_BUCKET_NAME]/menu_to_en.txt- Replace
[YOUR_RESULT_BUCKET_NAME]with the name of your Cloud Storage Bucket where translated text will be saved.
- Replace
