Vision API 可以检测和提取与图片内多组类别的实体相关的信息。
标签可以用来识别普通物体、地理位置、活动、动物品种和商品等。如果您需要有针对性的自定义标签,则可以使用 Cloud AutoML Vision 训练自定义机器学习模型,以便对图片进行分类。
标签仅以英文形式返回。Cloud Translation API 可以将英文标签翻译成多种其他语言。
例如,上图可能会返回下面的一列标签:
| 说明 | 得分 |
|---|---|
| 街道 | 0.872 |
| 快照 | 0.852 |
| 城镇 | 0.848 |
| 夜间 | 0.804 |
| 小巷 | 0.713 |
标签检测请求
设置您的 Google Cloud 项目和身份验证
如果您尚未创建 Google Cloud 项目,请立即创建。展开本部分可查看相关说明。
- 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 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. -
Install the Google Cloud CLI.
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
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 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. -
Install the Google Cloud CLI.
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init - BASE64_ENCODED_IMAGE:二进制图片数据的 base64 表示(ASCII 字符串)。此字符串应类似于以下字符串:
/9j/4QAYRXhpZgAA...9tAVx/zDQDlGxn//2Q==
- RESULTS_INT:(可选)要返回的结果的整数值。如果您省略
"maxResults"字段及其值,则 API 会默认返回 10 个结果。此字段不适用于以下功能类型:TEXT_DETECTION、DOCUMENT_TEXT_DETECTION或CROP_HINTS。 - PROJECT_ID:您的 Google Cloud 项目 ID。
mid-(如果存在)包含一个由机器生成且与该实体的 Google 知识图谱条目相对应的标识符 (MID)。请注意,mid值在不同语言中各不相同,因此,您可以使用这些值将来自不同语言的实体关联在一起。如需检查 MID 值,请参阅 Google Knowledge Graph API 文档。description- 标签说明。score- 置信度分数,范围为 0(零置信度)到 1(极高置信度)。topicality- ICA(图片内容注释)标签与图片的相关性。它衡量标签对于整个页面上下文的重要程度。
检测本地图片中的标签
您可以使用 Vision API 对本地图片文件执行特征检测。
对于 REST 请求,请将图片文件的内容作为 base64 编码的字符串在请求正文中发送。
对于 gcloud 和客户端库请求,请在请求中指定本地图片的路径。
REST
在使用任何请求数据之前,请先进行以下替换:
HTTP 方法和网址:
POST https://vision.googleapis.com/v1/images:annotate
请求 JSON 正文:
{
"requests": [
{
"image": {
"content": "BASE64_ENCODED_IMAGE"
},
"features": [
{
"maxResults": RESULTS_INT,
"type": "LABEL_DETECTION"
}
]
}
]
}
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json 的文件中,然后执行以下命令:
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" \
-d @request.json \
"https://vision.googleapis.com/v1/images:annotate"
PowerShell
将请求正文保存在名为 request.json 的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_ID" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://vision.googleapis.com/v1/images:annotate" | Select-Object -Expand Content
如果请求成功,服务器将返回一个 200 OK HTTP 状态代码以及 JSON 格式的响应。
LABEL_DETECTION 响应包含已检测到的标签、标签的分数、话题性和不透明的标签 ID,其中:
{
"responses": [
{
"labelAnnotations": [
{
"mid": "/m/01c8br",
"description": "Street",
"score": 0.87294734,
"topicality": 0.87294734
},
{
"mid": "/m/06pg22",
"description": "Snapshot",
"score": 0.8523099,
"topicality": 0.8523099
},
{
"mid": "/m/0dx1j",
"description": "Town",
"score": 0.8481104,
"topicality": 0.8481104
},
{
"mid": "/m/01d74z",
"description": "Night",
"score": 0.80408716,
"topicality": 0.80408716
},
{
"mid": "/m/01lwf0",
"description": "Alley",
"score": 0.7133322,
"topicality": 0.7133322
}
]
}
]
}
Go
试用此示例之前,请按照《Vision 快速入门:使用客户端库》中的 Go 设置说明进行操作。 如需了解详情,请参阅 Vision Go API 参考文档。
如需向 Vision 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。