Product Search Tutorial

This tutorial demonstrates how to create a product set which contains a group of products with reference images for those products. Once the product set has been indexed, you can query the product set using Vision Product Search.

In this tutorial, you will learn how to:

  1. Create a product set
  2. Create a product
  3. Add a product to a product set
  4. Update product
  5. Create reference image
  6. Search for similar products

Installation

Java

Node.js

Python

Set the following environment variables where /path/to/service_account.json is the path to your service account key files (see Before you begin), project-id is the ID of your Google Cloud Platform (GCP) project, and region-name is the GCP location that will run your tutorial, for example, "us-east1". Valid location identifiers are: "us-west1", "us-east1", "europe-west1", and "asia-east1".

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
export PROJECT_ID=project-id
export REGION_NAME=region-name

Source files

Java

Node.js

Python

Import Libraries

To use the Cloud Vision Product Search, download and install the client library and import the following modules:

Java

Node.js

You do not need to import libraries for the Node.js version of this tutorial.

Python

Running the Application

Step 1: Create a Product Set

Create an empty Product Set which is a simple container for a group of products.

Request

Create an empty Product Set and name it "PS_CLOTH-SHOE_070318" by executing the following request with operation type "create_product_set". Pass the product set id and display name as arguments.

Java

Node.js

You do not need to import libraries for the Node.js version of this tutorial.

Python

Code

The create_product_set() function takes in the following parameters and returns the newly created product set object.

  • project_id: Id of the project.
  • compute_region: Region name.
  • product_set_id: Id of the product set.
  • product_set_display_name: Display name of the product set.

Java

Node.js

Python

Response

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

Step 2: Create a Product

Once a product set has been created, the next step is to create a product.

Request

Create a product by executing the following request:

Java

Node.js

You do not need to import libraries for the Node.js version of this tutorial.

Python

Code

The create_product() function takes in the following parameters and returns the newly created product object.

  • project_id: Id of the project.
  • compute_region: Region name.
  • product_id: Id of the product.
  • display_name: Display name of the product.
  • product_category: Category of the product.
  • product_description: Description of the product.
  • product_labels: Labels of the product.

Java

Node.js

Python

Response

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

Step 3: Add a Product to a Product Set

Once a product set and a product have been created, you can add the product to the product set.

Request

Add a product to a product set by executing the following request with the operation type add_product_to_product_set. Pass the product id and product set id as arguments.

Java

Node.js

You do not need to import libraries for the Node.js version of this tutorial.

Python

Code

The add_product_to_product_set() function takes in the following parameters and add the product to the product set.

  • project_id: Id of the project.
  • compute_region: Region name.
  • product_id: Id of the product
  • product_set_id: Id of the product set.

Java

Node.js

Python

Response

Product added to product set.

Step 4: Update product

Update the labels of a product.

Request

Update product labels by executing the following request with the operation type update_product_labels. Pass the product id and product labels as arguments.

Java

Node.js

Python

Code

The update_product_labels() function takes in the following parameters and returns the updated product object.

  • project_id: Id of the project.
  • compute_region: Region name.
  • product_id: Id of the product.
  • product_labels: Labels of the product.

Java

Node.js

Python

Response

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

Step 5: Create Reference Image

Create a single reference image for a product.

You can, optionally, include bounding poly coordinates when you create a reference image. A bounding poly identifies an area of interest in the reference image. For example, if you create a reference image for a product that is a jacket, and the image contains both a jacket and a hat, you can identify the bounding poly coordinates for the region of the picture that contains only the jacket.

A convenient way to get the bounding poly coordinates for an image is to use Vision API object localization. For more information on object localization, see Detecting Multiple Objects.

Request

Create a single reference image by executing the following request with the operation type create_image. Pass the product id, image id, Google Cloud Storage URI and bounding poly as arguments.

Java

Node.js

Python

Code

The create_image() function takes in the following parameters and returns the newly created image reference object.

  • project_id: Id of the project.
  • compute_region: Region name.
  • product_id: Id of the product.
  • reference_image_id: Id of the reference image.
  • gcs_uri: Google Cloud Storage path of the input image.
  • bounding_polys: Bounding polygon for image annotation.

Java

Node.js

Python

Response

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
}

Step 6: Search For Matching Products

This interface takes a new image as input and search for the best matching product.

When searching for matching images, you can, optionally, include bounding poly coordinates. A bounding poly identifies an area of interest in the source image that you want to find matches for. For example, if your source image contains both a dress and a purse, and you only want to find matches for the dress, you can identify the bounding poly coordinates for the region of the picture that contains only the dress.

A convenient way to get the bounding poly coordinates for an image is to use Vision API object localization. For more information on object localization, see Detecting Multiple Objects.

Request

Search for matching products from the image catalog by executing the following request with the operation type get_similar_product. Pass the product set id, product category, image file path, empty filter and bounding poly as arguments.

Java

Node.js

Python

Code

The get_similar_product() function takes in the following parameters and returns the best matching product for an image with the score and the matching image. The best image is returned using the highest confidence value.

  • project_id: Id of the project.
  • compute_region: Region name.
  • product_set_id: Id of the product set.
  • product_category: Category of the product.
  • file_path: Local file path of the image to be searched.
  • filter_str: Condition to be applied on the labels.
  • bounding_polys: Bounding polygon for image annotation.

Java

Node.js

Python

Response

Best matching product to the image:
product {
  name: "projects/prj-prod-search-tutorials/locations/us-east1/products/p46930b6b1"
  display_name: "Evening gown"
  description: "Blue evening gown in 1940s style"
  product_category: "apparel"
  product_labels {
    key: "style"
    value: "women"
  }
  product_labels {
    key: "category"
    value: "dress"
  }
  product_labels {
    key: "color"
    value: "blue"
  }
}
score: 0.482027530670166
image: "projects/prj-prod-search-tutorials/locations/us-east1/products/p46930b6b1/referenceImages/46930b6b70ba11e89dfdd20059124800"
Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Vision API
Need help? Visit our support page.