创建图片语料库并执行 AnalyzeCorpus
以便对语料库数据生成嵌入信号后,您可以继续创建索引和索引端点。将索引部署到此索引端点后,您就可以使用条件过滤功能执行语义搜索了。搜索结果包含按相关性得分排序的图片列表。
使用输入文本进行搜索
如需在 corpus
中搜索 assets
,请使用以下格式将要搜索的内容填充到 SearchIndexEndpointRequest
中:
text_query
:用于搜索的文本查询。- 可选的
criteria
或exclusion_criteria
:基于用户提供的注释进行过滤。它可应用于文本、数字、布尔值或日期时间值。
在以下示例中,假设有一个包含自然风景图片的语料库。如需检索与“海滩日落”相关且标记了注释 "state": "California"
的所有素材资源,请发送以下请求(将 FIELD_NAME 替换为 state
,并将 FIELD_VALUE 替换为 California
):
REST 和命令行
在使用任何请求数据之前,请先进行以下替换:
- REGIONALIZED_ENDPOINT:端点可能包含与
LOCATION_ID
匹配的前缀,例如europe-west4-
。详细了解 区域级端点。 - PROJECT_NUMBER:您的 Google Cloud 项目编号。
- LOCATION_ID:您在其中使用 Vertex AI Vision 的区域。例如:
us-central1
、europe-west4
。请参阅可用区域。 - INDEX_ENDPOINT_ID:目标索引端点的 ID。
HTTP 方法和网址:
POST https://warehouse-visionai.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION_ID/indexEndpoints/INDEX_ENDPOINT_ID:searchIndexEndpoint
请求 JSON 正文:
{ "text_query": "sunset at beach", "criteria": [ { "field": "state", "text_array": { "txt_values": [ "California" ] } } ] }
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://warehouse-visionai.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION_ID/indexEndpoints/INDEX_ENDPOINT_ID:searchIndexEndpoint"
PowerShell
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://warehouse-visionai.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION_ID/indexEndpoints/INDEX_ENDPOINT_ID:searchIndexEndpoint" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
{ "searchResultItems": [ { "asset": "projects/PROJECT_NUMBER/locations/LOCATION_ID/corpora/CORPUS_ID/assets/ASSET_ID_1", "relevance": "0.99" }, { "asset": "projects/PROJECT_NUMBER/locations/LOCATION_ID/corpora/CORPUS_ID/assets/ASSET_ID_2", "relevance": "0.98" } ] }
使用输入图片进行搜索
如果您有一张海滩日落的图片,也可以将其用作搜索查询来检索类似图片:
REST 和命令行
在使用任何请求数据之前,请先进行以下替换:
- REGIONALIZED_ENDPOINT:端点可能包含与
LOCATION_ID
匹配的前缀,例如europe-west4-
。详细了解 区域级端点。 - PROJECT_NUMBER:您的 Google Cloud 项目编号。
- LOCATION_ID:您在其中使用 Vertex AI Vision 的区域。例如:
us-central1
、europe-west4
。请参阅可用区域。 - INDEX_ENDPOINT_ID:目标索引端点的 ID。
HTTP 方法和网址:
POST https://warehouse-visionai.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION_ID/indexEndpoints/INDEX_ENDPOINT_ID:searchIndexEndpoint
请求 JSON 正文:
{ "image_query": { "input_image": "IMAGE_BYTES" }, "criteria": [ { "field": "FIELD_NAME", "text_array": { "txt_values": [ "FIELD_VALUE" ] } } ] }
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://warehouse-visionai.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION_ID/indexEndpoints/INDEX_ENDPOINT_ID:searchIndexEndpoint"
PowerShell
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://warehouse-visionai.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION_ID/indexEndpoints/INDEX_ENDPOINT_ID:searchIndexEndpoint" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
{ "searchResultItems": [ { "asset": "projects/PROJECT_NUMBER/locations/LOCATION_ID/corpora/CORPUS_ID/assets/ASSET_ID_1", "relevance": 0.99 }, { "asset": "projects/PROJECT_NUMBER/locations/LOCATION_ID/corpora/CORPUS_ID/assets/ASSET_ID_2", "relevance": 0.98 } ] }
使用现有图片进行搜索
您还可以使用语料库中的现有图片素材资源来检索类似图片。第一个搜索结果将是相同的图片素材资源,相关性得分为 1.0:
REST 和命令行
在使用任何请求数据之前,请先进行以下替换:
- REGIONALIZED_ENDPOINT:端点可能包含与
LOCATION_ID
匹配的前缀,例如europe-west4-
。详细了解 区域级端点。 - PROJECT_NUMBER:您的 Google Cloud 项目编号。
- LOCATION_ID:您在其中使用 Vertex AI Vision 的区域。例如:
us-central1
、europe-west4
。请参阅可用区域。 - INDEX_ENDPOINT_ID:目标索引端点的 ID。
- CORPUS_ID:目标语料库的 ID。
- ASSET_ID:目标素材资源的 ID。
HTTP 方法和网址:
POST https://warehouse-visionai.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION_ID/indexEndpoints/INDEX_ENDPOINT_ID:searchIndexEndpoint
请求 JSON 正文:
{ "image_query": { "asset": "projects/PROJECT_NUMBER/locations/LOCATION_ID/corpora/CORPUS_ID/assets/ASSET_ID" } }
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://warehouse-visionai.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION_ID/indexEndpoints/INDEX_ENDPOINT_ID:searchIndexEndpoint"
PowerShell
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://warehouse-visionai.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION_ID/indexEndpoints/INDEX_ENDPOINT_ID:searchIndexEndpoint" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
{ "searchResultItems": [ { "asset": "projects/PROJECT_NUMBER/locations/LOCATION_ID/corpora/CORPUS_ID/assets/ASSET_ID_1", "relevance": 1.0 }, { "asset": "projects/PROJECT_NUMBER/locations/LOCATION_ID/corpora/CORPUS_ID/assets/ASSET_ID_2", "relevance": 0.99 } ] }