コマンドラインを使用して画像内のラベルを検出する

このページでは、REST インターフェースcurl コマンドを使用して、Vision API に 3 つの特徴検出リクエストとアノテーション リクエストを送信する方法について説明します。

Vision API を使用すると、Google の視覚認識技術をデベロッパーのアプリケーションに簡単に統合できます。Vision API に画像データと目的特徴タイプを送信すると、目的の画像属性に基づく対応するレスポンスが返されます。利用可能な特徴タイプの詳細については、Vision API のすべての機能の一覧をご覧ください。

始める前に

  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. Install the Google Cloud CLI.

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

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

    gcloud init
  5. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

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

  7. Enable the Vision API:

    gcloud services enable vision.googleapis.com
  8. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/storage.objectViewer

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
    • Replace PROJECT_ID with your project ID.
    • Replace USER_IDENTIFIER with the identifier for your user account. For example, user:myemail@example.com.

    • Replace ROLE with each individual role.
  9. Install the Google Cloud CLI.

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

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

    gcloud init
  12. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

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

  14. Enable the Vision API:

    gcloud services enable vision.googleapis.com
  15. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/storage.objectViewer

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
    • Replace PROJECT_ID with your project ID.
    • Replace USER_IDENTIFIER with the identifier for your user account. For example, user:myemail@example.com.

    • Replace ROLE with each individual role.

画像アノテーション リクエストを作成する

始める前にの手順を完了すると、Vision API を使用して画像ファイルにアノテーションを付けられるようになります。

この例では、次の画像で curl を使用して Vision API にリクエストを送信します。

Cloud Storage URI:

gs://cloud-samples-data/vision/using_curl/shanghai.jpeg

HTTPS URL:

https://console.cloud.google.com/storage/browser/cloud-samples-data/vision/using_curl/shanghai.jpeg

上海の街の画像
画像クレジット: Steve LongUnsplash より抜粋

JSON リクエストを作成する

次の request.json ファイルでは、3 つの images:annotate 機能をリクエストする方法と、レスポンスの結果を制限する方法について説明します。

次のテキストを含む JSON リクエスト ファイルを作成し、作業ディレクトリに request.json 書式なしテキスト ファイルとして保存します。

request.json

{
  "requests": [
    {
      "image": {
        "source": {
          "imageUri": "gs://cloud-samples-data/vision/using_curl/shanghai.jpeg"
        }
      },
      "features": [
        {
          "type": "LABEL_DETECTION",
          "maxResults": 3
        },
        {
          "type": "OBJECT_LOCALIZATION",
          "maxResults": 1
        },
        {
          "type": "TEXT_DETECTION",
          "maxResults": 1,
          "model": "builtin/latest"
        }
      ]
    }
  ]
}

リクエストを送信する

request.json の curl と本文のコンテンツを使用して、リクエストを Vision API に送信します。コマンドラインで次のように入力します。

curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "x-goog-user-project: PROJECT_ID" \
    -H "Content-Type: application/json; charset=utf-8" \
    https://vision.googleapis.com/v1/images:annotate -d @request.json

レスポンスを解釈する

以下のような JSON レスポンスが表示されます。

各アノテーション型に maxResults が指定された JSON 本文のリクエスト。したがって、レスポンスの JSON には次のようになります。

ラベル検出の結果

  1. 説明: 「人物」、スコア: 0.950
  2. 説明: 「通り」、スコア: 0.891
  3. 説明: 「交通手段」、スコア: 0.890
ラベル検出の結果を含む上海の街の画像。
画像: Steve LongUnsplash より (アノテーション入り)

テキスト検出の結果

  • テキスト: 牛牛面馆\ n
  • 頂点: (x: 159、y: 212)、(x: 947、y: 212)、(x: 947、y: 354)、(x: 159、y: 354)
テキスト検出の結果を含む上海の街の画像。
画像: Steve LongUnsplash より (アノテーション入り)

オブジェクト検出の結果

  • 名前: 「人物」、スコア: 0.944
  • 正規化された頂点: (x: 0.260、y: 0.468)、(x: 0.407、y: 0.468)、(x: 0.407、y: 0.895)、(x: 0.260、y: 0.895)
オブジェクト検出の結果を含む上海の街の画像。
画像: Steve LongUnsplash より (アノテーション入り)

これで完了です。Vision API への最初のリクエストを送信しました。

クリーンアップ

このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、Google Cloud プロジェクトとそのリソースをまとめて削除してください。

Optional: Revoke credentials from the gcloud CLI.

gcloud auth revoke

次のステップ

  • すべての機能タイプとその用途のリストをご覧ください。
  • お使いのプログラミング言語に対応した Vision API クライアント ライブラリを使用して、Vision API の使用を開始しましょう。
  • 入門ガイドで機能タイプの詳細や、個々のファイルまたは画像のアノテーションやサンプルをご覧ください。
  • 一括処理で画像ファイル(PDF / TIFF / GIF)にアノテーションを設定する方法をご確認ください。
  • クライアント ライブラリのコードサンプルの全体的なリストをご覧ください。