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 illustra 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 utilizzi i seguenti componenti fatturabili di Google Cloud:

Per generare una stima dei costi in base all'utilizzo previsto, utilizza il Calcolatore prezzi. I nuovi Google Cloud utenti potrebbero avere diritto a una prova gratuita.

Al termine delle attività descritte in questo documento, puoi evitare la fatturazione continua eliminando le risorse che hai creato. Per ulteriori informazioni, consulta la sezione 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. To initialize the gcloud CLI, run the following command:

    gcloud init
  4. 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.

  5. Make sure that billing is enabled for your Google Cloud project.

  6. Enable the Vertex AI Vision, Cloud Storage APIs:

    gcloud services enable visionai.googleapis.com storage.googleapis.com
  7. 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.

  8. 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.
  9. Install the Google Cloud CLI.
  10. To initialize the gcloud CLI, run the following command:

    gcloud init
  11. 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.

  12. Make sure that billing is enabled for your Google Cloud project.

  13. Enable the Vertex AI Vision, Cloud Storage APIs:

    gcloud services enable visionai.googleapis.com storage.googleapis.com
  14. 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.

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

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

  17. Ottieni l'SDK Python:
    wget https://github.com/google/visionai/releases/download/v0.0.5/visionai-0.0.5-py3-none-any.whl

Aggiungi i 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 di 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.

    Apporta le seguenti sostituzioni delle variabili:

    • PROJECT_ID: il tuo ID progetto Google Cloud .
    • LOCATION_ID: il tuo ID posizione. 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 saranno nello stesso bucket Cloud Storage del video di origine.

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