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

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

您可以按照本頁提供的步驟,或依 Google Cloud 訓練研究室程序嘗試本快速入門。

透過研究室嘗試

事前準備

  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. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the required API.

    Enable the API

  5. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  6. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  7. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.

  8. Install the Google Cloud CLI.

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

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

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

    Go to project selector

  12. Make sure that billing is enabled for your Google Cloud project.

  13. Enable the required API.

    Enable the API

  14. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  15. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  16. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.

  17. Install the Google Cloud CLI.

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

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

    gcloud init
  20. 發出註解影片要求

    gcloud

    使用 gcloud CLI 對欲分析影片的路徑呼叫 detect-labels 指令。

    gcloud ml video detect-labels gs://YOUR_BUCKET/YOUR_OBJECT
    

    指令列

    1. 使用 curlvideos:annotate 方法提出 POST 要求,並使用 gcloud auth application-default print-access-token 指令將存取權杖貼到服務帳戶:

      curl -X POST \
        -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
        -H "Content-Type: application/json; charset=utf-8" \
        --data '{"inputUri":"gs://YOUR_BUCKET/YOUR_OBJECT","features":["LABEL_DETECTION"]}'\
        "https://videointelligence.googleapis.com/v1/videos:annotate"
    2. Video Intelligence API 會建立「作業」來處理要求。回應含有以下作業名稱:

      {
        "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID"
      }
    3. 您可以呼叫 v1.operations 端點,並將下方範例中的 OPERATION_NAME 替換成前一步驟傳回的名稱,以要求作業的相關資訊:

       curl -X GET \
         -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
         https://videointelligence.googleapis.com/v1/OPERATION_NAME
    4. 您會看見作業的相關資訊。如果作業完成,就會納入 done 欄位並設為 true

      {
        "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID",
        "metadata": {
        "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoProgress",
          "annotationProgress": [
            {
              "inputUri": "/YOUR_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": "YOUR_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": "/YOUR_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 費用,請透過Google Cloud console 刪除不需要的專案。

後續步驟