执行产品搜索时指定边界多边形

Specifying Bounding Poly when performing product search

我正在尝试指定边界多边形,同时使用 GCP 的云视觉 API 进行产品搜索。我已经对代码进行了一些挖掘并构建了以下函数,特别是试图构建多边形的行。然而,当我 运行 这个时,我得到错误:

TypeError: Invalid constructor input for BoundingPoly: [y: 212
, x: 593
y: 212
, x: 593
y: 798
, y: 798
]

这是函数:

def get_similar_products_uri(image_uri):
    """Search similar products to image.
    Args:
        project_id: Id of the project.
        location: A compute region name.
        product_set_id: Id of the product set.
        product_category: Category of the product.
        image_uri: Cloud Storage location of image to be searched.
        filter: Condition to be applied on the labels.
        Example for filter: (color = red OR color = blue) AND style = kids
        It will search on all products with the following labels:
        color:red AND style:kids
        color:blue AND style:kids
    """
    # product_search_client is needed only for its helper methods.
    product_search_client = vision.ProductSearchClient()
    image_annotator_client = vision.ImageAnnotatorClient()

    # Create annotate image request along with product search feature.
    image_source = vision.ImageSource(image_uri=image_uri)
    image = vision.Image(source=image_source)

    x_vals = [0,593,593,0]
    y_vals = [212,212,798,798]

    x_len = len(x_vals)
    y_len = len(y_vals)
    poly_values = []

    if x_len != y_len:
        print("Please enter equal indices for key and value")
    else:
        for n in range(x_len):
            poly_values.append(vision.Vertex(x=x_vals[n], y=y_vals[n]))
            
    poly = vision.BoundingPoly(poly_values)
    
    # product search specific parameters
    product_set_path = product_search_client.product_set_path(
        project=project_id, location=location,
        product_set="gcp_product_set")

    product_search_params = vision.ProductSearchParams(
        product_set=product_set_path,
        product_categories=["apparel-v2"],
        filter=None,
        bounding_poly=poly)

    image_context = vision.ImageContext(
        product_search_params=product_search_params)
 
    # Search products similar to the image.
    response = image_annotator_client.product_search(
        image, image_context=image_context)

    index_time = response.product_search_results.index_time

    results = response.product_search_results.results

    print(response)
    return

要正确分配 Vertex() 的列表,您应该显式地将值传递给参数 vertex=poly_values

下面的代码片段基于您的代码:

    x_vals = [0,593,593,0]
    y_vals = [212,212,798,798]

    x_len = len(x_vals)
    y_len = len(y_vals)
    poly_values = []

    if x_len != y_len:
        print("Please enter equal indices for key and value")
    else:
        for n in range(x_len):
            poly_values.append(vision.Vertex(x=x_vals[n], y=y_vals[n]))

    poly = vision.BoundingPoly(vertices=poly_values)

    print(poly)
    print(f"Data type of poly: {type(poly)}")

测试: