使用 Python SDK 搭配臉部模糊處理模型


本教學課程說明如何使用 Python SDK 模糊處理影片中的臉部。這個範例會模糊處理 Cloud Storage bucket 中的影片檔案,並產生模糊處理的影片輸出內容。這些輸出影片會儲存在與來源影片相同的 Cloud Storage 值區。

目標

本教學課程將說明如何執行下列操作:

  • 建立 Cloud Storage bucket。
  • 將本機影片檔案上傳至值區。
  • 使用 Python SDK 傳送要求。
  • 查看模糊處理後的輸出影片。

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用資格。

完成本文所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱清除所用資源一節。

事前準備

  1. 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.
  2. Install the Google Cloud CLI.

  3. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

  4. 如要初始化 gcloud CLI,請執行下列指令:

    gcloud init
  5. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with 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_ID with your Google Cloud project name.

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI Vision, Cloud Storage APIs:

    gcloud services enable visionai.googleapis.com storage.googleapis.com
  8. 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.

  9. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/visionai.editor, roles/storage.objectAdmin

    gcloud 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.
  10. Install the Google Cloud CLI.

  11. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

  12. 如要初始化 gcloud CLI,請執行下列指令:

    gcloud init
  13. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with 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_ID with your Google Cloud project name.

  14. Verify that billing is enabled for your Google Cloud project.

  15. Enable the Vertex AI Vision, Cloud Storage APIs:

    gcloud services enable visionai.googleapis.com storage.googleapis.com
  16. 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.

  17. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/visionai.editor, roles/storage.objectAdmin

    gcloud 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.
  18. 取得 Vertex AI Vision SDK 原始碼:
    git clone https://github.com/google/visionai.git

    Python 範例位於 visionai/python/example/ 目錄中。

  19. 取得 Python SDK:
    wget https://github.com/google/visionai/releases/download/v0.0.5/visionai-0.0.5-py3-none-any.whl
  20. 將輸入檔案新增至 Cloud Storage

    使用 Python SDK 傳送要求前,請先建立 Cloud Storage bucket,並上傳要當做輸入內容的本機影片。

    1. 建立 Cloud Storage bucket:

      gcloud storage buckets create gs://BUCKET_NAME
      
    2. 將本機影片檔案上傳至新值區:

      gcloud storage cp LOCAL_FILE gs://BUCKET_NAME
      

    安裝依附元件並傳送要求

    建立 Cloud Storage 值區來存放輸入和輸出影片,並新增本機影片後,請安裝必要依附元件並傳送要求。

    1. (選用步驟) 設定虛擬環境:

      1. 如果尚未安裝,請安裝 virtualenv

        sudo apt-get install python3-venv
        
      2. 建立新的虛擬環境:

        python3 -m venv vaivenv
        
      3. 啟用虛擬環境:

        source vaivenv/bin/activate
        
    2. 安裝依附元件:

      pip3 install visionai-0.0.5-py3-none-any.whl
      pip3 install google-cloud-storage
      
    3. 使用 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!
       ```
      

    檢查輸出內容

    影片處理完畢後,您可以在 Cloud Storage 值區中查看輸出內容。生成的模糊處理影片檔案會與來源影片位於同一個 Cloud Storage 值區。

    1. 使用 gcloud storage ls 指令列出值區中的所有物件:

      gcloud storage ls gs://bucket
      

      您應該會看到類似下列內容的來源檔案和輸出檔案:

      test1.mp4
      test2.mp4
      test1_deid_output.mp4
      test2_deid_output.mp4
      
    2. (選用步驟) 使用 gcloud storage cp 指令將輸出檔案下載到本機,並查看模糊處理的影片:

      gcloud storage cp gs://BUCKET_NAME/FILE_NAME .
      

    清除所用資源

    如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。

    後續步驟