瞭解如何在 Google Cloud中執行光學字元辨識 (OCR)。本教學課程示範如何將圖片檔上傳至 Cloud Storage、使用 Cloud Vision API 從圖片中擷取文字、透過 Google Cloud Translation API 翻譯文字,以及將翻譯內容存回 Cloud Storage。Pub/Sub 可將各項工作排入佇列,並觸發正確的 Cloud Run functions 以執行工作。
如要進一步瞭解如何傳送文字偵測 (OCR) 要求,請參閱「偵測圖片中的文字」、「偵測圖片中的手寫內容」或「偵測檔案中的文字 (PDF/TIFF)」。
目標
- 編寫及部署數個背景 Cloud Run functions。
- 將圖片上傳至 Cloud Storage。
- 擷取、翻譯及儲存包含在上傳圖片中的文字。
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
- Cloud Run functions
- Pub/Sub
- Cloud Storage
- Cloud Translation API
- Cloud Vision
如要根據預測用量估算費用,請使用 Pricing Calculator。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Functions, Cloud Build, Cloud Pub/Sub, Cloud Storage, Cloud Translation, and Cloud Vision APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Functions, Cloud Build, Cloud Pub/Sub, Cloud Storage, Cloud Translation, and Cloud Vision APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init - 準備開發環境。
如已安裝 gcloud CLI,請執行下列指令進行更新:
gcloud components update
以圖表呈現資料流程
OCR 教學課程應用程式中的資料流程包含以下幾個步驟:
- 將包含任何語言文字的圖片上傳至 Cloud Storage。
- 觸發 Cloud Run 函式,使用 Vision API 擷取文字並偵測原文語言。
- 將訊息發布至 Pub/Sub 主題,藉此將文字排入翻譯佇列。系統會為每個與原文語言不同的譯文語言建立翻譯佇列。
- 如果譯文語言與原文語言相符,則會略過翻譯佇列,並將文字直接傳送至結果佇列,也就是另一個 Pub/Sub 主題。
- Cloud Run 函式會使用 Translation API 譯出翻譯佇列中的文字。翻譯結果會傳送至結果佇列。
- 另一個 Cloud Run 函式會將結果佇列中的翻譯文字儲存至 Cloud Storage。
- 翻譯結果會以文字檔案的形式儲存在 Cloud Storage 中,每份翻譯各有一個檔案。
以下可能有助於透過圖表呈現的方式瞭解步驟:
準備應用程式
建立用來上傳圖片的 Cloud Storage bucket,其中
YOUR_IMAGE_BUCKET_NAME是全域不重複的 bucket 名稱:gcloud storage buckets create gs://
YOUR_IMAGE_BUCKET_NAME建立用來儲存文字翻譯的 Cloud Storage bucket,其中
YOUR_RESULT_BUCKET_NAME是全域不重複的 bucket 名稱:gcloud storage buckets create gs://
YOUR_RESULT_BUCKET_NAME建立用來發布翻譯要求的 Pub/Sub 主題,其中
YOUR_TRANSLATE_TOPIC_NAME是翻譯要求主題的名稱:gcloud pubsub topics create
YOUR_TRANSLATE_TOPIC_NAME建立 Pub/Sub 主題,將翻譯完成的結果發布至該主題,其中
YOUR_RESULT_TOPIC_NAME是翻譯結果主題的名稱:gcloud pubsub topics create
YOUR_RESULT_TOPIC_NAME將應用程式存放區範例複製到本機電腦中:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
您也可以下載 zip 格式的範例檔案,然後將檔案解壓縮。
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
您也可以下載 zip 格式的範例檔案,然後將檔案解壓縮。
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
您也可以下載 zip 格式的範例檔案,然後將檔案解壓縮。
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
您也可以下載 zip 格式的範例檔案,然後將檔案解壓縮。
變更為包含 Cloud Run 函式程式碼範例的目錄:
Node.js
cd nodejs-docs-samples/functions/ocr/app/
Python
cd python-docs-samples/functions/ocr/app/
Go
cd golang-samples/functions/ocr/app/
Java
cd java-docs-samples/functions/ocr/ocr-process-image/
瞭解程式碼
匯入依附元件
應用程式必須匯入數個依附元件,才能與 Google Cloud Platform 服務通訊:
Node.js
Python
Go
Java
處理圖片
下列函式會從 Cloud Storage 中讀取上傳的圖片檔案,並呼叫函式來偵測圖片中是否包含文字:
Node.js
Python
Go
Java
下列函式會使用 Vision API 從圖片中擷取文字,並將文字排入翻譯佇列中:
Node.js
Python
Go
Java
翻譯文字
下列函式會翻譯擷取的文字,並將翻譯的文字排入存回 Cloud Storage 的佇列中:
Node.js
Python
Go
Java
儲存翻譯
最後,下列函式會接收翻譯的文字,並將文字存回 Cloud Storage:
Node.js
Python
Go
Java
部署函式
如要使用 Cloud Storage 觸發條件部署圖片處理函式,請在包含程式碼範例的目錄中執行下列指令 (如果是 Java,則是在
pom.xml檔案中執行):Node.js
gcloud functions deploy ocr-extract \ --runtime nodejs22 \
--trigger-bucket YOUR_IMAGE_BUCKET_NAME \
--entry-point processImage \
--set-env-vars "^:^GCP_PROJECT=YOUR_GCP_PROJECT_ID:TRANSLATE_TOPIC=YOUR_TRANSLATE_TOPIC_NAME:RESULT_TOPIC=YOUR_RESULT_TOPIC_NAME:TO_LANG=es,en,fr,ja"使用
--runtime旗標指定 Node.js 支援版本的執行階段 ID,執行函式。Python
gcloud functions deploy ocr-extract \ --runtime python312 \
--trigger-bucket YOUR_IMAGE_BUCKET_NAME \
--entry-point process_image \
--set-env-vars "^:^GCP_PROJECT=YOUR_GCP_PROJECT_ID:TRANSLATE_TOPIC=YOUR_TRANSLATE_TOPIC_NAME:RESULT_TOPIC=YOUR_RESULT_TOPIC_NAME:TO_LANG=es,en,fr,ja"使用
--runtime旗標指定 Python 支援版本的執行階段 ID,執行函式。Go
gcloud functions deploy ocr-extract \ --runtime go121 \
--trigger-bucket YOUR_IMAGE_BUCKET_NAME \
--entry-point ProcessImage \
--set-env-vars "^:^GCP_PROJECT=YOUR_GCP_PROJECT_ID:TRANSLATE_TOPIC=YOUR_TRANSLATE_TOPIC_NAME:RESULT_TOPIC=YOUR_RESULT_TOPIC_NAME:TO_LANG=es,en,fr,ja"Java
gcloud functions deploy ocr-extract \ --entry-point functions.OcrProcessImage \ --runtime java17 \ --memory 512MB \
--trigger-bucket YOUR_IMAGE_BUCKET_NAME \
--set-env-vars "^:^GCP_PROJECT=YOUR_GCP_PROJECT_ID:TRANSLATE_TOPIC=YOUR_TRANSLATE_TOPIC_NAME:RESULT_TOPIC=YOUR_RESULT_TOPIC_NAME:TO_LANG=es,en,fr,ja"其中
YOUR_IMAGE_BUCKET_NAME是您要上傳圖片的 Cloud Storage bucket 名稱。如要使用 Pub/Sub 觸發條件部署文字翻譯函式,請在包含程式碼範例的目錄中執行下列指令 (如果是 Java,則在
pom.xml檔案中執行):Node.js
gcloud functions deploy ocr-translate \ --runtime nodejs22 \
--trigger-topic YOUR_TRANSLATE_TOPIC_NAME \
--entry-point translateText \
--set-env-vars "GCP_PROJECT=YOUR_GCP_PROJECT_ID,RESULT_TOPIC=YOUR_RESULT_TOPIC_NAME"使用
--runtime旗標指定 Node.js 支援版本的執行階段 ID,執行函式。Python
gcloud functions deploy ocr-translate \ --runtime python312 \
--trigger-topic YOUR_TRANSLATE_TOPIC_NAME \
--entry-point translate_text \
--set-env-vars "GCP_PROJECT=YOUR_GCP_PROJECT_ID,RESULT_TOPIC=YOUR_RESULT_TOPIC_NAME"使用
--runtime旗標指定 Python 支援版本的執行階段 ID,執行函式。Go
gcloud functions deploy ocr-translate \ --runtime go121 \
--trigger-topic YOUR_TRANSLATE_TOPIC_NAME \
--entry-point TranslateText \
--set-env-vars "GCP_PROJECT=YOUR_GCP_PROJECT_ID,RESULT_TOPIC=YOUR_RESULT_TOPIC_NAME"Java
gcloud functions deploy ocr-translate \ --entry-point functions.OcrTranslateText \ --runtime java17 \ --memory 512MB \
--trigger-topic YOUR_TRANSLATE_TOPIC_NAME \
--set-env-vars "GCP_PROJECT=YOUR_GCP_PROJECT_ID,RESULT_TOPIC=YOUR_RESULT_TOPIC_NAME"如要使用 Cloud Pub/Sub 觸發條件部署函式,將結果儲存至 Cloud Storage,請在包含程式碼範例的目錄中執行下列指令 (如果是 Java,則是在
pom.xml檔案中執行):Node.js
gcloud functions deploy ocr-save \ --runtime nodejs22 \
--trigger-topic YOUR_RESULT_TOPIC_NAME \
--entry-point saveResult \
--set-env-vars "GCP_PROJECT=YOUR_GCP_PROJECT_ID,RESULT_BUCKET=YOUR_RESULT_BUCKET_NAME"使用
--runtime旗標指定 Node.js 支援版本的執行階段 ID,執行函式。Python
gcloud functions deploy ocr-save \ --runtime python312 \
--trigger-topic YOUR_RESULT_TOPIC_NAME \
--entry-point save_result \
--set-env-vars "GCP_PROJECT=YOUR_GCP_PROJECT_ID,RESULT_BUCKET=YOUR_RESULT_BUCKET_NAME"使用
--runtime旗標指定 Python 支援版本的執行階段 ID,執行函式。Go
gcloud functions deploy ocr-save \ --runtime go121 \
--trigger-topic YOUR_RESULT_TOPIC_NAME \
--entry-point SaveResult \
--set-env-vars "GCP_PROJECT=YOUR_GCP_PROJECT_ID,RESULT_BUCKET=YOUR_RESULT_BUCKET_NAME"Java
gcloud functions deploy ocr-save \ --entry-point functions.OcrSaveResult \ --runtime java17 \ --memory 512MB \
--trigger-topic YOUR_RESULT_TOPIC_NAME \
--set-env-vars "GCP_PROJECT=YOUR_GCP_PROJECT_ID,RESULT_BUCKET=YOUR_RESULT_BUCKET_NAME"
上傳圖片
將圖片上傳至圖片 Cloud Storage bucket:
gcloud storage cp
PATH_TO_IMAGEgs://YOUR_IMAGE_BUCKET_NAME其中
PATH_TO_IMAGE是本機系統中圖片檔 (內含文字) 的路徑。YOUR_IMAGE_BUCKET_NAME是您要上傳圖片的 bucket 名稱。
您可以從範例專案下載其中一個圖片。
觀察記錄以確定執行作業已完成:
gcloud functions logs read --limit 100
您可以在用於
YOUR_RESULT_BUCKET_NAME的 Cloud Storage bucket 中查看儲存的翻譯內容。
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 帳戶收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。
刪除專案的方法如下:
- 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。
- 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。
刪除函式
刪除 Cloud Run functions 不會移除儲存在 Cloud Storage 中的任何資源。
如要刪除在本教學課程中建立的 Cloud Run functions,請執行下列指令:
gcloud functions delete ocr-extract gcloud functions delete ocr-translate gcloud functions delete ocr-save
您也可以從Google Cloud 控制台刪除 Cloud Run functions。