Détecter des étiquettes dans une image à l'aide de l'API Cloud Vision

Ce guide de démarrage rapide vous montre comment :

  • créer un bucket Cloud Storage ;
  • importer votre image dans Cloud Storage et rendre le fichier accessible au public ;
  • envoyer une requête à l'API Vision avec cette image.

Ces étapes devraient durer environ cinq minutes. Vous pouvez stocker sans frais jusqu'à 5 Go de données dans Cloud Storage et envoyer jusqu'à 1 000 demandes de fonctionnalité par mois à l'API Vision sans frais.

Avant de commencer

Si vous ne l'avez pas déjà fait, suivez les instructions ci-dessous pour configurer votre projet et créer un bucket Cloud Storage.

Configurer votre projet

  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. Verify 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. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Vision API.

    Enable the API

  8. Créer un bucket 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.

    Envoyer une requête à Cloud Vision

    1. Enregistrez une version locale de l'exemple de fichier image (demo-img.jpg).

    2. Dans la console Google Cloud , accédez à la page Buckets de Cloud Storage.

      Accéder à la page "Buckets"

    3. Sélectionnez le bucket que vous avez créé dans la section précédente.

    4. Cliquez sur Importer des fichiers et sélectionnez la version locale du fichier demo-img.jpg à importer dans votre bucket Cloud Storage.

      Bouton "Importer des fichiers"
      Voici le fichier image que vous venez d'importer :
      Deux femmes qui font du vélo à Jakarta
      Crédit image : Rohiim Ariful sur Unsplash

    5. Une fois le fichier importé et présent dans le bucket Cloud Storage, rendez l'image publique.

    6. Dans la section Essayer cette méthode, renseignez le modèle interactif d'explorateur API en remplaçant cloud-samples-data/vision dans le champ image.source.imageUri par le nom du bucket Cloud Storage dans lequel vous avez importé le fichier demo-img.jpg. Le corps de la requête doit se présenter comme suit :

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

    7. Cliquez sur Exécuter pour envoyer la requête au service. La réponse JSON s'affiche après le corps de la requête.

    Félicitations ! Vous avez envoyé votre première requête images.annotate à Cloud Vision.

    Effectuer un nettoyage

    Pour éviter des frais Google Cloud inutiles, supprimez votre bucket Cloud Storage (et votre projet) à l'aide de la console Google Cloud si vous n'en avez plus besoin.

    Étapes suivantes