Use the Face Blur model with the Python SDK


This tutorial shows you how to use the Python SDK to blur faces in video. The example blurs video files from a Cloud Storage bucket and generates blurred video outputs. These output videos are stored to the same Cloud Storage bucket as the source videos.

Objectives

This tutorial shows you how to do the following:

  • Create a Cloud Storage bucket.
  • Upload a local video file to the bucket.
  • Send a request using the Python SDK.
  • View blurred output videos.

Costs

In this document, you use the following billable components of Google Cloud:

To generate a cost estimate based on your projected usage, use the pricing calculator .

New Google Cloud users might be eligible for a free trial .

When you finish the tasks that are described in this document, you can avoid continued billing by deleting the resources that you created. For more information, see Clean up .

Before you begin

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

    The Python examples are located in the visionai/python/example/ directory.

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

Add input files to Cloud Storage

Before you can send a request using the Python SDK, create a Cloud Storage bucket and upload a local video to use as input.

  1. Create a Cloud Storage bucket:

     gcloud  
    storage  
    buckets  
    create  
    gs:// BUCKET_NAME 
     
    
  2. Upload a local video file to the new bucket:

      gcloud 
     storage 
     cp 
      LOCAL_FILE 
     
     gs 
     : 
     // 
      BUCKET_NAME 
     
     
    

Install dependencies and send the request

After you create your Cloud Storage bucket for input and output videos and add a local video, install the necessary dependencies and send your request.

  1. Optional. Set up your virtual environment:

    1. If not installed, install virtualenv :

       sudo  
      apt-get  
      install  
      python3-venv 
      
    2. Create a new virtual environment:

       python3  
      -m  
      venv  
       vaivenv 
       
      
    3. Activate your virtual environment:

        source 
        
       vaivenv 
      /bin/activate 
      
  2. Install dependencies:

     pip3  
    install  
    visionai-0.0.5-py3-none-any.whl
    pip3  
    install  
    google-cloud-storage 
    
  3. Send your request with the Python SDK.

    Make the following variable substitutions:

    • PROJECT_ID : Your Google Cloud project ID.
    • LOCATION_ID : Your location ID. For example, us-central1 . More information . Supported regions .
    • BUCKET_NAME : The Cloud Storage bucket you created.
     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 
     
    

    You should see output similar to the following:

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

Examine output

After your video has finished processing you can examine the output in your Cloud Storage bucket. The generated blurred video files will be in the same Cloud Storage bucket as the source video.

  1. List all objects in your bucket with the gcloud storage ls command:

      gcloud 
     storage 
     ls 
     gs 
     : 
     // 
     bucket 
     
    

    You should see the source files and output files similar to the following:

      test1 
     . 
     mp4 
     test2 
     . 
     mp4 
     test1_deid_output 
     . 
     mp4 
     test2_deid_output 
     . 
     mp4 
     
    
  2. Optional. Download the output files locally with the gcloud storage cp command and view the blurred videos:

      gcloud 
     storage 
     cp 
     gs 
     : 
     // 
      BUCKET_NAME 
     
     / 
      FILE_NAME 
     
     . 
     
    

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.

What's next

Create a Mobile Website
View Site in Mobile | Classic
Share by: