Utilizzare il modello di sfocatura del viso con l'SDK Python


Questo tutorial mostra come utilizzare l'SDK Python per sfocare i volti nei video. L'esempio sfoca i file video di un bucket Cloud Storage e genera output video sfocati. Questi video di output vengono archiviati nello stesso bucket Cloud Storage dei video di origine.

Obiettivi

Questo tutorial mostra come:

  • Creare un bucket Cloud Storage.
  • Carica un file video locale nel bucket.
  • Invia una richiesta utilizzando l'SDK Python.
  • Visualizzare i video di output sfocati.

Costi

In questo documento vengono utilizzati i seguenti componenti fatturabili di Google Cloud:

Per generare una stima dei costi in base all'utilizzo previsto, utilizza il calcolatore prezzi.

I nuovi utenti di Google Cloud potrebbero avere diritto a una prova senza costi.

Al termine delle attività descritte in questo documento, puoi evitare l'addebito di ulteriori costi eliminando le risorse che hai creato. Per ulteriori informazioni, vedi Pulizia.

Prima di iniziare

  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 utilizzi un provider di identità (IdP) esterno, devi prima accedere alla gcloud CLI con la tua identità federata.

  4. Per inizializzare gcloud CLI, esegui questo comando:

    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. Verify 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 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 utilizzi un provider di identità (IdP) esterno, devi prima accedere alla gcloud CLI con la tua identità federata.

  12. Per inizializzare gcloud CLI, esegui questo comando:

    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. Verify 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 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. Recupera il codice sorgente dell'SDK Vertex AI Vision:
    git clone https://github.com/google/visionai.git

    Gli esempi Python si trovano nella directory visionai/python/example/.

  19. Ottieni l'SDK Python:
    wget https://github.com/google/visionai/releases/download/v0.0.5/visionai-0.0.5-py3-none-any.whl
  20. Aggiungi file di input a Cloud Storage

    Prima di poter inviare una richiesta utilizzando l'SDK Python, crea un bucket Cloud Storage e carica un video locale da utilizzare come input.

    1. Crea un bucket Cloud Storage:

      gcloud storage buckets create gs://BUCKET_NAME
      
    2. Carica un file video locale nel nuovo bucket:

      gcloud storage cp LOCAL_FILE gs://BUCKET_NAME
      

    Installa le dipendenze e invia la richiesta

    Dopo aver creato il bucket Cloud Storage per i video di input e output e aver aggiunto un video locale, installa le dipendenze necessarie e invia la richiesta.

    1. Facoltativo. Configura l'ambiente virtuale:

      1. Se non è installato, installa virtualenv:

        sudo apt-get install python3-venv
        
      2. Crea un nuovo ambiente virtuale:

        python3 -m venv vaivenv
        
      3. Attiva l'ambiente virtuale:

        source vaivenv/bin/activate
        
    2. Installa le dipendenze:

      pip3 install visionai-0.0.5-py3-none-any.whl
      pip3 install google-cloud-storage
      
    3. Invia la richiesta con l'SDK Python.

      Effettua le seguenti sostituzioni di variabili:

      • PROJECT_ID: il tuo ID progetto Google Cloud .
      • LOCATION_ID: il tuo ID sede. Ad esempio, us-central1. Scopri di più. Regioni supportate.
      • BUCKET_NAME: il bucket Cloud Storage che hai creato.
      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
      

      Dovresti vedere un output simile al seguente:

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

    Esamina l'output

    Una volta completata l'elaborazione del video, puoi esaminare l'output nel tuo bucket Cloud Storage. I file video sfocati generati si troveranno nello stesso bucket Cloud Storage del video sorgente.

    1. Elenca tutti gli oggetti nel bucket con il comando gcloud storage ls:

      gcloud storage ls gs://bucket
      

      Dovresti vedere i file di origine e i file di output simili ai seguenti:

      test1.mp4
      test2.mp4
      test1_deid_output.mp4
      test2_deid_output.mp4
      
    2. Facoltativo. Scarica i file di output localmente con il comando gcloud storage cp e visualizza i video sfocati:

      gcloud storage cp gs://BUCKET_NAME/FILE_NAME .
      

    Esegui la pulizia

    Per evitare che al tuo account Google Cloud vengano addebitati costi relativi alle risorse utilizzate in questo tutorial, elimina il progetto che contiene le risorse oppure mantieni il progetto ed elimina le singole risorse.

    Passaggi successivi