Cloud Vision API を使用して画像内のラベルを検出する

このクイックスタートでは、次の方法について説明します。

  • Cloud Storage バケットを作成する。
  • 画像を Cloud Storage にアップロードし、ファイルを一般公開する。
  • その画像を指定して Vision API にリクエストを送信する。

これらの手順を行うための所要時間はおよそ 5 分です。Cloud Storage には 5 GB まで無料でデータを格納できます。1 か月に Vision API に機能リクエストを 1,000 回まで無料で送信することができます。

始める前に

まだプロジェクトの設定と Cloud Storage バケットの作成を行っていない場合は、次の手順を実施します。

プロジェクトを設定する

  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 Cloud Vision API.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  7. Enable the Cloud Vision API.

    Enable the API

Cloud Storage バケットを作成する

  1. In the Google Cloud console, go to the Cloud Storage Buckets page.

    Go to Buckets

  2. Click Create.
  3. On the Create a bucket page, enter your bucket information. To go to the next step, click Continue.
    1. For Name your bucket, enter a unique bucket name. Don't include sensitive information in the bucket name, because the bucket namespace is global and publicly visible.
    2. In the Choose where to store your data section, do the following:
      1. Select a Location type.
      2. Choose a location where your bucket's data is permanently stored from the Location type drop-down menu.
      3. To set up cross-bucket replication, select Add cross-bucket replication via Storage Transfer Service and follow these steps:

        Set up cross-bucket replication

        1. In the Bucket menu, select a bucket.
        2. In the Replication settings section, click Configure to configure settings for the replication job.

          The Configure cross-bucket replication pane appears.

          • To filter objects to replicate by object name prefix, enter a prefix that you want to include or exclude objects from, then click Add a prefix.
          • To set a storage class for the replicated objects, select a storage class from the Storage class menu. If you skip this step, the replicated objects will use the destination bucket's storage class by default.
          • Click Done.
    3. In the Choose how to store your data section, do the following:
      1. In the Set a default class section, select the following: Standard.
      2. To enable hierarchical namespace, in the Optimize storage for data-intensive workloads section, select Enable hierarchical namespace on this bucket.
    4. In the Choose how to control access to objects section, select whether or not your bucket enforces public access prevention, and select an access control method for your bucket's objects.
    5. In the Choose how to protect object data section, do the following:
      • Select any of the options under Data protection that you want to set for your bucket.
        • To enable soft delete, click the Soft delete policy (For data recovery) checkbox, and specify the number of days you want to retain objects after deletion.
        • To set Object Versioning, click the Object versioning (For version control) checkbox, and specify the maximum number of versions per object and the number of days after which the noncurrent versions expire.
        • To enable the retention policy on objects and buckets, click the Retention (For compliance) checkbox, and then do the following:
          • To enable Object Retention Lock, click the Enable object retention checkbox.
          • To enable Bucket Lock, click the Set bucket retention policy checkbox, and choose a unit of time and a length of time for your retention period.
      • To choose how your object data will be encrypted, expand the Data encryption section (), and select a Data encryption method.
  4. Click Create.

Cloud Vision にリクエストを送信する

  1. サンプル画像ファイル(demo-img.jpg)のローカル バージョンを保存します。

  2. Google Cloud コンソールで、Cloud Storage の [バケット] ページに移動します。

    [バケット] に移動

  3. 前のセクションで作成したバケットを選択します。

  4. [ファイルをアップロード] をクリックし、Cloud Storage バケットにアップロードする demo-img.jpg ファイルのローカル バージョンを選択します。

    [ファイルをアップロード] ボタン
    これがいまアップロードされた画像ファイルです。
    ジャカルタで自転車に乗る 2 人の女性。
    画像クレジット: Rohiim ArifulUnsplash より

  5. ファイルがアップロードされ、Cloud Storage バケットに表示されたら、画像を一般公開で共有します。

  6. Try this method セクションで、image.source.imageUri フィールドの cloud-samples-data/vision を、demo-img.jpg ファイルをアップロードした Cloud Storage バケットの名前に置き換えて、対話型 API Explorer テンプレートを完成させます。リクエストの本文は次のようになります。

    {
      "requests": [
        {
          "features": [
            {
              "type": "LABEL_DETECTION"
            }
          ],
          "image": {
            "source": {
              "imageUri": "gs://cloud-samples-data/vision/demo-img.jpg"
            }
          }
        }
      ]
    }

  7. [実行] をクリックして、リクエストをサービスに送信します。JSON レスポンスはリクエスト本文の後に表示されます。

以上で完了です。Cloud Vision に最初の images.annotate リクエストが送信されました。

クリーンアップ

不要な Google Cloud 料金が発生しないようにするには、Google Cloud コンソールを使用して、不要になった Cloud Storage バケット(とプロジェクト)を削除します。

次のステップ

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