使用 Cloud Vision API 检测图片中的标签
本快速入门向您展示了如何执行以下操作:
- 创建 Cloud Storage 存储桶。
- 将图片上传到 Cloud Storage,并将文件设为可公开访问。
- 向 Vision API 发出处理该图片的请求。
完成上述步骤大约需要 5 分钟。您可以免费在 Cloud Storage 中存储多达 5 GB 的数据,并且每月可免费向 Vision API 发出多达 1,000 个功能请求。
准备工作
请按照以下说明设置项目并创建 Cloud Storage 存储桶(如果您尚未这样做)。
设置项目
- 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Vision API.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Vision API.
- 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 (Data encryption method. ), and select a
- Select any of the options under Data protection that you
want to set for your bucket.
- Click Create.
保存示例图片文件的本地版本 (
demo-img.jpg
)。在 Google Cloud 控制台中,前往 Cloud Storage 存储桶页面。
选择您在上一部分创建的存储桶。
点击上传文件,然后选择要上传到 Cloud Storage 存储桶的
demo-img.jpg
文件的本地版本。图片来源: Unsplash 用户 Rohiim Ariful。 在文件上传完毕并显示在 Cloud Storage 存储桶中后,公开共享图片。
在试用此方法部分中,通过将
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" } } } ] }
点击执行 (Execute) 以向该服务发送请求。JSON 响应会显示在请求正文之后。
创建 Cloud Storage 存储桶
向 Cloud Vision 发出请求
恭喜!您已向 Cloud Vision 发出了第一个 images.annotate
请求。
清理
为避免产生不必要的 Google Cloud 费用,请使用 Google Cloud 控制台删除不需要的 Cloud Storage 存储桶(和项目)。