本教學課程示範如何建立產品集,其中包含一組產品,以及這些產品的參考圖像。本教學課程說明如何透過線上 (個別) 匯入建立產品集。產品集建立索引後,您可以使用 Vision API Product Search 查詢產品集。
您在本教學課程中將學習以下內容:
- 透過線上 (個別) 匯入建立產品集
- 建立個別產品
- 將產品新增至產品集
- 更新產品
- 建立參考圖像
- 搜尋類似產品
事前準備
開始本教學課程之前,請確認您已安裝適當的用戶端程式庫、為專案啟用計費功能和 API,並正確設定驗證。
匯入程式庫
如要使用 Vision API Product Search,請在下載並安裝用戶端程式庫後,匯入下列模組:
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 參考文件。
執行應用程式
步驟 1:建立產品目錄
使用者可以透過兩種方式建立產品目錄:使用 CSV 檔案批次匯入,透過單一 API 呼叫匯入整個產品目錄;或透過線上匯入,控管產品集,並一次管理一項資源或關係。這主要是指個別建立產品集、產品和參考圖片。您也可以透過線上匯入功能,增量更新已透過批次匯入建立的產品目錄。
在本教學課程中,您將使用線上匯入功能。如需使用 CSV 批次匯入的範例,請參閱快速入門導覽課程。
線上 (個人) 匯入
1. 建立產品集
建立空的產品集,也就是一組產品的簡易容器。
要求
使用 create_product_set() 方法執行下列要求,建立空白的產品集,並命名為「PS_CLOTH-SHOE_070318」。將產品集 ID 和顯示名稱做為引數傳遞。
REST
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID: Google Cloud 專案 ID。
- LOCATION_ID:有效的地點 ID。有效的位置 ID 為:
us-west1、us-east1、europe-west1和asia-east1。 - DISPLAY_NAME:您選擇的字串顯示名稱。
HTTP 方法和網址:
POST https://vision.googleapis.com/v1/projects/project-id/locations/location-id/productSets
JSON 要求內文:
{
"displayName": "display-name"
}
如要傳送要求,請選擇以下其中一個選項:
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/projects/project-id/locations/location-id/productSets"
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/projects/project-id/locations/location-id/productSets" | Select-Object -Expand Content
如果要求成功,伺服器會傳回 200 OK HTTP 狀態碼和 JSON 格式的回應。
畫面會顯示類似以下的輸出。您可以使用產品集 ID (在本例中為 b6d809615b6dd675) 對產品集執行其他作業。
{
"name": "projects/project-id/locations/location-id/productSets/b6d809615b6dd675",
"displayName": "new-product-set"
}
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 參考文件。
回應
Product set name: projects/prj-prod-search-tutorials/locations/us-east1/productSets/PS_CLOTH-SHOE_070318 Product set id: PS_CLOTH-SHOE_070318 Product set display name: CLOTH-SHOE
2. 建立產品
建立產品集後,下一步是建立產品。執行下列要求來建立產品。
REST
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID: Google Cloud 專案 ID。
- LOCATION_ID:有效的地點 ID。有效的位置 ID 為:
us-west1、us-east1、europe-west1和asia-east1。 - DISPLAY_NAME:您選擇的字串顯示名稱。
- PRODUCT_DESCRIPTION:您選擇的字串說明。
- product-category:有效的產品類別。目前可用的產品類別如下:
homegoods-v2、apparel-v2、toys-v2、packagedgoods-v1和general-v1。 productLabels:與產品相關聯的一或多個鍵/值組合。每個 KEY_STRING 都必須有相關聯的 VALUE_STRING。
HTTP 方法和網址:
POST https://vision.googleapis.com/v1/projects/project-id/locations/location-id/products
JSON 要求內文:
{
"displayName": "display-name",
"description": "product-description",
"productCategory": "product-category",
"productLabels": [
{
"key": "key-string",
"value": "value-string"
}
]
}
如要傳送要求,請選擇以下其中一個選項:
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/projects/project-id/locations/location-id/products"
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/projects/project-id/locations/location-id/products" | Select-Object -Expand Content
要求主體範例:
{
"displayName": "sample-product-1234",
"description": "Athletic shorts",
"productCategory": "apparel-v2",
"productLabels": [
{
"key": "style",
"value": "womens"
},
{
"key": "color",
"value": "blue"
}
]
}
如果要求成功,伺服器會傳回 200 OK HTTP 狀態碼和 JSON 格式的回應。
畫面會顯示類似以下的輸出。您可以使用產品 ID (在本例中為 37b9811d308c4e42) 對產品執行其他作業。
{
"name": "projects/project-id/locations/location-id/products/37b9811d308c4e42",
"displayName": "sample-product-456",
"description": "Athletic shorts",
"productCategory": "apparel-v2",
"productLabels": [
{
"key": "style",
"value": "womens"
},
{
"key": "color",
"value": "blue"
}
]
}
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 參考文件。
回應
Product name: projects/prj-prod-search-tutorials/locations/us-east1/products/P_CLOTH-SHOE_46903668_070318
Product id: P_CLOTH-SHOE_46903668_070318
Product display name: Blue Dress
Product category: apparel
Product description: Short sleeved and 1950s style satin dress
Product labels:
Product label 1:
key: style
value: women
Product label 2:
key: category
value: dress
Product label 3:
key: color
value: dark-blue
3. 將產品新增至產品集
建立產品集和產品後,即可將產品新增至產品集。
REST
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID: Google Cloud 專案 ID。
- LOCATION_ID:有效的地點 ID。有效的位置 ID 為:
us-west1、us-east1、europe-west1和asia-east1。 - PRODUCT_SET_ID:要 執行作業的產品集 ID。
- PRODUCT_NAME:產品的完整資源名稱。格式:
projects/PROJECT_ID/locations/LOCATION_ID/products/PRODUCT_ID
HTTP 方法和網址:
POST https://vision.googleapis.com/v1/projects/project-id/locations/location-id/productSets/product-set-id:addProduct
JSON 要求內文:
{
"product": "product-name"
}
如要傳送要求,請選擇以下其中一個選項:
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/projects/project-id/locations/location-id/productSets/product-set-id:addProduct"
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/projects/project-id/locations/location-id/productSets/product-set-id:addProduct" | Select-Object -Expand Content
您應該會收到如下的 JSON 回覆:
{}
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 參考文件。
回應
Product added to product set.
4. 更新產品
如要在建立產品或產品集後進行更新,可以使用我們的更新方法。以下範例顯示標籤變更的產品更新:
指令列
傳送 PATCH 要求時,系統會清除所有先前的欄位及其值,但不可變更的 productCategory 欄位除外。提出 PATCH 更新要求時,請一併傳送所有需要值的欄位。
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID: Google Cloud 專案 ID。
- LOCATION_ID:有效的地點 ID。有效的位置 ID 為:
us-west1、us-east1、europe-west1和asia-east1。 - PRODUCT_ID:與參考圖片相關聯的產品 ID。這個 ID 是由系統隨機設定,或由使用者在建立產品時指定。
- display-name:您選擇的字串顯示名稱。這個值可以與先前的顯示名稱相同,也可以是更新後的值。
- description:您選擇的字串說明。這個值可以與先前的顯示名稱相同,也可以是更新後的值。如果不需要,請省略
description欄位和值。 productLabels:與產品相關聯的一或多個鍵/值組合。每個 KEY_STRING 都必須有相關聯的 VALUE_STRING。
HTTP 方法和網址:
PATCH https://vision.googleapis.com/v1/projects/project-id/locations/location-id/products/product-id
JSON 要求內文:
{
"displayName": "display-name",
"description": "description",
"productLabels": [
{
"key": "key-string",
"value": "value-string"
},
{
"key": "key-string",
"value": "value-string"
}
]
}
如要傳送要求,請選擇以下其中一個選項:
curl
將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:
curl -X PATCH \
-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/projects/project-id/locations/location-id/products/product-id"
PowerShell
將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-id" }
Invoke-WebRequest `
-Method PATCH `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://vision.googleapis.com/v1/projects/project-id/locations/location-id/products/product-id" | Select-Object -Expand Content
您應該會收到如下的 JSON 回覆:
{
"name": "projects/project-id/locations/location-id/products/product-id",
"displayName": "display-name",
"description": "description",
"productCategory": "apparel-v2",
"productLabels": [
{
"key": "style",
"value": "womens"
},
{
"key": "onSale",
"value": "true"
}
]
}
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 參考文件。
回應
Product name: projects/prj-prod-search-tutorials/locations/us-east1/products/P_CLOTH-SHOE_46903668_070318 Product id: P_CLOTH-SHOE_46903668_070318 Product display name: Blue Dress Updated product labels: Product label 1: key: style value: women Product label 2: key: category value: dress Product label 3: key: color value: blue Product description: Short sleeved and 1950s style satin dress
5. 建立產品的參考圖像
為個別產品建立參考圖像後,Vision API Product Search 就能在圖片建立索引後,透過這張圖片搜尋產品。一項產品可以有多張參考圖像,特別是當您希望提高比對品質時。
您隨時可以為產品新增參考圖像。
建立參考圖像時,您可以選擇加入邊界多邊形座標。定界多邊形會識別參考圖片中的感興趣區域。舉例來說,如果您為外套產品建立參考圖像,可以在 bounding poly 引數中提供外套的座標,系統在尋找相符產品時,就只會考慮外套。注意:您可以在建立索引時提供多個定界多邊形,但查詢時,API 僅支援單一定界多邊形。
如要取得圖片的定界多邊形座標,最方便的方法是使用 Vision API 物件定位功能。如要進一步瞭解物件定位,請參閱「偵測多個物件」。
REST
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID: Google Cloud 專案 ID。
- LOCATION_ID:有效的地點 ID。有效的位置 ID 為:
us-west1、us-east1、europe-west1和asia-east1。 - PRODUCT_ID:與參考圖片相關聯的產品 ID。這個 ID 是由系統隨機設定,或由使用者在建立產品時指定。
- CLOUD_STORAGE_IMAGE_URI:Cloud Storage bucket 中有效圖片檔的路徑。您至少必須具備檔案的讀取權限。
範例:
gs://storage-bucket/filename.jpg
HTTP 方法和網址:
POST https://vision.googleapis.com/v1/projects/project-id/locations/location-id/products/product-id/referenceImages
JSON 要求內文:
{
"uri": "cloud-storage-image-uri",
"boundingPolys": [
{
"vertices": [
{
"x": X_MIN,
"y": Y_MIN
},
{
"x": X_MAX,
"y": Y_MIN
},
{
"x": X_MAX,
"y": Y_MAX
},
{
"x": X_MIN,
"y": Y_MAX
}
]
}
]
}
如要傳送要求,請選擇以下其中一個選項:
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/projects/project-id/locations/location-id/products/product-id/referenceImages"
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/projects/project-id/locations/location-id/products/product-id/referenceImages" | Select-Object -Expand Content
如果要求成功,伺服器會傳回 200 OK HTTP 狀態碼和 JSON 格式的回應。
畫面會顯示類似以下的輸出。範例要求在圖片中指定了單一 boundingPoly。定界框的頂點「未」正規化;頂點值是實際像素值,而「非」相對於原始圖片且縮放至 0 到 1 的值。這些頂點的值如下:[(33,22),(282,22),(282,278),(33,278)]。
{
"name": "projects/project-id/locations/location-id/products/product-id/referenceImages/image-id",
"uri": "gs://storage-bucket/filename.jpg",
"boundingPolys": [
{
"vertices": [
{
"x": 33,
"y": 22
},
{
"x": 282,
"y": 22
},
{
"x": 282,
"y": 278
},
{
"x": 33,
"y": 278
}
]
}
]
}
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 參考文件。
回應
Reference image name: projects/prj-prod-search-tutorials/locations/us-east1/products/P_CLOTH-SHOE_46903668_070318/referenceImages/I_469a896b70ba11e8be97d20059124800_070418 Reference image id: I_469a896b70ba11e8be97d20059124800_070418 Reference image uri: gs://product-search-tutorial/dress-shoe-dataset/469a896b70ba11e8be97d20059124800.jpg Reference image bounding polygons: vertices { x: 80 y: 50 } vertices { x: 80 y: 660 } vertices { x: 300 y: 50 } vertices { x: 430 y: 660 }
步驟 2:搜尋相符產品
這個介面可讓您查詢建立的產品目錄,方法是輸入新圖片,並搜尋最相符的產品。
與建立參考圖像類似,搜尋相符圖片時,您可以選擇加入定界多邊形座標。定界多邊形會識別來源圖片中要尋找相符項目的感興趣區域。舉例來說,如果來源圖片同時包含洋裝和皮包,而您只想尋找洋裝的相符項目,可以找出只包含洋裝的圖片區域,並找出該區域的定界多邊形座標。根據預設,如果未指定定界多邊形,API 會判斷最大的定界多邊形,並自動查詢。
如要取得圖片的定界多邊形座標,最方便的方法是使用 Vision API 物件定位功能。如要進一步瞭解物件定位,請參閱「偵測多個物件」。舉例來說,您可以指定整個圖片方塊的定界多邊形,明確查詢完整圖片:[(0, 0), (0, 1), (1, 1), (1, 0)]。
這項要求會傳回 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 參考文件。
服飾回應範例
Search Image: D:/product/final/images-20180618T073733Z-01/images/469355b570ba11e88ff2d20059124800.jpg

Similar product information: Product id: 46930b6b Product display name: Evening gown Product description: Blue evening gown in 1940s style Product category: apparel style: women category: dress color: blue
使用標籤搜尋
以下搜尋範例包含依顏色篩選的條件。
要求
使用 get_similar_products_file() 或 get_similar_products_uri() 方法執行下列要求,即可提出搜尋要求。產品集 ID、本機圖片檔案路徑和篩選器會以引數形式傳遞。這張輸入圖片也位於「resources/input/」中。
Python
python product_search.py get_similar_products_file "12000002" "D:/product/final/images-20180618T073733Z-001/images/469355b570ba11e88ff2d20059124800.jpg" "color=white"
回應
Search Image: D:/product/final/images-20180618T073733Z-001/images/469355b570ba11e88ff2d20059124800.jpg

Similar product information: Product id: p569d4e7a1 Product display name: Wedding Dress Product description: Elegant Wedding Dress for women Product category: apparel style: women category: dress color: white