Python SDK で Face Blur モデルを使用する


このチュートリアルでは、Python SDK を使用して動画内の顔をぼかす方法について説明します。この例では、Cloud Storage バケットの動画ファイルをぼかし、ぼかし動画の出力を生成します。これらの出力動画は、ソース動画と同じ Cloud Storage バケットに保存されます。

目標

このチュートリアルでは、次の方法を説明します。

  • Cloud Storage バケットを作成する。
  • ローカル動画ファイルをバケットにアップロードします。
  • Python SDK を使用してリクエストを送信します。
  • ぼかし処理された出力動画を表示します。

費用

このドキュメントでは、課金対象である次の Google Cloudコンポーネントを使用します。

料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。

新規の 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. 外部 ID プロバイダ(IdP)を使用している場合は、まずフェデレーション ID を使用して 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. 外部 ID プロバイダ(IdP)を使用している場合は、まずフェデレーション ID を使用して 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 バケットを作成し、入力として使用するローカル動画をアップロードします。

    1. Cloud Storage バケットを作成します。

      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 を使用してリクエストを送信します。

      次の変数を置き換えます。

      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 アカウントに課金されないようにするには、リソースを含むプロジェクトを削除するか、プロジェクトを維持して個々のリソースを削除します。

    次のステップ