本教學課程說明如何使用 Python SDK 模糊處理影片中的臉部。這個範例會模糊處理 Cloud Storage bucket 中的影片檔案,並產生模糊處理的影片輸出內容。這些輸出影片會儲存在與來源影片相同的 Cloud Storage 值區。
目標
本教學課程說明如何執行下列操作:
- 建立 Cloud Storage bucket。
- 將本機影片檔案上傳至值區。
- 使用 Python SDK 傳送要求。
- 查看模糊處理後的輸出影片。
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
- Vertex AI Vision (Models - Person / face blur)
- Cloud Storage
您可以使用 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.
-
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
Create or select 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.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI Vision, Cloud Storage APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable visionai.googleapis.com
storage.googleapis.com -
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/visionai.editor, roles/storage.objectAdmingcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID: Your project ID.USER_IDENTIFIER: The identifier for your user account. For example,myemail@example.com.ROLE: The IAM role that you grant to your user account.
-
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
Create or select 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.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI Vision, Cloud Storage APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable visionai.googleapis.com
storage.googleapis.com -
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/visionai.editor, roles/storage.objectAdmingcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID: Your project ID.USER_IDENTIFIER: The identifier for your user account. For example,myemail@example.com.ROLE: The IAM role that you grant to your user account.
- 取得 Vertex AI Vision SDK 原始碼:
git clone https://github.com/google/visionai.git
Python 範例位於
visionai/python/example/目錄中。 - 取得 Python SDK:
wget https://github.com/google/visionai/releases/download/v0.0.5/visionai-0.0.5-py3-none-any.whl
建立 Cloud Storage bucket:
gcloud storage buckets create gs://BUCKET_NAME將本機影片檔案上傳至新值區:
gcloud storage cp LOCAL_FILE gs://BUCKET_NAME(選用步驟) 設定虛擬環境:
如果尚未安裝,請安裝
virtualenv:sudo apt-get install python3-venv建立新的虛擬環境:
python3 -m venv vaivenv啟用虛擬環境:
source vaivenv/bin/activate
安裝依附元件:
pip3 install visionai-0.0.5-py3-none-any.whl pip3 install google-cloud-storage使用 Python SDK 傳送要求。
請替換下列變數:
- PROJECT_ID:您的 Google Cloud 專案 ID
- LOCATION_ID:您的位置 ID。例如:
us-central1。更多資訊。支援的地區。 - BUCKET_NAME:您建立的 Cloud Storage bucket。
python3 visionai/python/example/blur_gcs_video.py \ --project_id=PROJECT_ID –cluster_id=application-cluster-0 \ –location_id=LOCATION_ID –bucket_name=BUCKET_NAME輸出結果應該會類似下列內容:
Listing mp4 files... test1.mp4 test2.mp4 Creating deid processes... process vnluvxgl is created process rvrdoucx is created Waiting for processes to finish... process vnluvxgl state is COMPLETED process rvrdoucx state is COMPLETED All processes have finished, please check the GCS bucket! ```使用
gcloud storage ls指令列出值區中的所有物件:gcloud storage ls gs://bucket您應該會看到類似下列內容的來源檔案和輸出檔案:
test1.mp4 test2.mp4 test1_deid_output.mp4 test2_deid_output.mp4(選用步驟) 使用
gcloud storage cp指令將輸出檔案下載到本機,並查看模糊處理的影片:gcloud storage cp gs://BUCKET_NAME/FILE_NAME .
將輸入檔案新增至 Cloud Storage
使用 Python SDK 傳送要求前,請先建立 Cloud Storage bucket,並上傳要當做輸入內容的本機影片。
安裝依附元件並傳送要求
為輸入和輸出影片建立 Cloud Storage bucket,並新增本機影片後,請安裝必要依附元件並傳送要求。
檢查輸出內容
影片處理完畢後,您可以在 Cloud Storage 值區中查看輸出內容。生成的模糊處理影片檔案會與來源影片位於同一個 Cloud Storage bucket。
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。