Creating and managing your product resources

After you have created a product set, you can create products and add them to the product set. When you create a product, you must provide a display name for the product and a product category. Currently supported categories are: homegoods or apparel.

You can also supply an optional description of your product, and optional labels for your product. Labels are key/value pairs that describe your product such as color=black or style=mens. You can include labels to filter the results of a product search to only search certain product images.

Creating a product

You can use online import to create an individual product. After a product is created you can then add a reference image to it or add it to a product set.

Command-line

To get the information for a product set, send a request to the following URI.

  • Replace project-id with the id of your Google Cloud Platform (GCP) project.

  • Replace location-id with a valid location identifier. Valid location identifiers are: us-west1, us-east1, europe-west1, and asia-east1.

POST https://vision.googleapis.com/v1p3beta1/projects/project-id/locations/location-id/products
{
  'displayName': 'sample-product-1234',
  'description': 'Athletic shorts',
  'productCategory': 'apparel',
  'productLabels': [
      { 'key': 'style', 'value': 'womens' },
      { 'key': 'color', 'value': 'blue' }
  ]
}

Python

Before you can run this code example, you must install the Python Client Library.

You must also import the client libraries as shown in the following examples.

from google.cloud import vision_v1p3beta1 as vision
from google.cloud.vision_v1p3beta1.types import Product
from google.cloud.vision_v1p3beta1.types import ProductSet
from google.cloud.vision_v1p3beta1.types import ReferenceImage
def create_product(
        project_id, location, product_id, product_display_name,
        product_category):
    """Create one product.
    Args:
        project_id: Id of the project.
        location: A compute region name.
        product_id: Id of the product.
        product_display_name: Display name of the product.
        product_category: Category of the product.
    """
    client = vision.ProductSearchClient()

    # A resource that represents Google Cloud Platform location.
    location_path = client.location_path(project=project_id, location=location)

    # Create a product with the product specification in the region.
    # Set product name and product display name.
    product = vision.types.Product(
        display_name=product_display_name,
        product_category=product_category)

    # The response is the product with the `name` field populated.
    response = client.create_product(
        parent=location_path,
        product=product,
        product_id=product_id)

    # Display the product information.
    print('Product name: {}'.format(response.name))

Java

For more on installing and creating a Vision Product Search client, refer to Vision Product Search Client Libraries.

/**
 * Create one product.
 *
 * @param projectId - Id of the project.
 * @param computeRegion - Region name.
 * @param productId - Id of the product.
 * @param productDisplayName - Display name of the product.
 * @param productCategory - Category of the product.
 * @param productDescription - Description of the product.
 * @param productLabels - Labels of the product.
 * @throws IOException - on I/O errors.
 */
public static void createProduct(
    String projectId,
    String computeRegion,
    String productId,
    String productDisplayName,
    String productCategory,
    String productDescription,
    String productLabels)
    throws IOException {
  ProductSearchClient client = ProductSearchClient.create();

  // A resource that represents Google Cloud Platform location.
  LocationName projectLocation = LocationName.of(projectId, computeRegion);

  // Create a product with the product specification in the region.
  // Multiple labels are also supported.
  Product myProduct =
      Product.newBuilder()
          .setName(productId)
          .setDisplayName(productDisplayName)
          .setProductCategory(productCategory)
          .setDescription(productDescription)
          .addProductLabels(
              KeyValue.newBuilder()
                  .setKey(productLabels.split(",")[0].split("=")[0])
                  .setValue(productLabels.split(",")[0].split("=")[1])
                  .build())
          .build();
  Product product = client.createProduct(projectLocation.toString(), myProduct, productId);

  // Display the product information
  System.out.println(String.format("Product name: %s", product.getName()));
}

Adding a product to a product set

When you have a product and product set available you can then add the product to the product set.

Command-line

To add a product to a product set, send a POST request to the following URI.

  • Replace project-id with the id of your Google Cloud Platform (GCP) project.

  • Replace location-id with a valid location identifier. Valid location identifiers are: us-west1, us-east1, europe-west1, and asia-east1.

  • Replace product-set-id with the id for the product set to associate with the product.

  • Replace product-id with the id for the product to add.

POST https://vision.googleapis.com/v1p3beta1/projects/project-id/locations/location-id/productSets/product-set-id:addProduct
{
  'product': 'product-id'
}

Python

