Modell zum Unkenntlichmachen von Gesichtern mit dem Python SDK verwenden


In dieser Anleitung wird gezeigt, wie Sie mit dem Python SDK Gesichter in Videos unkenntlich machen. Im Beispiel werden Videodateien aus einem Cloud Storage-Bucket unkenntlich gemacht und unkenntlich gemachte Videoausgaben generiert. Diese Ausgabevideos werden im selben Cloud Storage-Bucket wie die Quellvideos gespeichert.

Lernziele

In dieser Anleitung wird Folgendes beschrieben:

  • Cloud Storage-Bucket erstellen
  • Laden Sie eine lokale Videodatei in den Bucket hoch.
  • Senden Sie eine Anfrage mit dem Python SDK.
  • Verpixelte Ausgabevideos ansehen

Kosten

In diesem Dokument verwenden Sie die folgenden kostenpflichtigen Komponenten von Google Cloud:

Mit dem Preisrechner können Sie eine Kostenschätzung für Ihre voraussichtliche Nutzung vornehmen.

Neuen Google Cloud Nutzern steht möglicherweise eine kostenlose Testversion zur Verfügung.

Nach Abschluss der in diesem Dokument beschriebenen Aufgaben können Sie weitere Kosten vermeiden, indem Sie die erstellten Ressourcen löschen. Weitere Informationen finden Sie unter Bereinigen.

Hinweise

  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. Quellcode des Vertex AI Vision SDK abrufen:
    git clone https://github.com/google/visionai.git

    Die Python-Beispiele befinden sich im Verzeichnis visionai/python/example/.

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

Eingabedateien in Cloud Storage hinzufügen

Bevor Sie eine Anfrage mit dem Python SDK senden können, müssen Sie einen Cloud Storage-Bucket erstellen und ein lokales Video als Eingabe hochladen.

  1. Erstellen Sie einen Cloud Storage-Bucket:

    gcloud storage buckets create gs://BUCKET_NAME
    
  2. Laden Sie eine lokale Videodatei in den neuen Bucket hoch:

    gcloud storage cp LOCAL_FILE gs://BUCKET_NAME
    

Abhängigkeiten installieren und Anfrage senden

Nachdem Sie Ihren Cloud Storage-Bucket für Ein- und Ausgabevideos erstellt und ein lokales Video hinzugefügt haben, installieren Sie die erforderlichen Abhängigkeiten und senden Sie Ihre Anfrage.

  1. Optional. Virtuelle Umgebung einrichten:

    1. Wenn virtualenv nicht installiert ist, installieren Sie es:

      sudo apt-get install python3-venv
      
    2. Erstellen Sie eine neue virtuelle Umgebung:

      python3 -m venv vaivenv
      
    3. Aktivieren Sie Ihre virtuelle Umgebung:

      source vaivenv/bin/activate
      
  2. Installieren Sie die Abhängigkeiten:

    pip3 install visionai-0.0.5-py3-none-any.whl
    pip3 install google-cloud-storage
    
  3. Senden Sie Ihre Anfrage mit dem Python SDK.

    Ersetzen Sie die folgenden Variablen:

    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
    

    Die Ausgabe sollte in etwa so aussehen:

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

Ausgabe prüfen

Nachdem die Verarbeitung Ihres Videos abgeschlossen ist, können Sie sich die Ausgabe in Ihrem Cloud Storage-Bucket ansehen. Die generierten unkenntlich gemachten Videodateien befinden sich im selben Cloud Storage-Bucket wie das Quellvideo.

  1. Listen Sie alle Objekte in Ihrem Bucket mit dem Befehl gcloud storage ls auf:

    gcloud storage ls gs://bucket
    

    Die Quell- und Ausgabedateien sollten in etwa so aussehen:

    test1.mp4
    test2.mp4
    test1_deid_output.mp4
    test2_deid_output.mp4
    
  2. Optional. Laden Sie die Ausgabedateien mit dem Befehl gcloud storage cp lokal herunter und sehen Sie sich die unkenntlich gemachten Videos an:

    gcloud storage cp gs://BUCKET_NAME/FILE_NAME .
    

Bereinigen

Damit Ihrem Google Cloud-Konto die in dieser Anleitung verwendeten Ressourcen nicht in Rechnung gestellt werden, löschen Sie entweder das Projekt, das die Ressourcen enthält, oder Sie behalten das Projekt und löschen die einzelnen Ressourcen.

Nächste Schritte