在 GCP Vision 中为产品分配多个标签
Assigning multiple labels to product in GCP Vision
我正在使用 GCP Cloud Vision 对产品进行分类。创建产品后,我必须更新它才能为产品添加标签。
如果我通过 CSV 批量导入上传产品,标签可以在 CSV 中格式化为“color=black,style=formal”或“color=black,style=formal,style=mens”。但是,当通过 API 将它们添加到批量导入之外的特定产品时,文档中的示例要求将键和值作为变量。
https://cloud.google.com/vision/product-search/docs/update-resources
如何通过此 API 将多个标签(文档说支持)分配到产品中?
我已尝试将它们添加为以下格式的列表,但出现错误:
GCP 示例: def update_product_labels(product_id, key, value):
尝试:update_product_labels('axel-arigato-1',['category', 'colour'], ['shoes', 'black'])
错误:TypeError: ['category', 'colour'] has type list, but expected one of: bytes, unicode
使用您提供的 link 中的示例代码。可以更新它以创建 KeyValue()
的列表,然后传递它来创建您的 vision.Product()
对象。请参阅下面的更新代码:
from google.cloud import vision
from google.protobuf import field_mask_pb2 as field_mask
def update_product_labels(
project_id, location, product_id, key, value):
"""Update the product labels.
Args:
project_id: Id of the project.
location: A compute region name.
product_id: Id of the product.
key: The key of the label.
value: The value of the label.
"""
client = vision.ProductSearchClient()
# Get the name of the product.
product_path = client.product_path(
project=project_id, location=location, product=product_id)
# Set product name, product label and product display name.
# Multiple labels are also supported.
key_len = len(key)
value_len = len(value)
key_value = []
if key_len != value_len:
print("Please enter equal indices for key and value")
else:
for n in range(key_len):
key_value.append(vision.Product.KeyValue(key=key[n], value=value[n]))
product = vision.Product(
name=product_path,
product_labels=key_value)
# Updating only the product_labels field here.
update_mask = field_mask.FieldMask(paths=['product_labels'])
# This overwrites the product_labels.
updated_product = client.update_product(
product=product, update_mask=update_mask)
# Display the updated product information.
print('Product name: {}'.format(updated_product.name))
print('Updated product labels: {}'.format(product.product_labels))
update_product_labels(
project_id = "your-project-id",
location = "us-east1", #used this location for testing
product_id = "product_id0", #used this product_id for testing
key = ['category', 'colour'],
value = ['shoes', 'black'],
)
样本运行:
检查产品是否真的应用了。我在下面的端点发送了一个 GET:
curl -X GET \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
"https://vision.googleapis.com/v1/projects/your-project-id/locations/us-east1/products/product_id0"
获取产品时的输出:
注意:以这种方式更新键值对会覆盖之前的数据。因此,例如,如果你想添加一个新的键值对,你应该包括旧数据。调用该函数时,您的输入应为 key = ['category', 'colour','new_key'], value = ['shoes', 'black', 'new_value']
.
我正在使用 GCP Cloud Vision 对产品进行分类。创建产品后,我必须更新它才能为产品添加标签。
如果我通过 CSV 批量导入上传产品,标签可以在 CSV 中格式化为“color=black,style=formal”或“color=black,style=formal,style=mens”。但是,当通过 API 将它们添加到批量导入之外的特定产品时,文档中的示例要求将键和值作为变量。
https://cloud.google.com/vision/product-search/docs/update-resources
如何通过此 API 将多个标签(文档说支持)分配到产品中?
我已尝试将它们添加为以下格式的列表,但出现错误:
GCP 示例: def update_product_labels(product_id, key, value):
尝试:update_product_labels('axel-arigato-1',['category', 'colour'], ['shoes', 'black'])
错误:TypeError: ['category', 'colour'] has type list, but expected one of: bytes, unicode
使用您提供的 link 中的示例代码。可以更新它以创建 KeyValue()
的列表,然后传递它来创建您的 vision.Product()
对象。请参阅下面的更新代码:
from google.cloud import vision
from google.protobuf import field_mask_pb2 as field_mask
def update_product_labels(
project_id, location, product_id, key, value):
"""Update the product labels.
Args:
project_id: Id of the project.
location: A compute region name.
product_id: Id of the product.
key: The key of the label.
value: The value of the label.
"""
client = vision.ProductSearchClient()
# Get the name of the product.
product_path = client.product_path(
project=project_id, location=location, product=product_id)
# Set product name, product label and product display name.
# Multiple labels are also supported.
key_len = len(key)
value_len = len(value)
key_value = []
if key_len != value_len:
print("Please enter equal indices for key and value")
else:
for n in range(key_len):
key_value.append(vision.Product.KeyValue(key=key[n], value=value[n]))
product = vision.Product(
name=product_path,
product_labels=key_value)
# Updating only the product_labels field here.
update_mask = field_mask.FieldMask(paths=['product_labels'])
# This overwrites the product_labels.
updated_product = client.update_product(
product=product, update_mask=update_mask)
# Display the updated product information.
print('Product name: {}'.format(updated_product.name))
print('Updated product labels: {}'.format(product.product_labels))
update_product_labels(
project_id = "your-project-id",
location = "us-east1", #used this location for testing
product_id = "product_id0", #used this product_id for testing
key = ['category', 'colour'],
value = ['shoes', 'black'],
)
样本运行:
检查产品是否真的应用了。我在下面的端点发送了一个 GET:
curl -X GET \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
"https://vision.googleapis.com/v1/projects/your-project-id/locations/us-east1/products/product_id0"
获取产品时的输出:
注意:以这种方式更新键值对会覆盖之前的数据。因此,例如,如果你想添加一个新的键值对,你应该包括旧数据。调用该函数时,您的输入应为 key = ['category', 'colour','new_key'], value = ['shoes', 'black', 'new_value']
.