コマンドラインを使用して動画にアノテーションを付ける
このページでは、所有しているプロジェクトで curl
を使用して Video Intelligence API リクエストを行う方法について説明します。
このページの手順に従うことも、このクイックスタートを Google Cloud トレーニング ラボとして試すこともできます。
準備
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the required API.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
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
. - Click Create and continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
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. - Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the required API.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
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
. - Click Create and continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
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. - Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
アノテーション付き動画のリクエストを行う
gcloud
gcloud CLI を使用して、分析する動画のパスでdetect-labels
コマンドを呼び出します。
gcloud ml video detect-labels gs://YOUR_BUCKET/YOUR_OBJECT
コマンドライン
curl
を使用してvideos: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"
リクエストを処理するオペレーションが作成され、オペレーション名を含むレスポンスが返されます。
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID" }
オペレーションの情報は、次のサンプルの OPERATION_NAME を前の手順で返された名前に置き換えて
v1.operations
エンドポイントを呼び出すことでリクエストできます。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": "/YOUR_BUCKET/YOUR_OBJECT", "progressPercent": 100, "startTime": "2020-04-01T22:13:17.978847Z", "updateTime": "2020-04-01T22:13:29.576004Z" } ] }, "done": true, ... }
リクエストを送信して 1 分ほど待つと、アノテーションの結果が返されます。
{ "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 コンソールを使用して、不要なプロジェクトを削除します。
次のステップ
- 入門ガイドを参照する。
- サンプルとチュートリアルを参照する。
- API リファレンスを参照して、すべての API 呼び出しの詳細を確認する。