Apparel Search Tutorial

This tutorial is designed to let a developer quickly start exploring and developing applications with Cloud Vision Product Search.

The Apparel Product Search application allows retailers to set up their entire list of clothing, eyewear, accessories, footwear and headgear products with images and allows retrieving the product code when a user queries with own image.

Overview of Product Search Methods

This tutorial includes the following methods:

  • import_product_sets: Performs required setup including creation of a product set and product, adding products to the product set, and importing images to the products using a CSV file.

  • get_similar_products: Searches for products that match an image.

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: Import the product set

The retailer must initially pass the list of products belonging to apparel department (Product Set) and create an index in Vision Product Search. The import_product_sets interface creates a list of products by creating a product set and product, adding products to the product set, and importing images to the products using a CSV file.

Product Images CSV File

You can provide image data to Vision Product Search using a CSV file. The CSV file contains the list of image locations of all products that to be indexed for your product set. The file must be uploaded to a Google Cloud Storage bucket. The CSV format is described here

The following shows an example of rows from a CSV file:

gs://cloud-ai-vision-data/product-search-tutorial/dress-shoe-dataset/469a896b70ba11e8be97d20059124800.jpg,image-id1,12000002,469a896b,apparel,"product name","style=women,category=dress","0.0,0.0,0.4,0.5",
gs://cloud-ai-vision-data/product-search-tutorial/dress-shoe-dataset/469ac87870ba11e88fe4d20059124800.jpg,image-id2,12000002,469ac878,apparel,"product name","style=women,category=dress,kids=true",,
gs://cloud-ai-vision-data/product-search-tutorial/dress-shoe-dataset/469ae13370ba11e894f4d20059124800.jpg,image-id3,12000002,469ae133,apparel,"product name","style=women,category=shoe","0.0,0.1,0.2,0.3",

The import_product_sets method creates product set with a product set id (department) of “12000002” and adds all the products and reference images. Labels are created for columns such as ‘style’, ‘category’, and ‘kids’.

Product images are imported from Google Cloud Storage bucket location as specified in the ‘ImageUri’ column in the CSV file.

Request

Make a request to import_product_sets to create a product set and products, and then add those products to the product set and import the product reference images. Pass the CSV file path that has the list of image locations of all products as an argument.

Java

Node.js

Python

Code

The import_product_sets() function takes in the following parameters, creates a product set and products, and then adds those products to the product set. The function then adds image references to the products in the product set.

  • projectId: Id of the project.
  • computeRegion: Region name.
  • gcs_uri: Product image file path (Google Cloud Storage). This csv file contains list of image locations of all products that is to be indexed.

Java

Node.js

Python

Response

Processing operation name: locations/us-east1/operations/bf639a4355989c9b Processing done.
Results of the processing:
....
Status of processing line 20 of the csv:
name: "projects/prj-prod-search-tutorials/locations/us-east1/products/p46916b401/referenceImages/46916b4070ba11e89dbed20059124800"
uri: "gs://product-search-tutorial/dress-shoe-dataset/46916b4070ba11e89dbed20059124800.jpg"

Status of processing line 21 of the csv:
name: "projects/prj-prod-search-tutorials/locations/us-east1/products/p4691aea11/referenceImages/4691aea170ba11e8a1b3d20059124800"
uri: "gs://product-search-tutorial/dress-shoe-dataset/4691aea170ba11e8a1b3d20059124800.jpg"
...

The retailer can now pass an image uploaded from a consumer to Vision Product Search to find a product id that where the image closely matches an image from the catalog.

Step 2: Search and Display Products

A user can take a photo of the dress they are interested in and search for the same or similar products in a retailer’s portal. The get_similar_product interface takes as input the consumer photo image, finds the product id that best matches the input image and displays the complete product information.

The interface takes as input the product set id, location of consumer image to be searched, the filtering condition based on the labels.

As the Product Search API returns a list of Product Ids with confidence, this tutorial uses only the product id with the highest confidence to be the closely matching product.

Request

Make a search request by executing the following request with operation type ‘get_similar_product’. Product set id, local image file path and empty filter are passed as arguments. This input image is also present in “resources/input/”

Java

Node.js

Python

Code

The get_similar_product() function takes in the following parameters, searches for the product image that best matches the input image and displays the complete product information. The draw_image() function displays the best matched product image with product information.

  • project_id: Id of the project.
  • compute_region: Region name.
  • product_set_id: Id of Product Set to search
  • search_image: Path of the image to be searched
  • filter: Condition to be applied on the labels.

Java

Node.js

Python

Response

Search Image:
  D:/product/final/images-20180618T073733Z-01/images/469355b570ba11e88ff2d20059124800.jpg

Image search result

Similar product information:
 Product id: 46930b6b
 Product display name: Evening gown
 Product description: Blue evening gown in 1940s style
 Product category: apparel
 style: women
 category: dress
 color: blue

Searching with a Label

The following search example includes a filter based on color.

Request

Make a search request by executing the following request with operation type ‘get_similar_product’. Product set id, local image file path and empty filter are passed as arguments. This input image is also present in “resources/input/”

Java

Node.js

Python

Response

Search Image:
  D:/product/final/images-20180618T073733Z-001/images/469355b570ba11e88ff2d20059124800.jpg

Image search result 2

Similar product information:
 Product id: p569d4e7a1
 Product display name: Wedding Dress
 Product description: Elegant Wedding Dress for women
 Product category: apparel
 style: women
 category: dress
 color: white

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Vision API
Need help? Visit our support page.