建立產品組合並建立索引後,您可以使用 Cloud Vision API 查詢產品組合。
搜尋相符產品
您可以將圖片的 Google Cloud Storage URI、網址或 Base64 編碼字串傳送至 Vision API Product Search,尋找與指定圖片相似的產品。如要瞭解要求大小上限和配額資訊,請參閱「用量限制」。
如需單一產品偵測和多重產品偵測的範例,請參閱「瞭解搜尋回應和多重偵測」主題。
使用本機圖片搜尋
下列範例會讀取本機檔案,並在要求中內嵌原始圖片位元組 (Base64 編碼圖片),藉此查詢 API。
REST
使用任何要求資料之前,請先修改下列項目的值:
- BASE64_ENCODED_IMAGE:二進位圖片資料的 Base64 表示法 (ASCII 字串)。這個字串應與下列字串類似:
/9j/4QAYRXhpZgAA...9tAVx/zDQDlGxn//2Q==
- PROJECT_ID: Google Cloud 專案 ID。
- LOCATION_ID:有效的地點 ID。有效的位置 ID 為:
us-west1、us-east1、europe-west1和asia-east1。 - PRODUCT_SET_ID:要 執行作業的產品集 ID。
欄位專屬注意事項:
features.maxResults:要傳回的結果數量上限。imageContext.productCategories:要搜尋的產品類別。目前只能指定一個產品類別 (居家用品、服飾、玩具、民生消費用品和一般用品)。imageContext.filter- (選用) 產品標籤的鍵/值篩選運算式 (或多個運算式)。格式:「key=value」。篩選鍵/值組合可與 AND 或 OR 運算式連結:「color=blueANDstyle=mens」或「color=blueORcolor=black」。如果使用 OR 運算式,運算式中的所有鍵都必須相同。
HTTP 方法和網址:
POST https://vision.googleapis.com/v1/images:annotate
JSON 要求內文:
{
"requests": [
{
"image": {
"content": base64-encoded-image
},
"features": [
{
"type": "PRODUCT_SEARCH",
"maxResults": 5
}
],
"imageContext": {
"productSearchParams": {
"productSet": "projects/project-id/locations/location-id/productSets/product-set-id",
"productCategories": [
"apparel"
],
"filter": "style = womens"
}
}
}
]
}
如要傳送要求,請選擇以下其中一個選項:
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 格式的回應。
回應 JSON 包含下列兩種結果類型:
productSearchResults:包含整張圖片的相符產品清單。在範例回應中,相符的產品為:product_id65、product_id35、product_id34、product_id62、product_id32。productGroupedResults:包含圖片中識別出的「每項」產品的定界框座標和相符項目。在下列回應中,系統只會識別一項產品,後面接著範例產品集中的相符產品:product_id65、product_id35、product_id34、product_id93、product_id62。
請注意,雖然這兩種結果類型有重疊,但也可能存在差異 (例如回應中的 product_id32 和 product_id93)。
Go
如要瞭解如何安裝及使用 Vision API Product Search 的用戶端程式庫,請參閱「Vision API Product Search 用戶端程式庫」。詳情請參閱 Vision API Product Search Go API 參考文件。
如要向 Vision API Product Search 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Java
如要瞭解如何安裝及使用 Vision API Product Search 的用戶端程式庫,請參閱「Vision API Product Search 用戶端程式庫」。詳情請參閱 Vision API Product Search Java API 參考文件。
如要向 Vision API Product Search 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Node.js
如要瞭解如何安裝及使用 Vision API Product Search 的用戶端程式庫,請參閱「Vision API Product Search 用戶端程式庫」。詳情請參閱 Vision API Product Search Node.js API 參考文件。
如要向 Vision API Product Search 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Python
如要瞭解如何安裝及使用 Vision API Product Search 的用戶端程式庫,請參閱「Vision API Product Search 用戶端程式庫」。詳情請參閱 Vision API Product Search Python API 參考文件。
如要向 Vision API Product Search 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
其他語言
C#:請按照用戶端程式庫頁面上的 C# 設定操作說明完成相關步驟,然後參閱 .NET 適用的 Vision API Product Search 參考文件。
PHP:請按照用戶端程式庫頁面上的 PHP 設定操作說明完成相關步驟,然後參閱 PHP 適用的 Vision API Product Search 參考文件。
Ruby:請按照用戶端程式庫頁面上的 Ruby 設定操作說明完成相關步驟,然後參閱 Ruby 適用的 Vision API Product Search 參考文件。
使用遠端映像檔搜尋
您也可以指定圖片的 Cloud Storage URI,找出與該圖片類似的產品。
REST
使用任何要求資料之前,請先修改下列項目的值:
- CLOUD_STORAGE_IMAGE_URI:Cloud Storage bucket 中有效圖片檔的路徑。您至少必須具備檔案的讀取權限。
範例:
gs://storage-bucket/filename.jpg
- PROJECT_ID: Google Cloud 專案 ID。
- LOCATION_ID:有效的地點 ID。有效的位置 ID 為:
us-west1、us-east1、europe-west1和asia-east1。 - PRODUCT_SET_ID:要 執行作業的產品集 ID。
欄位專屬注意事項:
features.maxResults:要傳回的結果數量上限。imageContext.productCategories:要搜尋的產品類別。目前只能指定一個產品類別 (居家用品、服飾、玩具、民生消費用品和一般用品)。imageContext.filter- (選用) 產品標籤的鍵/值篩選運算式 (或多個運算式)。格式:「key=value」。篩選鍵/值組合可與 AND 或 OR 運算式連結:「color=blueANDstyle=mens」或「color=blueORcolor=black」。如果使用 OR 運算式,運算式中的所有鍵都必須相同。
HTTP 方法和網址:
POST https://vision.googleapis.com/v1/images:annotate
JSON 要求內文:
{
"requests": [
{
"image": {
"source": {
"gcsImageUri": "cloud-storage-image-uri"
}
},
"features": [
{
"type": "PRODUCT_SEARCH",
"maxResults": 5
}
],
"imageContext": {
"productSearchParams": {
"productSet": "projects/project-id/locations/location-id/productSets/product-set-id",
"productCategories": [
"apparel"
],
"filter": "style = womens"
}
}
}
]
}
如要傳送要求,請選擇以下其中一個選項:
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 格式的回應。
回應 JSON 包含下列兩種結果類型:
productSearchResults:包含整張圖片的相符產品清單。在範例回應中,相符的產品為:product_id65、product_id35、product_id34、product_id62、product_id32。productGroupedResults:包含圖片中識別出的「每項」產品的定界框座標和相符項目。在下列回應中,系統只會識別一項產品,後面接著範例產品集中的相符產品:product_id65、product_id35、product_id34、product_id93、product_id62。
請注意,雖然這兩種結果類型有重疊,但也可能存在差異 (例如回應中的 product_id32 和 product_id93)。
Go
如要瞭解如何安裝及使用 Vision API Product Search 的用戶端程式庫,請參閱「Vision API Product Search 用戶端程式庫」。詳情請參閱 Vision API Product Search Go API 參考文件。
如要向 Vision API Product Search 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Java
如要瞭解如何安裝及使用 Vision API Product Search 的用戶端程式庫,請參閱「Vision API Product Search 用戶端程式庫」。詳情請參閱 Vision API Product Search Java API 參考文件。
如要向 Vision API Product Search 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Node.js
如要瞭解如何安裝及使用 Vision API Product Search 的用戶端程式庫,請參閱「Vision API Product Search 用戶端程式庫」。詳情請參閱 Vision API Product Search Node.js API 參考文件。
如要向 Vision API Product Search 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Python
如要瞭解如何安裝及使用 Vision API Product Search 的用戶端程式庫,請參閱「Vision API Product Search 用戶端程式庫」。詳情請參閱 Vision API Product Search Python API 參考文件。
如要向 Vision API Product Search 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
其他語言
C#:請按照用戶端程式庫頁面上的 C# 設定操作說明完成相關步驟,然後參閱 .NET 適用的 Vision API Product Search 參考文件。
PHP:請按照用戶端程式庫頁面上的 PHP 設定操作說明完成相關步驟,然後參閱 PHP 適用的 Vision API Product Search 參考文件。
Ruby:請按照用戶端程式庫頁面上的 Ruby 設定操作說明完成相關步驟,然後參閱 Ruby 適用的 Vision API Product Search 參考文件。