Membuat anotasi video menggunakan command line

Halaman ini menunjukkan cara membuat permintaan Video Intelligence API dengan curl untuk project yang Anda miliki.

Anda dapat mengikuti langkah-langkah di halaman ini atau mencoba memulai cepat ini sebagai Google Cloud Lab pelatihan.

Mencobanya dengan lab

Sebelum memulai

  1. Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  4. Verify that billing is enabled for your Google Cloud project.

  5. Enable the Video Intelligence API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  6. Instal Google Cloud CLI.

  7. Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.

  8. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    gcloud init
  9. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  10. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  11. Verify that billing is enabled for your Google Cloud project.

  12. Enable the Video Intelligence API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  13. Instal Google Cloud CLI.

  14. Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.

  15. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    gcloud init

Peran yang diperlukan

Untuk mendapatkan izin yang Anda perlukan untuk memberi anotasi pada video, minta administrator untuk memberi Anda peran IAM berikut pada project:

Untuk mengetahui informasi selengkapnya tentang pemberian peran, lihat Mengelola akses ke project, folder, dan organisasi.

Anda mungkin juga bisa mendapatkan izin yang diperlukan melalui peran khusus atau peran bawaan lainnya.

Membuat bucket

Di lingkungan pengembangan Anda, gunakan perintah gcloud storage buckets create untuk membuat bucket.

Sebelum menjalankan perintah, ganti nilai berikut:

  • BUCKET_LOCATION: Lokasi geografis tempat Anda ingin menyimpan data di bucket. Contoh, US.
gcloud storage buckets create gs://videointelligence-quickstart-bucket --location=BUCKET_LOCATION

Jika permintaan berhasil, perintah akan menampilkan pesan berikut ini:

Creating gs://videointelligence-quickstart-bucket/...

Mengupload video ke bucket

Di lingkungan pengembangan Anda, gunakan perintah gcloud storage cp untuk mengupload video ke bucket yang Anda buat.

Sebelum menjalankan perintah, ganti nilai berikut:

  • OBJECT_LOCATION: Jalur lokal ke objek Anda. Contoh, Desktop/dog.mp4.
gcloud storage cp OBJECT_LOCATION gs://videointelligence-quickstart-bucket

Jika berhasil, responsnya akan terlihat seperti contoh berikut:

Completed files 1/1 | 164.3kiB/164.3kiB

Anotasi video

gcloud

Gunakan gcloud CLI untuk memanggil perintah detect-labels di jalur video yang akan dianalisis.

Sebelum menjalankan perintah, ganti nilai berikut:

  • YOUR_OBJECT: Nama file video yang Anda upload. Contoh, dog.mp4.
gcloud ml video detect-labels gs://videointelligence-quickstart-bucket/YOUR_OBJECT

Command-line

Gunakan curl untuk membuat permintaan POST ke metode videos:annotate.

Sebelum menjalankan perintah, ganti nilai berikut:

  • YOUR_OBJECT: Nama file video yang Anda upload. Contoh, dog.mp4.
curl -X POST \
  -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
  -H "Content-Type: application/json; charset=utf-8" \
  --data '{"inputUri":"gs://videointelligence-quickstart-bucket/YOUR_OBJECT","features":["LABEL_DETECTION"]}'\
  "https://videointelligence.googleapis.com/v1/videos:annotate"

Video Intelligence API membuat operasi untuk memproses permintaan Anda. Respons mencakup nama operasi:

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID"
}

Anda dapat meminta informasi tentang operasi dengan memanggil endpoint v1.operations, mengganti OPERATION_NAME dalam contoh di bawah dengan nama yang ditampilkan di langkah sebelumnya:

  curl -X GET \
    -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
    https://videointelligence.googleapis.com/v1/OPERATION_NAME

Perintah ini menampilkan informasi terkait operasi Anda. Jika operasi telah selesai, kolom done akan disertakan dan ditetapkan ke true:

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID",
  "metadata": {
  "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoProgress",
    "annotationProgress": [
      {
        "inputUri": "/videointelligence-quickstart-bucket/YOUR_OBJECT",
        "progressPercent": 100,
        "startTime": "2020-04-01T22:13:17.978847Z",
        "updateTime": "2020-04-01T22:13:29.576004Z"
      }
    ]
  },
  "done": true,
  ...
}

Setelah sekitar satu menit, permintaan akan menampilkan hasil anotasi. Hasilnya akan terlihat seperti berikut:

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoProgress",
    "annotationProgress": [
      {
        "inputUri": "videointelligence-quickstart-bucket/YOUR_OBJECT",
        "progressPercent": 100,
        "startTime": "2020-04-01T22:13:17.978847Z",
        "updateTime": "2020-04-01T22:13:29.576004Z"
      }
    ]
  },
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoResponse",
    "annotationResults": [
      {
        "inputUri": "/videointelligence-quickstart-bucket/YOUR_OBJECT",
        "segmentLabelAnnotations": [
          {
            "entity": {
              "entityId": "/m/07bsy",
              "description": "transport",
              "languageCode": "en-US"
            },
            "segments": [
              {
                "segment": {
                  "startTimeOffset": "0s",
                  "endTimeOffset": "38.757872s"
                },
                "confidence": 0.81231534
              }
            ]
          },
         {
          "entity": {
              "entityId": "/m/01n32",
              "description": "city",
              "languageCode": "en-US"
            },
            "categoryEntities": [
              {
                "entityId": "/m/043rvww",
                "description": "geographical feature",
                "languageCode": "en-US"
              }
            ],
            "segments": [
              {
                "segment": {
                  "startTimeOffset": "0s",
                  "endTimeOffset": "38.757872s"
                },
                "confidence": 0.3942462
              }
            ]
          },
          ...
          {
            "entity": {
              "entityId": "/m/06gfj",
              "description": "road",
              "languageCode": "en-US"
            },
            "segments": [
              {
                "segment": {
                  "startTimeOffset": "0s",
                  "endTimeOffset": "38.757872s"
                },
                "confidence": 0.86698604
              }
            ]
          }
        ]
      }
    ]
  }
}
   

Selamat! Anda telah mengirimkan permintaan pertama ke Video Intelligence API.

Pembersihan

Agar akun Google Cloud Anda tidak dikenai biaya untuk resource yang digunakan di halaman ini, lakukan salah satu hal berikut:

  • Jika Anda ingin terus menggunakan project yang Anda buat, gunakan perintah gcloud storage rm dengan flag --recursive untuk menghapus bucket dan semua yang ada di dalamnya:

    gcloud storage rm gs://videointelligence-quickstart-bucket --recursive
  • Jika Anda tidak memerlukan project yang Anda buat untuk panduan memulai ini, hapus project tersebut.

    1. Di Konsol Google Cloud , buka halaman Manage resources.

      Buka Kelola resource

    2. Pada daftar project, pilih project yang ingin Anda hapus, lalu klik Delete.
    3. Pada dialog, ketik project ID, lalu klik Shut down untuk menghapus project.

Langkah berikutnya