使用指令列為影片加上註解

本頁面說明如何使用 curl,為您擁有的專案提出 Video Intelligence API 要求。

您可以按照這個頁面的步驟操作,也可以嘗試使用這個快速入門指南做為Google Cloud 訓練實驗室。

透過研究室嘗試

事前準備

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自體驗產品的實際應用成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
  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, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  6. Install the Google Cloud CLI.

  7. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  8. To initialize the gcloud CLI, run the following command:

    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, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  13. Install the Google Cloud CLI.

  14. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  15. To initialize the gcloud CLI, run the following command:

    gcloud init

必要的角色

如要取得為影片加上註解所需的權限,請要求管理員授予您專案的下列 IAM 角色:

如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

您或許也能透過自訂角色或其他預先定義的角色,取得必要權限。

建立 Bucket

在開發環境中,使用 gcloud storage buckets create 指令建立 bucket。

執行指令前,請先替換下列值:

  • BUCKET_LOCATION:您要在 bucket 中儲存資料的地理位置。例如:US。
gcloud storage buckets create gs://videointelligence-quickstart-bucket --location=BUCKET_LOCATION

如果要求成功,指令會傳回下列訊息:

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

將影片上傳至 bucket

在開發環境中,使用 gcloud storage cp 指令將影片上傳至您建立的值區。

執行指令前,請先替換下列值:

  • OBJECT_LOCATION:物件的本機路徑。例如:Desktop/dog.mp4。
gcloud storage cp OBJECT_LOCATION gs://videointelligence-quickstart-bucket

如果成功,回應會類似以下範例:

Completed files 1/1 | 164.3kiB/164.3kiB

為影片加上註解

gcloud

使用 gcloud CLI,在要分析的影片路徑上呼叫 detect-labels 指令。

執行指令前,請先替換下列值:

  • YOUR_OBJECT:上傳影片的檔案名稱。例如:dog.mp4。
gcloud ml video detect-labels gs://videointelligence-quickstart-bucket/YOUR_OBJECT

指令列

使用 curl 對 videos:annotate 方法發出 POST 要求。

執行指令前,請先替換下列值:

  • YOUR_OBJECT:上傳影片的檔案名稱。例如: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 會建立「作業」來處理要求。回應含有以下作業名稱:

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

您可以呼叫 v1.operations 端點,要求作業相關資訊。請將下方範例中的 OPERATION_NAME 替換為上一步傳回的名稱:

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

這個指令會傳回作業的相關資訊。作業完成後,就會納入 done 欄位並設為 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,
  ...
}

大約一分鐘後,要求會傳回註解結果。結果應如下所示:

{
  "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
              }
            ]
          }
        ]
      }
    ]
  }
}
   

恭喜!您已傳送第一個要求到 Video Intelligence API。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請採取下列任一做法:

  • 如要繼續使用您建立的專案,請使用 gcloud storage rm 指令搭配 --recursive 旗標,刪除 bucket 及其內容:

    gcloud storage rm gs://videointelligence-quickstart-bucket --recursive
  • 如果您不需要為本快速入門導覽課程建立的專案,請刪除該專案。

    1. 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。

      前往「Manage resources」(管理資源)

    2. 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
    3. 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。

後續步驟