Utiliser le modèle de floutage du visage avec le SDK Python


Ce tutoriel vous explique comment utiliser le SDK Python pour flouter les visages dans une vidéo. L'exemple floute les fichiers vidéo d'un bucket Cloud Storage et génère des sorties vidéo floutées. Ces vidéos de sortie sont stockées dans le même bucket Cloud Storage que les vidéos sources.

Objectifs

Ce tutoriel vous explique comment effectuer les tâches suivantes :

  • Créez un bucket Cloud Storage.
  • Importez un fichier vidéo local dans le bucket.
  • Envoyez une requête à l'aide du SDK Python.
  • Affichez les vidéos de sortie floutées.

Coûts

Dans ce document, vous utilisez les composants facturables suivants de Google Cloud :

Pour obtenir une estimation des coûts en fonction de votre utilisation prévue, utilisez le simulateur de coût.

Les nouveaux utilisateurs de Google Cloud peuvent bénéficier d'un essai gratuit.

Une fois que vous avez terminé les tâches décrites dans ce document, vous pouvez éviter de continuer à payer des frais en supprimant les ressources que vous avez créées. Pour en savoir plus, consultez la section Effectuer un nettoyage.

Avant de commencer

  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. Obtenez le code source du SDK Vertex AI Vision :
    git clone https://github.com/google/visionai.git

    Les exemples Python se trouvent dans le répertoire visionai/python/example/.

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

Ajouter des fichiers d'entrée à Cloud Storage

Avant de pouvoir envoyer une requête à l'aide du SDK Python, créez un bucket Cloud Storage et importez une vidéo locale à utiliser comme entrée.

  1. Créez un bucket Cloud Storage :

    gcloud storage buckets create gs://BUCKET_NAME
    
  2. Importez un fichier vidéo local dans le nouveau bucket :

    gcloud storage cp LOCAL_FILE gs://BUCKET_NAME
    

Installer les dépendances et envoyer la requête

Après avoir créé votre bucket Cloud Storage pour les vidéos d'entrée et de sortie, et ajouté une vidéo locale, installez les dépendances nécessaires et envoyez votre requête.

  1. Facultatif. Configurez votre environnement virtuel :

    1. Si ce n'est pas le cas, installez virtualenv :

      sudo apt-get install python3-venv
      
    2. Créez un environnement virtuel :

      python3 -m venv vaivenv
      
    3. Activez votre environnement virtuel :

      source vaivenv/bin/activate
      
  2. Installez les dépendances :

    pip3 install visionai-0.0.5-py3-none-any.whl
    pip3 install google-cloud-storage
    
  3. Envoyez votre demande avec le SDK Python.

    Effectuez les substitutions de variables suivantes :

    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
    

    La sortie obtenue doit ressembler à ceci :

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

Examiner le résultat

Une fois le traitement de votre vidéo terminé, vous pouvez examiner le résultat dans votre bucket Cloud Storage. Les fichiers vidéo floutés générés se trouveront dans le même bucket Cloud Storage que la vidéo source.

  1. Répertoriez tous les objets de votre bucket avec la commande gcloud storage ls :

    gcloud storage ls gs://bucket
    

    Les fichiers sources et les fichiers de sortie doivent être semblables à ce qui suit :

    test1.mp4
    test2.mp4
    test1_deid_output.mp4
    test2_deid_output.mp4
    
  2. Facultatif. Téléchargez les fichiers de sortie en local avec la commande gcloud storage cp et affichez les vidéos floutées :

    gcloud storage cp gs://BUCKET_NAME/FILE_NAME .
    

Effectuer un nettoyage

Pour éviter que les ressources utilisées lors de ce tutoriel soient facturées sur votre compte Google Cloud, supprimez le projet contenant les ressources, ou conservez le projet et supprimez les ressources individuelles.

Étapes suivantes