本教學課程示範如何使用 Cloud Run 函式、Cloud Vision API 和 ImageMagick,偵測並模糊處理上傳至 Cloud Storage 值區的令人反感圖片。
目標
- 部署由儲存空間觸發的背景 Cloud Run 函式。
- 使用 Vision API 偵測暴力或成人內容。
- 使用 ImageMagick 來模糊處理令人反感的圖片。
- 上傳食人殭屍的圖片來測試函式。
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
- Cloud Run functions
- Cloud Storage
- Cloud Vision
如要根據預測用量估算費用,請使用 Pricing Calculator。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
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
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, Cloud Storage, and Cloud Vision APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
Install the 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
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, Cloud Storage, and Cloud Vision APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
-
如要初始化 gcloud CLI,請執行下列指令:
gcloud init
- 準備開發環境。 <0x
- 將圖片上傳至 Cloud Storage 值區。
- 函式會使用 Vision API 分析圖片。
- 如果偵測到暴力或成人內容,這項函式會使用 ImageMagick 將圖片模糊處理。
- 模糊處理後的圖片會上傳至另一個 Cloud Storage bucket,供您使用。
建立 Cloud Storage 值區以供上傳圖片,其中
YOUR_INPUT_BUCKET_NAME
是不重複的值區名稱:gcloud storage buckets create gs://YOUR_INPUT_BUCKET_NAME
建立 Cloud Storage 值區以接收模糊處理的圖片,其中
YOUR_OUTPUT_BUCKET_NAME
是不重複的值區名稱:gcloud storage buckets create gs://YOUR_OUTPUT_BUCKET_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 格式的範例,然後解壓縮該檔案。
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
您也可以 下載 zip 格式的範例,然後解壓縮該檔案。
變更為包含 Cloud Run 函式範例程式碼的目錄:
Node.js
cd nodejs-docs-samples/functions/imagemagick/
Python
cd python-docs-samples/functions/imagemagick/
Go
cd golang-samples/functions/imagemagick/
Java
cd java-docs-samples/functions/imagemagick/
Ruby
cd ruby-docs-samples/functions/imagemagick/
RUNTIME
:以 Ubuntu 18.04 為基礎的執行階段 (後續執行階段不支援 ImageMagick)。YOUR_INPUT_BUCKET_NAME
:用於上傳圖片的 Cloud Storage bucket 名稱。YOUR_OUTPUT_BUCKET_NAME
:要儲存模糊處理後圖片的 bucket 名稱。上傳令人反感的圖片,例如這張食人殭屍圖片:
gcloud storage cp zombie.jpg gs://YOUR_INPUT_BUCKET_NAME
其中
YOUR_INPUT_BUCKET_NAME
是您稍早建立來上傳圖片的 Cloud Storage 值區。觀察記錄以確定執行已經完成:
gcloud functions logs read --limit 100
您可以在先前建立的
YOUR_OUTPUT_BUCKET_NAME
Cloud Storage bucket 中查看模糊處理的圖片。- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
如果您已安裝 gcloud CLI,請執行下列指令來更新:
gcloud components update
視覺化資料流動過程
ImageMagick 教學課程應用程式中的資料流動過程涉及數個步驟:
準備應用程式
瞭解程式碼
匯入依附元件
應用程式必須匯入多個依附元件,才能與Google Cloud 服務、ImageMagick 和檔案系統互動:
Node.js
Python
Go
Java
Ruby
分析圖片
將圖片上傳至您建立來儲存圖片的 Cloud Storage 值區時,會叫用下列函式。這項函式會使用 Vision API 偵測上傳圖片中的暴力或成人內容。
Node.js
Python
Go
Java
Ruby
模糊處理圖片
在上傳的圖片中偵測到暴力或成人內容時,會呼叫下列函式。函式會下載令人反感的圖片,使用 ImageMagick 模糊處理圖片,然後上傳模糊處理後的圖片覆蓋原始圖片。
Node.js
Python
Go
Java
Ruby
部署函式
如要透過 Storage 觸發條件部署函式,請在包含程式碼範例的目錄中執行下列指令 (如果是 Java,則在 pom.xml
檔案中執行):
Node.js
gcloud functions deploy blurOffensiveImages \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Python
gcloud functions deploy blur_offensive_images \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Go
gcloud functions deploy BlurOffensiveImages \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Java
gcloud functions deploy java-blur-function \ --no-gen2 \ --entry-point=functions.ImageMagick \ --runtime=RUNTIME \ --memory 512MB \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
C#
gcloud functions deploy csharp-blur-function \ --no-gen2 \ --entry-point=ImageMagick.Function \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Ruby
gcloud functions deploy blur_offensive_images \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
更改下列內容:
在本範例中,請勿在 deploy
指令中加入 gs://
做為 bucket 名稱的一部分。
上傳圖片
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。
如要刪除專案:
刪除函式
刪除 Cloud Run 函式不會移除儲存在 Cloud Storage 中的任何資源。
如要刪除您在本教學課程中部署的函式,請執行下列指令:
Node.js
gcloud functions delete blurOffensiveImages
Python
gcloud functions delete blur_offensive_images
Go
gcloud functions delete BlurOffensiveImages
Java
gcloud functions delete java-blur-function
Ruby
gcloud functions delete blur_offensive_images
您也可以從Google Cloud 控制台刪除 Cloud Run 函式。