Detectar etiquetas en una imagen con la API Cloud Vision
En esta guía de inicio rápido se explica cómo hacer lo siguiente:
- Crea un segmento de Cloud Storage.
- Sube la imagen a Cloud Storage y haz que el archivo sea accesible públicamente.
- Envía una solicitud a la API Vision con esa imagen.
Estos pasos deberían llevarte unos cinco minutos. Puedes almacenar hasta 5 GB de datos en Cloud Storage sin coste económico y enviar hasta 1000 solicitudes de funciones al mes a la API Vision sin coste económico.
Antes de empezar
Si aún no lo has hecho, sigue estas instrucciones para configurar tu proyecto y crear un segmento de Cloud Storage.
Configurar el proyecto
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Vision API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Vision API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Make sure that you have the following role or roles on the project: Storage Admin (
roles/storage.admin)Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
Ir a Gestión de Identidades y Accesos - Selecciona el proyecto.
- Haz clic en Conceder acceso.
-
En el campo Nuevos principales, introduce tu identificador de usuario. Normalmente, se trata de la dirección de correo de una cuenta de Google.
- En la lista Selecciona un rol, elige un rol.
- Para conceder más roles, haz clic en Añadir otro rol y añade cada rol adicional.
- Haz clic en Guardar.
Crea un segmento de Cloud Storage
- In the Google Cloud console, go to the Cloud Storage Buckets page.
- Click Create.
- On the Create a bucket page, enter your bucket information. To go to the next
step, click Continue.
- 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.
-
In the Choose where to store your data section, do the following:
- Select a Location type.
- Choose a location where your bucket's data is permanently stored from the Location type drop-down menu.
- If you select the dual-region location type, you can also choose to enable turbo replication by using the relevant checkbox.
- To set up cross-bucket replication, select
Add cross-bucket replication via Storage Transfer Service and
follow these steps:
Set up cross-bucket replication
- In the Bucket menu, select a bucket.
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.
-
In the Choose how to store your data section, do the following:
- In the Set a default class section, select the following: Standard.
- To enable hierarchical namespace, in the Optimize storage for data-intensive workloads section, select Enable hierarchical namespace on this bucket.
- 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.
-
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.
- Select any of the options under Data protection that you
want to set for your bucket.
- Click Create.
Hacer una solicitud a Cloud Vision
Guarda una versión local del siguiente archivo de imagen de muestra (
demo-img.jpg).
Atribución de la imagen: Rohiim Ariful en Unsplash. En la Google Cloud consola, ve a la página Segmentos de Cloud Storage.
Selecciona el segmento que has creado en la sección anterior.
Haz clic en Subir archivos y selecciona la versión local del archivo
demo-img.jpgque quieras subir a tu segmento de Cloud Storage.Una vez que se haya subido el archivo y aparezca en el segmento de Cloud Storage, comparte la imagen públicamente.
En la sección Prueba este método, completa la plantilla interactiva del Explorador de APIs sustituyendo
cloud-samples-data/visionen el campoimage.source.imageUripor el nombre del segmento de Cloud Storage en el que has subido el archivodemo-img.jpg. El cuerpo de la solicitud debe tener el siguiente aspecto:{ "requests": [ { "features": [ { "type": "LABEL_DETECTION" } ], "image": { "source": { "imageUri": "gs://cloud-samples-data/vision/demo-img.jpg" } } } ] }Haz clic en Ejecutar para enviar la solicitud al servicio. La respuesta JSON aparece después del cuerpo de la solicitud.
¡Enhorabuena! Has enviado tu primera solicitud
images.annotatea Cloud Vision.Limpieza
Para evitar que se te cobren cargos innecesarios, usa la consola para eliminar tu segmento de Cloud Storage (y tu proyecto) si no los necesitas. Google Cloud Google Cloud
Siguientes pasos
- Consulta una lista de todos los tipos de funciones y sus usos.
- Empieza a usar la API Vision en el idioma que elijas con una biblioteca de cliente de la API Vision.
- Consulta las guías prácticas para obtener más información sobre funciones específicas, ver ejemplos de anotaciones y obtener anotaciones de un archivo o una imagen concretos.
- Consulta información sobre la anotación por lotes de imágenes y archivos (PDF, TIFF o GIF).
- Consulta una lista completa de ejemplos de código de bibliotecas de cliente.
-