将人脸模糊处理模型与 Python SDK 搭配使用


本教程将向您展示如何使用 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. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

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

  11. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  12. To initialize the gcloud CLI, run the following command:

    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. Make sure 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 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.
  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

将输入文件添加到 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 发送请求。

    进行以下变量替换:

    • 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 存储桶中。

  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 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。

后续步骤