Usar o modelo de desfoque de rosto com o SDK do Python


Neste tutorial, mostramos como usar o SDK do Python para desfocar rostos em vídeos. O exemplo desfoca arquivos de vídeo de um bucket do Cloud Storage e gera saídas de vídeo desfocadas. Esses vídeos de saída são armazenados no mesmo bucket do Cloud Storage que os vídeos de origem.

Objetivos

Este tutorial mostra como fazer o seguinte:

  • Criar um bucket do Cloud Storage.
  • Faça upload de um arquivo de vídeo local para o bucket.
  • Envie uma solicitação usando o SDK do Python.
  • Ver vídeos de saída desfocados.

Custos

Neste documento, você vai usar os seguintes componentes faturáveis do Google Cloud:

Para gerar uma estimativa de custo baseada na projeção de uso deste tutorial, use a calculadora de preços.

Novos usuários do Google Cloud podem estar qualificados para uma avaliação gratuita.

Ao concluir as tarefas descritas neste documento, é possível evitar o faturamento contínuo excluindo os recursos criados. Saiba mais em Limpeza.

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

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

  19. Acesse o SDK do Python:
    wget https://github.com/google/visionai/releases/download/v0.0.5/visionai-0.0.5-py3-none-any.whl

Adicionar arquivos de entrada ao Cloud Storage

Antes de enviar uma solicitação usando o SDK do Python, crie um bucket do Cloud Storage e faça upload de um vídeo local para usar como entrada.

  1. Crie um bucket do Cloud Storage:

    gcloud storage buckets create gs://BUCKET_NAME
    
  2. Faça upload de um arquivo de vídeo local para o novo bucket:

    gcloud storage cp LOCAL_FILE gs://BUCKET_NAME
    

Instalar dependências e enviar a solicitação

Depois de criar o bucket 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 sua solicitação.

  1. Opcional. Configure o ambiente virtual:

    1. Se não estiver instalado, instale 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. Instale as dependências:

    pip3 install visionai-0.0.5-py3-none-any.whl
    pip3 install google-cloud-storage
    
  3. Envie sua solicitação com o SDK do Python.

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

    • PROJECT_ID: o ID do projeto do Google Cloud .
    • LOCATION_ID: o ID do local. Por exemplo, us-central1. Mais informações. Regiões com suporte.
    • BUCKET_NAME: o bucket do Cloud Storage que você 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
    

    A resposta será semelhante a esta:

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

Examinar a saída

Depois que o vídeo terminar de ser processado, examine a saída no bucket do Cloud Storage. Os arquivos de vídeo desfocados gerados vão estar no mesmo bucket do Cloud Storage que o vídeo de origem.

  1. Liste todos os objetos no bucket com o comando gcloud storage ls:

    gcloud storage ls gs://bucket
    

    Você vai ver os arquivos de origem e de saída semelhantes a estes:

    test1.mp4
    test2.mp4
    test1_deid_output.mp4
    test2_deid_output.mp4
    
  2. Opcional. Baixe os arquivos de saída localmente com o comando gcloud storage cp e confira os vídeos desfocados:

    gcloud storage cp gs://BUCKET_NAME/FILE_NAME .
    

Limpar

Para evitar cobranças na sua conta do Google Cloud pelos recursos usados no tutorial, exclua o projeto que os contém ou mantenha o projeto e exclua os recursos individuais.

A seguir