Menggunakan model Wajah Buram dengan Python SDK


Tutorial ini menunjukkan cara menggunakan Python SDK untuk memburamkan wajah dalam video. Contoh ini memburamkan file video dari bucket Cloud Storage dan menghasilkan output video yang diburamkan. Video output ini disimpan ke bucket Cloud Storage yang sama dengan video sumber.

Tujuan

Tutorial ini menunjukkan cara melakukan hal berikut:

  • Membuat bucket Cloud Storage.
  • Upload file video lokal ke bucket.
  • Kirim permintaan menggunakan Python SDK.
  • Melihat video output yang diburamkan.

Biaya

Dalam dokumen ini, Anda akan menggunakan komponen Google Cloudyang dapat ditagih berikut:

Untuk membuat perkiraan biaya berdasarkan proyeksi penggunaan Anda, gunakan kalkulator harga. Pengguna Google Cloud baru mungkin memenuhi syarat untuk mendapatkan uji coba gratis.

Setelah menyelesaikan tugas yang dijelaskan dalam dokumen ini, Anda dapat menghindari penagihan berkelanjutan dengan menghapus resource yang Anda buat. Untuk mengetahui informasi selengkapnya, lihat Pembersihan.

Sebelum memulai

  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. Dapatkan kode sumber Vertex AI Vision SDK:
    git clone https://github.com/google/visionai.git

    Contoh Python terletak di direktori visionai/python/example/.

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

Menambahkan file input ke Cloud Storage

Sebelum dapat mengirim permintaan menggunakan Python SDK, buat bucket Cloud Storage dan upload video lokal untuk digunakan sebagai input.

  1. Membuat bucket Cloud Storage:

    gcloud storage buckets create gs://BUCKET_NAME
    
  2. Upload file video lokal ke bucket baru:

    gcloud storage cp LOCAL_FILE gs://BUCKET_NAME
    

Menginstal dependensi dan mengirim permintaan

Setelah membuat bucket Cloud Storage untuk video input dan output serta menambahkan video lokal, instal dependensi yang diperlukan dan kirim permintaan Anda.

  1. Opsional. Siapkan lingkungan virtual Anda:

    1. Jika belum diinstal, instal virtualenv:

      sudo apt-get install python3-venv
      
    2. Buat lingkungan virtual baru:

      python3 -m venv vaivenv
      
    3. Aktifkan lingkungan virtual Anda:

      source vaivenv/bin/activate
      
  2. Instal dependensi:

    pip3 install visionai-0.0.5-py3-none-any.whl
    pip3 install google-cloud-storage
    
  3. Kirim permintaan Anda dengan Python SDK.

    Lakukan penggantian variabel berikut:

    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
    

    Anda akan melihat output yang mirip dengan berikut ini:

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

Memeriksa output

Setelah video selesai diproses, Anda dapat memeriksa output-nya di bucket Cloud Storage. File video yang diburamkan yang dihasilkan akan berada di bucket Cloud Storage yang sama dengan video sumber.

  1. Buat daftar semua objek di bucket Anda dengan perintah gcloud storage ls:

    gcloud storage ls gs://bucket
    

    Anda akan melihat file sumber dan file output yang mirip dengan berikut ini:

    test1.mp4
    test2.mp4
    test1_deid_output.mp4
    test2_deid_output.mp4
    
  2. Opsional. Download file output secara lokal dengan perintah gcloud storage cp dan lihat video yang diburamkan:

    gcloud storage cp gs://BUCKET_NAME/FILE_NAME .
    

Pembersihan

Agar tidak perlu membayar biaya pada akun Google Cloud Anda untuk resource yang digunakan dalam tutorial ini, hapus project yang berisi resource tersebut, atau simpan project dan hapus setiap resource.

Langkah berikutnya