使用命令行检测图片中的标签
本页面展示如何使用 REST 接口和 curl
命令向 Vision API 发送三个特征检测和注释请求。
通过 Vision API,可将 Google 视觉识别技术轻松集成到开发者应用中。您可以将图片数据和所需的特征类型发送给 Vision API,后者会根据您感兴趣的图片特性返回相应的响应。如需详细了解 Vision API 提供的特征类型,请参阅所有 Vision API 特征列表。
准备工作
- 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.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vision API:
gcloud services enable vision.googleapis.com
-
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
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vision API:
gcloud services enable vision.googleapis.com
-
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
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
发出图片注释请求
完成准备工作步骤后,就可以使用 Vision API 对图片文件进行注释了。
在以下示例中,您将使用下面的图片,通过 curl 向 Vision API 发送请求:
Cloud Storage URI:
gs://cloud-samples-data/vision/using_curl/shanghai.jpeg
HTTPS 网址:
https://console.cloud.google.com/storage/browser/cloud-samples-data/vision/using_curl/shanghai.jpeg
创建请求 JSON
以下 request.json
文件演示了如何请求三个 images:annotate
特征并限制响应中的结果。
创建包含以下文本的 JSON 请求文件,然后将其以 request.json
纯文本文件的格式保存在工作目录下:
request.json
{ "requests": [ { "image": { "source": { "imageUri": "gs://cloud-samples-data/vision/using_curl/shanghai.jpeg" } }, "features": [ { "type": "LABEL_DETECTION", "maxResults": 3 }, { "type": "OBJECT_LOCALIZATION", "maxResults": 1 }, { "type": "TEXT_DETECTION", "maxResults": 1, "model": "builtin/latest" } ] } ] }
发送请求
您可以使用 request.json
中的正文内容和 curl 工具向 Vision API 发送请求。在命令行中输入以下内容:
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "x-goog-user-project: PROJECT_ID" \ -H "Content-Type: application/json; charset=utf-8" \ https://vision.googleapis.com/v1/images:annotate -d @request.json
解读响应
您应该看到和下方类似的 JSON 响应。
该 JSON 格式的请求正文为每种注释类型指定了 maxResults
。
因此,您将在响应 JSON 中看到以下内容:
- 三条
labelAnnotations
返回结果 - 一条
textAnnotations
返回结果(为简洁起见,这里省略了部分响应) - 一条
localizedObjectAnnotations
返回结果
标签检测结果
- 说明:“人物”,得分:0.950
- 说明:“街道”,得分:0.891
- 说明:“交通方式”,得分:0.890
文本检测结果
- 文本:牛牛面馆\n
- 顶点:(x: 159, y: 212)、(x: 947, y: 212)、(x: 947, y: 354)、(x: 159, y: 354)
对象检测结果
- 名称:“人”,得分:0.944
- 归一化顶点:(x: 0.260, y: 0.468)、(x: 0.407, y: 0.468)、(x: 0.407, y: 0.895)、(x: 0.260, y: 0.895)
恭喜!您已向 Vision API 发送了自己的第一个请求!
清理
为避免因本页面中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的 Google Cloud 项目。
Optional: Revoke credentials from the gcloud CLI.
gcloud auth revoke