このチュートリアルでは、Python SDK を使用して動画内の顔をぼかす方法について説明します。この例では、Cloud Storage バケットの動画ファイルをぼかし、ぼかし動画の出力を生成します。これらの出力動画は、ソース動画と同じ Cloud Storage バケットに保存されます。
目標
このチュートリアルでは、次の方法を説明します。
- Cloud Storage バケットを作成する。
- ローカル動画ファイルをバケットにアップロードします。
- Python SDK を使用してリクエストを送信します。
- ぼかし処理された出力動画を表示します。
料金
このドキュメントでは、課金対象である次の Google Cloudコンポーネントを使用します。
- Vertex AI Vision (Models - Person / face blur)
- Cloud Storage
料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。
このドキュメントに記載されているタスクの完了後、作成したリソースを削除すると、それ以上の請求は発生しません。詳細については、クリーンアップをご覧ください。
始める前に
- 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.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI Vision, Cloud Storage APIs:
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.objectAdmin
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
- Replace
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI Vision, Cloud Storage APIs:
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.objectAdmin
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
- Replace
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
- 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 に追加する
Python SDK を使用してリクエストを送信する前に、Cloud Storage バケットを作成し、入力として使用するローカル動画をアップロードします。
Cloud Storage バケットを作成します。
gcloud storage buckets create gs://BUCKET_NAME
ローカル動画ファイルを新しいバケットにアップロードします。
gcloud storage cp LOCAL_FILE gs://BUCKET_NAME
依存関係をインストールしてリクエストを送信する
入力動画と出力動画用の Cloud Storage バケットを作成してローカル動画を追加したら、必要な依存関係をインストールしてリクエストを送信します。
省略可。仮想環境を設定します。
インストールされていない場合は、
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 バケット。
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 バケットに保存されます。
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 .
クリーンアップ
このチュートリアルで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、リソースを含むプロジェクトを削除するか、プロジェクトを維持して個々のリソースを削除します。
次のステップ
- 詳しくは、人物ぼかしモデルをご覧ください。
- Google Cloud に関するリファレンス アーキテクチャ、図、ベスト プラクティスを確認する。Cloud アーキテクチャ センターをご覧ください。