pythongoogle-cloud-platformgoogle-visiongoogle-product-search

Specifying Bounding Poly when performing product search


I'm trying to specify the Bounding Poly whilst conducting a product search using the cloud vision API from GCP. I've done some digging through the code and have built the following function, in particular, the lines attempting to build the poly. However when I run this, I get the error:

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

This is the function:

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

Solution

  • To properly assign list of Vertex() you should explicitly pass a value to the argument vertex=poly_values.

    Code snippet below is based from your code:

        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)}")
    

    Testing:

    enter image description here