Before you can run this code example, you must install the Python Client Library.

You must also import the client libraries as shown in the following examples.

from google.cloud import vision_v1p3beta1 as vision
from google.cloud.vision_v1p3beta1.types import Product
from google.cloud.vision_v1p3beta1.types import ProductSet
from google.cloud.vision_v1p3beta1.types import ReferenceImage
def add_product_to_product_set(
        project_id, location, product_id, product_set_id):
    """Add a product to a product set.
    Args:
        project_id: Id of the project.
        location: A compute region name.
        product_id: Id of the product.
        product_set_id: Id of the product set.
    """
    client = vision.ProductSearchClient()

    # Get the full path of the product set.
    product_set_path = client.product_set_path(
        project=project_id, location=location,
        product_set=product_set_id)

    # Get the full path of the product.
    product_path = client.product_path(
        project=project_id, location=location, product=product_id)

    # Add the product to the product set.
    client.add_product_to_product_set(
        name=product_set_path, product=product_path)
    print('Product added to product set.')

Java

For more on installing and creating a Vision Product Search client, refer to Vision Product Search Client Libraries.

/**
 * Add a product to a product set.
 *
 * @param projectId - Id of the project.
 * @param computeRegion - Region name.
 * @param productId - Id of the product.
 * @param productSetId - Id of the product set.
 * @throws IOException - on I/O errors.
 */
public static void addProductToSet(
    String projectId, String computeRegion, String productId, String productSetId)
    throws IOException {
  ProductSearchClient client = ProductSearchClient.create();

  // Get the full path of the product set.
  ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId);

  // Get the full path of the product.
  String productPath = ProductName.of(projectId, computeRegion, productId).toString();

  // Add the product to the product set.
  client.addProductToProductSet(productSetPath, productPath);

  System.out.println(String.format("Product added to product set."));
}

Removing a product from a product set

You can also remove an existing product from a product set.

Command-line

To remove a product from a product set, send a POST request to the following URI.

  • Replace project-id with the id of your Google Cloud Platform (GCP) project.

  • Replace location-id with a valid location identifier. Valid location identifiers are: us-west1, us-east1, europe-west1, and asia-east1.

  • Replace product-set-id with the id for the product set that is associated with the product.

  • Replace product-id with the id for the product to remove.

POST https://vision.googleapis.com/v1p3beta1/projects/project-id/locations/location-id/productSets/product-set-id:removeProduct
{
  'product': 'product-id'
}

Python

Before you can run this code example, you must install the Python Client Library.

You must also import the client libraries as shown in the following examples.

from google.cloud import vision_v1p3beta1 as vision
from google.cloud.vision_v1p3beta1.types import Product
from google.cloud.vision_v1p3beta1.types import ProductSet
from google.cloud.vision_v1p3beta1.types import ReferenceImage
def remove_product_from_product_set(
        project_id, location, product_id, product_set_id):
    """Remove a product from a product set.
    Args:
        project_id: Id of the project.
        location: A compute region name.
        product_id: Id of the product.
        product_set_id: Id of the product set.
    """
    client = vision.ProductSearchClient()

    # Get the full path of the product set.
    product_set_path = client.product_set_path(
        project=project_id, location=location,
        product_set=product_set_id)

    # Get the full path of the product.
    product_path = client.product_path(
        project=project_id, location=location, product=product_id)

    # Remove the product from the product set.
    client.remove_product_from_product_set(
        name=product_set_path, product=product_path)
    print('Product removed from product set.')

Java

For more on installing and creating a Vision Product Search client, refer to Vision Product Search Client Libraries.

/**
 * Remove a product from a product set.
 *
 * @param projectId - Id of the project.
 * @param computeRegion - Region name.
 * @param productId - Id of the product.
 * @param productSetId - Id of the product set.
 * @throws IOException - on I/O errors.
 */
public static void removeProductFromProductSet(
    String projectId, String computeRegion, String productId, String productSetId)
    throws IOException {
  ProductSearchClient client = ProductSearchClient.create();

  // Get the full path of the product set.
  ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId);

  // Get the full path of the product.
  String productPath = ProductName.of(projectId, computeRegion, productId).toString();

  // Remove the product from the product set.
  client.removeProductFromProductSet(productSetPath, productPath);

  System.out.println(String.format("Product removed from product set."));
}

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

Send feedback about...

Cloud Vision API