Use o modelo Desfocar rostos com o SDK Python


Este tutorial mostra como usar o SDK Python para esbater rostos em vídeos. O exemplo esbate ficheiros de vídeo de um contentor do Cloud Storage e gera resultados de vídeo esbatidos. Estes vídeos de saída são armazenados no mesmo contentor do Cloud Storage que os vídeos de origem.

Objetivos

Este tutorial mostra como fazer o seguinte:

  • Crie um contentor do Cloud Storage.
  • Carregue um ficheiro de vídeo local para o contentor.
  • Envie um pedido através do SDK Python.
  • Veja vídeos com a saída esbatida.

Custos

Neste documento, usa os seguintes componentes faturáveis do Google Cloud:

Para gerar uma estimativa de custos com base na sua utilização projetada, use a calculadora de preços.

Os novos Google Cloud utilizadores podem ser elegíveis para uma avaliação gratuita.

Quando terminar as tarefas descritas neste documento, pode evitar a faturação contínua eliminando os recursos que criou. Para mais informações, consulte o artigo Limpe.

Antes de começar

  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. Se estiver a usar um fornecedor de identidade (IdP) externo, tem primeiro de iniciar sessão na CLI gcloud com a sua identidade federada.

  4. Para inicializar a CLI gcloud, execute o seguinte comando:

    gcloud init
  5. 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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • 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:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    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. Se estiver a usar um fornecedor de identidade (IdP) externo, tem primeiro de iniciar sessão na CLI gcloud com a sua identidade federada.

  12. Para inicializar a CLI gcloud, execute o seguinte comando:

    gcloud init
  13. 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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • 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:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    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. Obtenha o código-fonte do SDK Vertex AI Vision:
    git clone https://github.com/google/visionai.git

    Os exemplos de Python estão localizados no diretório visionai/python/example/.

  19. Obtenha o SDK Python:
    wget https://github.com/google/visionai/releases/download/v0.0.5/visionai-0.0.5-py3-none-any.whl
  20. Adicione ficheiros de entrada ao Cloud Storage

    Antes de poder enviar um pedido através do SDK Python, crie um contentor do Cloud Storage e carregue um vídeo local para usar como entrada.

    1. Crie um contentor do Cloud Storage:

      gcloud storage buckets create gs://BUCKET_NAME
      
    2. Carregue um ficheiro de vídeo local para o novo contentor:

      gcloud storage cp LOCAL_FILE gs://BUCKET_NAME
      

    Instale as dependências e envie o pedido

    Depois de criar o contentor do Cloud Storage para vídeos de entrada e saída e adicionar um vídeo local, instale as dependências necessárias e envie o seu pedido.

    1. Opcional. Configure o ambiente virtual:

      1. Se não estiver instalada, instale a app virtualenv:

        sudo apt-get install python3-venv
        
      2. Crie um novo ambiente virtual:

        python3 -m venv vaivenv
        
      3. Ative o ambiente virtual:

        source vaivenv/bin/activate
        
    2. Instalar dependências:

      pip3 install visionai-0.0.5-py3-none-any.whl
      pip3 install google-cloud-storage
      
    3. Envie o seu pedido com o SDK Python.

      Faça as seguintes substituições de variáveis:

      • PROJECT_ID: o ID do seu Google Cloud projeto.
      • LOCATION_ID: o ID da sua localização. Por exemplo, us-central1. Mais informações. Regiões suportadas.
      • BUCKET_NAME: o contentor do Cloud Storage que criou.
      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
      

      Deverá ver uma saída semelhante à seguinte:

       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!
       ```
      

    Examine a saída

    Depois de o vídeo terminar o processamento, pode examinar o resultado no seu contentor do Cloud Storage. Os ficheiros de vídeo esbatidos gerados vão estar no mesmo contentor do Cloud Storage que o vídeo de origem.

    1. Apresente uma lista de todos os objetos no seu contentor com o comando gcloud storage ls:

      gcloud storage ls gs://bucket
      

      Deverá ver os ficheiros de origem e os ficheiros de saída semelhantes ao seguinte:

      test1.mp4
      test2.mp4
      test1_deid_output.mp4
      test2_deid_output.mp4
      
    2. Opcional. Transfira os ficheiros de saída localmente com o comando gcloud storage cp e veja os vídeos com o efeito de esbatimento:

      gcloud storage cp gs://BUCKET_NAME/FILE_NAME .
      

    Limpar

    Para evitar incorrer em custos na sua conta do Google Cloud pelos recursos usados neste tutorial, elimine o projeto que contém os recursos ou mantenha o projeto e elimine os recursos individuais.

    O que se segue?