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

本頁說明如何使用 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.

    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. 安裝 Google Cloud CLI。

  7. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  8. 執行下列指令,初始化 gcloud CLI:

    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. 安裝 Google Cloud CLI。

  14. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  15. 執行下列指令,初始化 gcloud CLI:

    gcloud init

必要的角色

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

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

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

建立值區

在開發環境中,使用 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 指令將影片上傳至您建立的 bucket。

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

  • 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

指令列

使用 curlvideos: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」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 帳戶收取本頁面所用資源的費用,請採取下列任一做法:

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

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

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

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

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

後續步驟