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:
- Vertex AI Vision (Models - Person / face blur)
- Cloud Storage
Para gerar uma estimativa de custo baseada na projeção de uso deste tutorial, use a calculadora de preços.
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
- 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
- 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/
. - 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.
Crie um bucket do Cloud Storage:
gcloud storage buckets create gs://BUCKET_NAME
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.
Opcional. Configure o ambiente virtual:
Se não estiver instalado, instale
virtualenv
:sudo apt-get install python3-venv
Crie um novo ambiente virtual:
python3 -m venv vaivenv
Ative o ambiente virtual:
source vaivenv/bin/activate
Instale as dependências:
pip3 install visionai-0.0.5-py3-none-any.whl pip3 install google-cloud-storage
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.
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
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
- Leia mais sobre o modelo de desfoque de pessoas.
- Confira arquiteturas de referência, diagramas, tutoriais e práticas recomendadas do Google Cloud. Confira o Centro de arquitetura do Cloud.