このページでは、curl
を使用して Video Intelligence API リクエストを行う方法について説明します。
このページの手順に従うことも、このクイックスタートを Google Cloud トレーニング ラボとして試すこともできます。
準備
- Google アカウントにログインします。
Google アカウントをまだお持ちでない場合は、新しいアカウントを登録します。
-
Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。
-
Cloud プロジェクトに対して課金が有効になっていることを確認します。プロジェクトに対して課金が有効になっていることを確認する方法を学習する。
- 必要な API を有効にします。
-
サービス アカウントを作成します。
-
Cloud Console で [サービス アカウントの作成] ページに移動します。
[サービス アカウントの作成] に移動 - プロジェクトを選択します。
-
[サービス アカウント名] フィールドに名前を入力します。 Cloud Console は、この名前に基づいて [サービス アカウント ID] フィールドに入力します。
[サービス アカウントの説明] フィールドに説明を入力します。例:
Service account for quickstart
。 -
[完了] をクリックして、サービス アカウントの作成を完了します。
ブラウザ ウィンドウを閉じないでください。次のステップでこれを使用します。
-
-
サービス アカウント キーを作成します。
- Cloud Console で、作成したサービス アカウントのメールアドレスをクリックします。
- [キー] をクリックします。
- [キーを追加] をクリックし、[新しいキーを作成] をクリックします。
- [CREATE] をクリックします。JSON キーファイルがパソコンにダウンロードされます。
- [閉じる] をクリックします。
-
環境変数
GOOGLE_APPLICATION_CREDENTIALS
を、サービス アカウント キーが含まれる JSON ファイルのパスに設定します。 この変数は現在のシェル セッションにのみ適用されるため、新しいセッションを開く場合は、変数を再度設定します。 - Cloud SDK をインストールして初期化します。
アノテーション付き動画のリクエストを行う
gcloud
gcloud
コマンドライン ツールを使用して、分析する動画のパスで 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" }
オペレーションの情報をリクエストするには、
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": "/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 Platform 料金が発生しないようにするには、Cloud Console を使用して、不要なプロジェクトを削除します。
次のステップ
- 入門ガイドを参照する。
- サンプルとチュートリアルを参照する。
- API リファレンスを参照して、すべての API 呼び出しの詳細を確認する。