Detectar etiquetas en una imagen mediante bibliotecas de cliente

En esta página se explica cómo empezar a usar la API Vision en tu lenguaje de programación favorito.


Para seguir las instrucciones paso a paso de esta tarea directamente en el editor de Cloud Shell, haz clic en Ayúdame:

Guíame


Antes de empezar

  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. Si utilizas un proveedor de identidades (IdP) externo, primero debes iniciar sesión en la CLI de gcloud con tu identidad federada.

  4. Para inicializar gcloud CLI, ejecuta el siguiente comando:

    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. Verify 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 the following:

    • PROJECT_ID: your project ID.
    • USER_IDENTIFIER: the identifier for your user account—for example, myemail@example.com.
    • ROLE: the IAM role that you grant to your user account.
  9. Install the Google Cloud CLI.

  10. Si utilizas un proveedor de identidades (IdP) externo, primero debes iniciar sesión en la CLI de gcloud con tu identidad federada.

  11. Para inicializar gcloud CLI, ejecuta el siguiente comando:

    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. Verify 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 the following:

    • PROJECT_ID: your project ID.
    • USER_IDENTIFIER: the identifier for your user account—for example, myemail@example.com.
    • ROLE: the IAM role that you grant to your user account.
  16. Instalar la biblioteca cliente

    Go

    go get cloud.google.com/go/vision/apiv1

    Java

    Para obtener más información sobre cómo configurar tu entorno de desarrollo de Java, consulta la guía de configuración del entorno de desarrollo de Java.

    If you are using Maven, add the following to your pom.xml file. For more information about BOMs, see The Google Cloud Platform Libraries BOM.

    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>com.google.cloud</groupId>
          <artifactId>libraries-bom</artifactId>
          <version>26.66.0</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    
    <dependencies>
      <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-vision</artifactId>
      </dependency>
    </dependencies>

    If you are using Gradle, add the following to your dependencies:

    implementation 'com.google.cloud:google-cloud-vision:3.70.0'

    If you are using sbt, add the following to your dependencies:

    libraryDependencies += "com.google.cloud" % "google-cloud-vision" % "3.70.0"

    If you're using Visual Studio Code, IntelliJ, or Eclipse, you can add client libraries to your project using the following IDE plugins:

    The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.

    Node.js

    Para obtener más información sobre cómo configurar tu entorno de desarrollo de Node.js, consulta la guía de configuración del entorno de desarrollo de Node.js.

    npm install @google-cloud/vision

    Python

    Para obtener más información sobre cómo configurar tu entorno de desarrollo de Python, consulta la guía de configuración del entorno de desarrollo de Python.

    pip install --upgrade google-cloud-vision

    Ejemplos de detección de etiquetas

    Ahora puedes usar la API Vision para solicitar información de una imagen, como la detección de etiquetas. Ejecuta el siguiente código para realizar tu primera solicitud de detección de etiquetas de imagen.

    Go

    Antes de probar este ejemplo, sigue las instrucciones de configuración de Go que se indican en la guía de inicio rápido de Vision con bibliotecas de cliente. Para obtener más información, consulta la documentación de referencia de la API Go de Vision.

    Para autenticarte en Vision, configura las credenciales predeterminadas de la aplicación. Para obtener más información, consulta el artículo Configurar la autenticación en un entorno de desarrollo local.

    
    // Sample vision-quickstart uses the Google Cloud Vision API to label an image.
    package main
    
    import (
    	"context"
    	"fmt"
    	"log"
    	"os"
    
    	vision "cloud.google.com/go/vision/apiv1"
    )
    
    func main() {
    	ctx := context.Background()
    
    	// Creates a client.
    	client, err := vision.NewImageAnnotatorClient(ctx)
    	if err != nil {
    		log.Fatalf("Failed to create client: %v", err)
    	}
    	defer client.Close()
    
    	// Sets the name of the image file to annotate.
    	filename := "../testdata/cat.jpg"
    
    	file, err := os.Open(filename)
    	if err != nil {
    		log.Fatalf("Failed to read file: %v", err)
    	}
    	defer file.Close()
    	image, err := vision.NewImageFromReader(file)
    	if err != nil {
    		log.Fatalf("Failed to create image: %v", err)
    	}
    
    	labels, err := client.DetectLabels(ctx, image, nil, 10)
    	if err != nil {
    		log.Fatalf("Failed to detect labels: %v", err)
    	}
    
    	fmt.Println("Labels:")
    	for _, label := range labels {
    		fmt.Println(label.Description)
    	}
    }
    

    Java

    Antes de probar este ejemplo, sigue las instrucciones de configuración de Java que se indican en la guía de inicio rápido de Vision con bibliotecas de cliente. Para obtener más información, consulta la documentación de referencia de la API Java de Vision.

    Para autenticarte en Vision, configura las credenciales predeterminadas de la aplicación. Para obtener más información, consulta el artículo Configurar la autenticación en un entorno de desarrollo local.

    // Imports the Google Cloud client library
    
    import com.google.cloud.vision.v1.AnnotateImageRequest;
    import com.google.cloud.vision.v1.AnnotateImageResponse;
    import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
    import com.google.cloud.vision.v1.EntityAnnotation;
    import com.google.cloud.vision.v1.Feature;
    import com.google.cloud.vision.v1.Feature.Type;
    import com.google.cloud.vision.v1.Image;
    import com.google.cloud.vision.v1.ImageAnnotatorClient;
    import com.google.protobuf.ByteString;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.ArrayList;
    import java.util.List;
    
    public class QuickstartSample {
      public static void main(String... args) throws Exception {
        // Initialize client that will be used to send requests. This client only needs to be created
        // once, and can be reused for multiple requests. After completing all of your requests, call
        // the "close" method on the client to safely clean up any remaining background resources.
        try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) {
    
          // The path to the image file to annotate
          String fileName = "./resources/wakeupcat.jpg";
    
          // Reads the image file into memory
          Path path = Paths.get(fileName);
          byte[] data = Files.readAllBytes(path);
          ByteString imgBytes = ByteString.copyFrom(data);
    
          // Builds the image annotation request
          List<AnnotateImageRequest> requests = new ArrayList<>();
          Image img = Image.newBuilder().setContent(imgBytes).build();
          Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
          AnnotateImageRequest request =
              AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
          requests.add(request);
    
          // Performs label detection on the image file
          BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
          List<AnnotateImageResponse> responses = response.getResponsesList();
    
          for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
              System.out.format("Error: %s%n", res.getError().getMessage());
              return;
            }
    
            for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
              annotation
                  .getAllFields()
                  .forEach((k, v) -> System.out.format("%s : %s%n", k, v.toString()));
            }
          }
        }
      }
    }

    Node.js

    Antes de probar este ejemplo, sigue las instrucciones de configuración de Node.js que se indican en la guía de inicio rápido de Vision con bibliotecas de cliente. Para obtener más información, consulta la documentación de referencia de la API Node.js de Vision.

    Para autenticarte en Vision, configura las credenciales predeterminadas de la aplicación. Para obtener más información, consulta el artículo Configurar la autenticación en un entorno de desarrollo local.

    async function quickstart() {
      // Imports the Google Cloud client library
      const vision = require('@google-cloud/vision');
    
      // Creates a client
      const client = new vision.ImageAnnotatorClient();
    
      // Performs label detection on the image file
      const [result] = await client.labelDetection('./resources/wakeupcat.jpg');
      const labels = result.labelAnnotations;
      console.log('Labels:');
      labels.forEach(label => console.log(label.description));
    }
    quickstart();

    Python

    Antes de probar este ejemplo, sigue las instrucciones de configuración de Python que se indican en la guía de inicio rápido de Vision con bibliotecas de cliente. Para obtener más información, consulta la documentación de referencia de la API Python de Vision.

    Para autenticarte en Vision, configura las credenciales predeterminadas de la aplicación. Para obtener más información, consulta el artículo Configurar la autenticación en un entorno de desarrollo local.

    
    # Imports the Google Cloud client library
    from google.cloud import vision
    
    
    
    def run_quickstart() -> vision.EntityAnnotation:
        """Provides a quick start example for Cloud Vision."""
    
        # Instantiates a client
        client = vision.ImageAnnotatorClient()
    
        # The URI of the image file to annotate
        file_uri = "gs://cloud-samples-data/vision/label/wakeupcat.jpg"
    
        image = vision.Image()
        image.source.image_uri = file_uri
    
        # Performs label detection on the image file
        response = client.label_detection(image=image)
        labels = response.label_annotations
    
        print("Labels:")
        for label in labels:
            print(label.description)
    
        return labels
    
    

    Ejecutar la muestra

    Para ejecutar los ejemplos anteriores, usa los siguientes comandos. Si has clonado el repositorio de GitHub del lenguaje de ejemplo específico, quita el comentario de la primera línea:

    Go

    # cd golang-samples/vision/vision_quickstart/
    go run .
    

    Java

    # cd java-docs-samples/vision/snippets/src/main/java/com/example/vision/quickstart/
    javac QuickstartSample.java
    java QuickstartSample.java
    

    Node.js

    # cd nodejs-docs-samples/vision/
    node quickstart.js
    

    Python

    # cd python-docs-samples/vision/snippets/quickstart
    python -c 'import quickstart; quickstart.run_quickstart()'
    
    ¡Enhorabuena! Has enviado tu primera solicitud a Vision.

    ¿Cómo ha ido?

    Limpieza

    Para evitar que se apliquen cargos en tu cuenta de Google por los recursos utilizados en esta guía de inicio rápido, sigue estos pasos:

    Siguientes pasos

    Consulte más información sobre nuestras bibliotecas de cliente de la API Vision.