產品集是產品群組的簡易容器。
建立空白產品集
建議您為所有項目使用單一產品集,並視需要建立其他產品集進行測試。下列程式碼範例說明如何建立空白產品集。
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 參考文件。
使用大量匯入功能建立包含產品的產品集
您也可以使用大量匯入功能,同時建立產品集、多項產品和參考圖像。
產品集、產品和參考圖片的各種參照,都是在您使用的大量匯入 CSV 中設定。詳情請參閱「設定大量匯入 CSV 檔案的格式」。
執行下列程式碼,執行大量匯入作業:
REST
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID: Google Cloud 專案 ID。
- LOCATION_ID:有效的地點 ID。有效的位置 ID 為:
us-west1、us-east1、europe-west1和asia-east1。 - STORAGE_PATH:儲存輸入 CSV 檔案的 Cloud Storage bucket/目錄。提出要求的使用者至少須具備 bucket 的讀取權限。
HTTP 方法和網址:
POST https://vision.googleapis.com/v1/projects/project-id/locations/location-id/productSets:import
JSON 要求內文:
{
"inputConfig": {
"gcsSource": {
"csvFileUri": "storage-path"
}
}
}
如要傳送要求,請選擇以下其中一個選項:
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:import"
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:import" | Select-Object -Expand Content
畫面會顯示類似以下的輸出。您可以使用作業 ID (在本例中為 f10f34e32c40a710) 取得工作狀態。如需範例,請參閱「取得作業狀態」:
{
"name": "projects/project-id/locations/location-id/operations/f10f34e32c40a710"
}長時間執行的作業完成後,您可以取得匯入作業的詳細資料。回應內容應與下方類似:
{
"name": "locations/location-id/operations/f10f34e32c40a710",
"metadata": {
"@type": "type.googleapis.com/google.cloud.vision.v1.BatchOperationMetadata",
"state": "SUCCESSFUL",
"submitTime": "2019-12-06T21:16:04.476466873Z",
"endTime": "2019-12-06T21:16:40.594258084Z"
},
"done": true,
"response": {
"@type": "type.googleapis.com/google.cloud.vision.v1.ImportProductSetsResponse",
"referenceImages": [
{
"name": "projects/project-id/locations/location-id/products/product_id0/referenceImages/image0",
"uri": "gs://my-storage-bucket/img_039.jpg"
},
{
"name": "projects/project-id/locations/location-id/products/product_id1/referenceImages/image1",
"uri": "gs://my-storage-bucket/img_105.jpg"
},
{
"name": "projects/project-id/locations/location-id/products/product_id2/referenceImages/image2",
"uri": "gs://my-storage-bucket/img_224.jpg"
},
{
"name": "projects/project-id/locations/location-id/products/product_id3/referenceImages/image3",
"uri": "gs://my-storage-bucket/img_385.jpg"
}
],
"statuses": [
{},
{},
{},
{}
]
}
}
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 參考文件。
取得作業狀態
您要求的幾項作業需要長時間執行,例如透過大量匯入建立產品集、清除產品集,以及清除孤立產品。這類要求會傳回 JSON,其中包含可用於取得作業狀態的作業 ID。
舉例來說,批次刪除 (purge) 要求會傳回下列 JSON:
{
"name": "projects/project-id/locations/location-id/operations/bc4e1d412863e626"
}在本例中,作業 ID 為 bc4e1d412863e626。以下範例說明如何使用這個 ID 取得這項作業的狀態。
REST
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID: Google Cloud 專案 ID。
- LOCATION_ID:有效的地點 ID。有效的位置 ID 為:
us-west1、us-east1、europe-west1和asia-east1。 - OPERATION_ID:作業 ID。ID 是作業名稱的最後一個元素。舉例來說:
- 作業名稱:
projects/PROJECT_ID/locations/LOCATION_ID/operations/bc4e1d412863e626 - 作業 ID:
bc4e1d412863e626
- 作業名稱:
HTTP 方法和網址:
GET https://vision.googleapis.com/v1/locations/location-id/operations/operation-id
如要傳送要求,請選擇以下其中一個選項:
curl
執行下列指令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-id" \
"https://vision.googleapis.com/v1/locations/location-id/operations/operation-id"
PowerShell
執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-id" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://vision.googleapis.com/v1/locations/location-id/operations/operation-id" | Select-Object -Expand Content
{
"name": "locations/location-id/operations/operation-id",
"metadata": {
"@type": "type.googleapis.com/google.cloud.vision.v1.BatchOperationMetadata",
"state": "SUCCESSFUL",
"submitTime": "2019-09-04T15:58:39.131591882Z",
"endTime": "2019-09-04T15:58:43.099020580Z"
},
"done": true,
"response": {
"@type": "type.googleapis.com/google.cloud.vision.v1.PurgeProductsRequest",
"parent": "projects/project-id/locations/location-id",
"productSetPurgeConfig": {
"productSetId": "project-set-id"
},
"force": true
}
}清除孤立產品作業完成後,您應該會看到類似以下的輸出內容:
{
"name": "locations/location-id/operations/operation-id",
"metadata": {
"@type": "type.googleapis.com/google.cloud.vision.v1.BatchOperationMetadata",
"state": "SUCCESSFUL",
"submitTime": "2019-09-04T16:08:38.278197397Z",
"endTime": "2019-09-04T16:08:45.075778639Z"
},
"done": true,
"response": {
"@type": "type.googleapis.com/google.cloud.vision.v1.PurgeProductsRequest",
"parent": "projects/project-id/locations/location-id",
"deleteOrphanProducts": true,
"force": true
}
}