コマンドラインを使用して動画にアノテーションを付ける
このページでは、所有しているプロジェクトで curl を使用して Video Intelligence API リクエストを行う方法について説明します。
このページの手順に従うことも、このクイックスタートをGoogle Cloud トレーニング ラボとして試すこともできます。
始める前に
- Google Cloud アカウントにログインします。 Google Cloudを初めて使用する場合は、 アカウントを作成して、実際のシナリオでの Google プロダクトのパフォーマンスを評価してください。新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $300 分を差し上げます。
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.-
Google Cloud CLI をインストールします。
-
外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。
-
gcloud CLI を初期化するには、次のコマンドを実行します。
gcloud init -
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.-
Google Cloud CLI をインストールします。
-
外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。
-
gcloud CLI を初期化するには、次のコマンドを実行します。
gcloud init
必要なロール
動画にアノテーションを付けるために必要な権限を取得するには、プロジェクトに対する次の IAM ロールを付与するよう管理者に依頼してください。
-
Service Usage ユーザー(
roles/serviceusage.serviceUsageConsumer) -
ストレージ管理者(
roles/storage.admin)
ロールの付与については、プロジェクト、フォルダ、組織へのアクセス権の管理をご覧ください。
必要な権限は、カスタムロールや他の事前定義ロールから取得することもできます。
バケットの作成
開発環境で、gcloud storage buckets create コマンドを使用してバケットを作成します。
コマンドを実行する前に、次の値を置き換えます。
BUCKET_LOCATION: バケットにデータを保存する地理的な位置。例:US
gcloud storage buckets create gs://videointelligence-quickstart-bucket --location=BUCKET_LOCATION
リクエストが成功すると、コマンドから次のメッセージが返されます。
Creating gs://videointelligence-quickstart-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"
リクエストを処理するオペレーションが作成され、オペレーション名を含むレスポンスが返されます。
{ "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": "/videointelligence-quickstart-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": "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コマンドを使用し、バケットとその中のすべてのものを削除します。gcloud storage rm gs://videointelligence-quickstart-bucket --recursive
このクイックスタート用に作成したプロジェクトが不要になった場合は、プロジェクトを削除します。
- Google Cloud コンソールで [リソースの管理] ページに移動します。
- プロジェクト リストで、削除するプロジェクトを選択し、[削除] をクリックします。
- ダイアログでプロジェクト ID を入力し、[シャットダウン] をクリックしてプロジェクトを削除します。
次のステップ
- 入門ガイドを参照する。
- サンプルとチュートリアルを参照する。
- API リファレンスを参照して、すべての API 呼び出しの詳細を確認する。