Rechercher des produits

Une fois que vous avez créé votre ensemble de produits et que celui-ci a été indexé, vous pouvez l'interroger à l'aide de l'API Cloud Vision.

Pour rechercher des produits semblables à une image donnée, transmettez l'URI Google Cloud Storage, l'URL Web ou la chaîne encodée en base64 de l'image à la fonctionnalité de recherche de produits de l'API Cloud Vision. Consultez la page Limites d'utilisation pour connaître la taille maximale des requêtes et obtenir des informations sur les quotas.

Pour obtenir des exemples de détection d'un seul produit et de détection de plusieurs produits dans une image, consultez la section Comprendre les réponses des recherches et la multidétection.

Effectuer une recherche à l'aide d'une image locale

Les exemples suivants lisent un fichier local et interrogent l'API en intégrant dans la requête les octets bruts de l'image (qui est encodée en base64).

API REST et ligne de commande

Avant d'utiliser les données de requête ci-dessous, effectuez les remplacements suivants :

  • base64-encoded-image : représentation en base64 (chaîne ASCII) de vos données d'image binaires. Cette chaîne doit ressembler à la chaîne suivante :
    • /9j/4QAYRXhpZgAA...9tAVx/zDQDlGxn//2Q==
    Consultez la section encodage en base64 pour plus d'informations.
  • project-id : votre ID de projet GCP.
  • location-id : identifiant d'emplacement valide. Les identifiants d'emplacement valides sont : us-west1, us-east1, europe-west1 et asia-east1.
  • product-set-id : ID de l'ensemble de produits sur lequel vous souhaitez exécuter l'opération.

Remarque sur les champs :

  • features.maxResults : nombre maximal de résultats à afficher.
  • imageContext.productCategories : catégorie de produits dans laquelle effectuer la recherche. Actuellement, vous ne pouvez spécifier qu'une seule catégorie de produits : "homegoods" (articles de maison), "apparel" (vêtements), "toys" (jouets), "packaged goods" (biens de consommation courante) et "general" (général).
  • imageContext.filter : expression (ou expressions multiples) de filtrage par clé-valeur pour le libellé de produit. Format : "key=value". Le filtrage des paires clé-valeur peut être lié aux expressions AND ou OR : "color=blue AND style=mens" ou "color=blue OR color=black ". Si vous utilisez l'expression OR, toutes les clés de l'expression doivent être identiques.

Méthode HTTP et URL :

POST https://vision.googleapis.com/v1/images:annotate

Corps JSON de la requête :

{
  "requests": [
    {
      "image": {
        "content": base64-encoded-image
      },
      "features": [
        {
          "type": "PRODUCT_SEARCH",
          "maxResults": 5
        }
      ],
      "imageContext": {
        "productSearchParams": {
          "productSet": "projects/project-id/locations/location-id/productSets/product-set-id",
          "productCategories": [
               "apparel"
          ],
          "filter": "style = womens"
        }
      }
    }
  ]
}

Pour envoyer votre requête, choisissez l'une des options suivantes :

curl

.

Enregistrez le corps de la requête dans un fichier nommé request.json, puis exécutez la commande suivante :

curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
https://vision.googleapis.com/v1/images:annotate

PowerShell

.

Enregistrez le corps de la requête dans un fichier nommé request.json, puis exécutez la commande suivante :

$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://vision.googleapis.com/v1/images:annotate" | Select-Object -Expand Content

Si la requête aboutit, le serveur affiche un code d'état HTTP 200 OK et la réponse au format JSON.

La réponse JSON inclut les deux types de résultats suivants :

  • productSearchResults : contient une liste de produits correspondants pour l'image entière. Dans l'exemple de réponse, les produits correspondants sont : product_id65, product_id35, product_id34, product_id62, product_id32.
  • productGroupedResults : contient les coordonnées du cadre de délimitation et les éléments correspondants pour chaque produit identifié dans l'image. Dans la réponse suivante, un seul produit a été identifié, suivi des produits correspondants dans l'exemple d'ensemble de produits : product_id65, product_id35, product_id34, product_id93, product_id62.

Notez que, même s'il existe un chevauchement dans les deux types de résultats, il peut également y avoir des différences (par exemple, product_id32 et product_id93 dans la réponse).

C#

private static int GetSimilarProductsFile(GetSimilarProductsOptions opts)
{
    // Create annotate image request along with product search feature.
    Image image = Image.FromFile(opts.FilePath);
    var imageAnnotatorClient = ImageAnnotatorClient.Create();

    // Product Search specific parameters
    var productSearchParams = new ProductSearchParams
    {
        ProductSetAsProductSetName = new ProductSetName(opts.ProjectID,
                                                        opts.ComputeRegion,
                                                        opts.ProductSetId),
        ProductCategories = { opts.ProductCategory },
        Filter = opts.Filter
    };

    // Search products similar to the image.
    var results = imageAnnotatorClient.DetectSimilarProducts(image, productSearchParams);

    Console.WriteLine("Similar products:");
    foreach (var result in results.Results)
    {
        var product = result.Product;
        Console.WriteLine($"Score (Confidence): {result.Score}");
        Console.WriteLine($"Image name: {result.Image}");
        Console.WriteLine($"Product name: {product.Name}");
        Console.WriteLine($"Product display name: {product.DisplayName}");
        Console.WriteLine($"Product description: {product.Description}");
        Console.WriteLine($"Product labels: {product.ProductLabels}");
    }
    return 0;
}

Go


import (
	"context"
	"fmt"
	"io"
	"os"

	vision "cloud.google.com/go/vision/apiv1"
	visionpb "google.golang.org/genproto/googleapis/cloud/vision/v1"
)

// getSimilarProducts searches for products from a product set similar to products in an image file.
func getSimilarProducts(w io.Writer, projectID string, location string, productSetID string, productCategory string, file string, filter string) error {
	ctx := context.Background()
	c, err := vision.NewImageAnnotatorClient(ctx)
	if err != nil {
		return fmt.Errorf("NewImageAnnotatorClient: %v", err)
	}
	defer c.Close()

	f, err := os.Open(file)
	if err != nil {
		return fmt.Errorf("Open: %v", err)
	}
	defer f.Close()

	image, err := vision.NewImageFromReader(f)
	if err != nil {
		return fmt.Errorf("NewImageFromReader: %v", err)
	}

	ictx := &visionpb.ImageContext{
		ProductSearchParams: &visionpb.ProductSearchParams{
			ProductSet:        fmt.Sprintf("projects/%s/locations/%s/productSets/%s", projectID, location, productSetID),
			ProductCategories: []string{productCategory},
			Filter:            filter,
		},
	}

	response, err := c.ProductSearch(ctx, image, ictx)
	if err != nil {
		return fmt.Errorf("ProductSearch: %v", err)
	}

	fmt.Fprintf(w, "Product set index time:\n")
	fmt.Fprintf(w, "seconds: %d\n", response.IndexTime.Seconds)
	fmt.Fprintf(w, "nanos: %d\n", response.IndexTime.Nanos)

	fmt.Fprintf(w, "Search results:\n")
	for _, result := range response.Results {
		fmt.Fprintf(w, "Score(Confidence): %f\n", result.Score)
		fmt.Fprintf(w, "Image name: %s\n", result.Image)

		fmt.Fprintf(w, "Prodcut name: %s\n", result.Product.Name)
		fmt.Fprintf(w, "Product display name: %s\n", result.Product.DisplayName)
		fmt.Fprintf(w, "Product labels: %s\n", result.Product.ProductLabels)
	}

	return nil
}

Java

/**
 * Search similar products to image in local file.
 *
 * @param projectId - Id of the project.
 * @param computeRegion - Region name.
 * @param productSetId - Id of the product set.
 * @param productCategory - Category of the product.
 * @param filePath - Local file path of the image to be searched
 * @param 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
 * @throws IOException - on I/O errors.
 */
public static void getSimilarProductsFile(
    String projectId,
    String computeRegion,
    String productSetId,
    String productCategory,
    String filePath,
    String filter)
    throws IOException {
  try (ImageAnnotatorClient queryImageClient = ImageAnnotatorClient.create()) {

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

    // Read the image as a stream of bytes.
    File imgPath = new File(filePath);
    byte[] content = Files.readAllBytes(imgPath.toPath());

    // Create annotate image request along with product search feature.
    Feature featuresElement = Feature.newBuilder().setType(Type.PRODUCT_SEARCH).build();
    // The input image can be a HTTPS link or Raw image bytes.
    // Example:
    // To use HTTP link replace with below code
    //  ImageSource source = ImageSource.newBuilder().setImageUri(imageUri).build();
    //  Image image = Image.newBuilder().setSource(source).build();
    Image image = Image.newBuilder().setContent(ByteString.copyFrom(content)).build();
    ImageContext imageContext =
        ImageContext.newBuilder()
            .setProductSearchParams(
                ProductSearchParams.newBuilder()
                    .setProductSet(productSetPath)
                    .addProductCategories(productCategory)
                    .setFilter(filter))
            .build();

    AnnotateImageRequest annotateImageRequest =
        AnnotateImageRequest.newBuilder()
            .addFeatures(featuresElement)
            .setImage(image)
            .setImageContext(imageContext)
            .build();
    List<AnnotateImageRequest> requests = Arrays.asList(annotateImageRequest);

    // Search products similar to the image.
    BatchAnnotateImagesResponse response = queryImageClient.batchAnnotateImages(requests);

    List<Result> similarProducts =
        response.getResponses(0).getProductSearchResults().getResultsList();
    System.out.println("Similar Products: ");
    for (Result product : similarProducts) {
      System.out.println(String.format("\nProduct name: %s", product.getProduct().getName()));
      System.out.println(
          String.format("Product display name: %s", product.getProduct().getDisplayName()));
      System.out.println(
          String.format("Product description: %s", product.getProduct().getDescription()));
      System.out.println(String.format("Score(Confidence): %s", product.getScore()));
      System.out.println(String.format("Image name: %s", product.getImage()));
    }
  }
}

Node.js

async function getSimilarProductsFile() {
  // Imports the Google Cloud client library
  const vision = require('@google-cloud/vision');
  const fs = require('fs');
  // Creates a client
  const productSearchClient = new vision.ProductSearchClient();
  const imageAnnotatorClient = new vision.ImageAnnotatorClient();

  /**
   * TODO(developer): Uncomment the following line before running the sample.
   */
  // const projectId = 'nodejs-docs-samples';
  // const location = 'us-west1';
  // const productSetId = 'indexed_product_set_id_for_testing';
  // const productCategory = 'apparel';
  // const filePath = './resources/shoes_1.jpg';
  // const filter = '';
  const productSetPath = productSearchClient.productSetPath(
    projectId,
    location,
    productSetId
  );
  const content = fs.readFileSync(filePath, 'base64');
  const request = {
    // The input image can be a GCS link or HTTPS link or Raw image bytes.
    // Example:
    // To use GCS link replace with below code
    // image: {source: {gcsImageUri: filePath}}    // To use HTTP link replace with below code
    // image: {source: {imageUri: filePath}}    image: {content: content},
    features: [{type: 'PRODUCT_SEARCH'}],
    imageContext: {
      productSearchParams: {
        productSet: productSetPath,
        productCategories: [productCategory],
        filter: filter,
      },
    },
  };
  const [response] = await imageAnnotatorClient.batchAnnotateImages({
    requests: [request],
  });
  console.log('Search Image:', filePath);
  const results = response['responses'][0]['productSearchResults']['results'];
  console.log('\nSimilar product information:');
  results.forEach(result => {
    console.log('Product id:', result['product'].name.split('/').pop(-1));
    console.log('Product display name:', result['product'].displayName);
    console.log('Product description:', result['product'].description);
    console.log('Product category:', result['product'].productCategory);
  });
}

PHP

namespace Google\Cloud\Samples\Vision;

use Google\Cloud\Vision\V1\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\ProductSearchClient;
use Google\Cloud\Vision\V1\ProductSearchParams;

/**
 * Search similar products to image
 *
 * @param string $projectId Your Google Cloud project ID
 * @param string $location Google Cloud compute region name
 * @param string $productSetId ID of the product set
 * @param string $productCategory Category of the product
 * @param string $filePath Local file path of the image to be searched
 * @param string $filter Condition to be applied on the labels
 */
function product_search_similar($projectId, $location, $productSetId, $productCategory, $filePath, $filter)
{
    $imageAnnotatorClient = new ImageAnnotatorClient();
    $productSearchClient = new ProductSearchClient();

    # read the image
    $image = file_get_contents($filePath);

    # get the name of the product set
    $productSetPath = $productSearchClient->productSetName($projectId, $location, $productSetId);

    # product search specific parameters
    $productSearchParams = (new ProductSearchParams())
        ->setProductSet($productSetPath)
        ->setProductCategories([$productCategory])
        ->setFilter($filter);

    # search products similar to the image
    $response = $imageAnnotatorClient->productSearch($image, $productSearchParams);

    if ($productSearchResults = $response->getProductSearchResults()) {
        $indexTime = $productSearchResults->getIndexTime();
        printf('Product set index time: %d seconds %d nanos' . PHP_EOL, $indexTime->getSeconds(), $indexTime->getNanos());

        $results = $productSearchResults->getResults();
        print('Search results: ' . PHP_EOL);
        foreach ($results as $result) {
            printf('Score (confidence): %d' . PHP_EOL, $result->getScore());

            # display the product information.
            $product = $result->getProduct();
            $productName = $product->getName();
            $productNameArray = explode('/', $productName);

            printf('Product name: %s' . PHP_EOL, $productName);
            printf('Product id: %s' . PHP_EOL, end($productNameArray));
            printf('Product display name: %s' . PHP_EOL, $product->getDisplayName());
            printf('Product description: %s' . PHP_EOL, $product->getDescription());
            printf('Product category: %s' . PHP_EOL, $product->getProductCategory());
            print('Product labels: ' . PHP_EOL);
            foreach ($product->getProductLabels() as $label) {
                printf('%s : %s' . PHP_EOL, $label->getKey(), $label->getValue());
            }
        }
    } else {
        print($response->getError()->getMessage());
    }

    $imageAnnotatorClient->close();
    $productSearchClient->close();
}

Python

from google.cloud import vision

def get_similar_products_file(
        project_id, location, product_set_id, product_category,
        file_path, filter):
    """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.
        file_path: Local file path of the 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()

    # Read the image as a stream of bytes.
    with open(file_path, 'rb') as image_file:
        content = image_file.read()

    # Create annotate image request along with product search feature.
    image = vision.types.Image(content=content)

    # product search specific parameters
    product_set_path = product_search_client.product_set_path(
        project=project_id, location=location,
        product_set=product_set_id)
    product_search_params = vision.types.ProductSearchParams(
        product_set=product_set_path,
        product_categories=[product_category],
        filter=filter)
    image_context = vision.types.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
    print('Product set index time:')
    print('  seconds: {}'.format(index_time.seconds))
    print('  nanos: {}\n'.format(index_time.nanos))

    results = response.product_search_results.results

    print('Search results:')
    for result in results:
        product = result.product

        print('Score(Confidence): {}'.format(result.score))
        print('Image name: {}'.format(result.image))

        print('Product name: {}'.format(product.name))
        print('Product display name: {}'.format(
            product.display_name))
        print('Product description: {}\n'.format(product.description))
        print('Product labels: {}\n'.format(product.product_labels))

Ruby

require "google/cloud/vision"

def product_search_get_similar_products \
    project_id       = "your-project-id",
    location         = "us-west1",
    product_set_id   = "your-product-set-id",
    product_category = "apparel",
    file_path        = "path/to/product_image.jpg",
    filter           = "(color = red OR color = blue) AND style = kids"

  product_search_client  = Google::Cloud::Vision.product_search
  image_annotator_client = Google::Cloud::Vision.image_annotator

  product_set_path = product_search_client.product_set_path(
    project:     project_id,
    location:    location,
    product_set: product_set_id
  )

  product_set = product_search_client.get_product_set name: product_set_path

  if product_set.index_time.seconds.zero?
    puts "Product set has not been indexed. Please wait and try again."
    return
  end

  product_search_params = {
    product_set:        product_set_path,
    product_categories: [product_category],
    filter:             filter
  }
  image_context = { product_search_params: product_search_params }

  response = image_annotator_client.product_search_detection(
    image:         file_path,
    image_context: image_context
  )

  display_similar_products response.responses.first.product_search_results
end

def display_similar_products search_results
  index_time = search_results.index_time
  puts "Product set index time:"
  puts "\tseconds: #{index_time.seconds}"
  puts "\tnanos: #{index_time.nanos}\n"

  puts "Search results:"
  search_results.results.each_with_index do |result, index|
    puts "Result #{index + 1}:"
    product = result.product

    puts "\tScore(Confidence): #{result.score}"
    puts "\tImage name: #{result.image}"

    puts "\tProduct name: #{product.name}"
    puts "\tProduct display name: #{product.display_name}"
    puts "\tProduct description: #{product.description}"
    puts "\tProduct labels: #{product.product_labels}"
  end
end

Rechercher sur la base d'une image distante

Vous avez également la possibilité de rechercher des produits similaires à une image en spécifiant l'URI Google Cloud Storage de l'image.

API REST et ligne de commande

Avant d'utiliser les données de requête ci-dessous, effectuez les remplacements suivants :

  • cloud-storage-image-uri : chemin d'accès à un fichier image valide dans un bucket Cloud Storage. Il faut au minimum disposer des droits en lecture sur le fichier. Exemple :
    • gs://storage-bucket/filename.jpg
  • project-id : votre ID de projet GCP.
  • location-id : identifiant d'emplacement valide. Les identifiants d'emplacement valides sont : us-west1, us-east1, europe-west1 et asia-east1.
  • product-set-id : ID de l'ensemble de produits sur lequel vous souhaitez exécuter l'opération.

Remarque sur les champs :

  • features.maxResults : nombre maximal de résultats à afficher.
  • imageContext.productCategories : catégorie de produits dans laquelle effectuer la recherche. Actuellement, vous ne pouvez spécifier qu'une seule catégorie de produits : "homegoods" (articles de maison), "apparel" (vêtements), "toys" (jouets), "packaged goods" (biens de consommation courante) et "general" (général).
  • imageContext.filter : expression (ou expressions multiples) de filtrage par clé-valeur pour le libellé de produit. Format : "key=value". Le filtrage des paires clé-valeur peut être lié aux expressions AND ou OR : "color=blue AND style=mens" ou "color=blue OR color=black ". Si vous utilisez l'expression OR, toutes les clés de l'expression doivent être identiques.

Méthode HTTP et URL :

POST https://vision.googleapis.com/v1/images:annotate

Corps JSON de la requête :

{
  "requests": [
    {
      "image": {
        "source": {
          "gcsImageUri": "cloud-storage-image-uri"
        }
      },
      "features": [
        {
          "type": "PRODUCT_SEARCH",
          "maxResults": 5
        }
      ],
      "imageContext": {
        "productSearchParams": {
          "productSet": "projects/project-id/locations/location-id/productSets/product-set-id",
          "productCategories": [
               "apparel"
          ],
          "filter": "style = womens"
        }
      }
    }
  ]
}

Pour envoyer votre requête, choisissez l'une des options suivantes :

curl

.

Enregistrez le corps de la requête dans un fichier nommé request.json, puis exécutez la commande suivante :

curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
https://vision.googleapis.com/v1/images:annotate

PowerShell

.

Enregistrez le corps de la requête dans un fichier nommé request.json, puis exécutez la commande suivante :

$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://vision.googleapis.com/v1/images:annotate" | Select-Object -Expand Content

Si la requête aboutit, le serveur affiche un code d'état HTTP 200 OK et la réponse au format JSON.

La réponse JSON inclut les deux types de résultats suivants :

  • productSearchResults : contient une liste de produits correspondants pour l'image entière. Dans l'exemple de réponse, les produits correspondants sont : product_id65, product_id35, product_id34, product_id62, product_id32.
  • productGroupedResults : contient les coordonnées du cadre de délimitation et les éléments correspondants pour chaque produit identifié dans l'image. Dans la réponse suivante, un seul produit a été identifié, suivi des produits correspondants dans l'exemple d'ensemble de produits : product_id65, product_id35, product_id34, product_id93, product_id62.

Notez que, même s'il existe un chevauchement dans les deux types de résultats, il peut également y avoir des différences (par exemple, product_id32 et product_id93 dans la réponse).

C#

private static int GetSimilarProductsGcs(GetSimilarProductsGcsOptions opts)
{
    // Create annotate image request along with product search feature.
    Image image = Image.FromUri(opts.GcsUri);
    var imageAnnotatorClient = ImageAnnotatorClient.Create();

    // Product Search specific parameters
    var productSearchParams = new ProductSearchParams
    {
        ProductSetAsProductSetName = new ProductSetName(opts.ProjectID,
                                                        opts.ComputeRegion,
                                                        opts.ProductSetId),
        ProductCategories = { opts.ProductCategory },
        Filter = opts.Filter
    };

    // Search products similar to the image.
    var results = imageAnnotatorClient.DetectSimilarProducts(image, productSearchParams);

    Console.WriteLine("Similar products:");
    foreach (var result in results.Results)
    {
        var product = result.Product;
        Console.WriteLine($"Score (Confidence): {result.Score}");
        Console.WriteLine($"Image name: {result.Image}");
        Console.WriteLine($"Product name: {product.Name}");
        Console.WriteLine($"Product display name: {product.DisplayName}");
        Console.WriteLine($"Product description: {product.Description}");
        Console.WriteLine($"Product labels: {product.ProductLabels}");
    }
    return 0;
}

Go


import (
	"context"
	"fmt"
	"io"

	vision "cloud.google.com/go/vision/apiv1"
	visionpb "google.golang.org/genproto/googleapis/cloud/vision/v1"
)

// getSimilarProductsURI searches for products from a product set similar to products in an image file on GCS.
func getSimilarProductsURI(w io.Writer, projectID string, location string, productSetID string, productCategory string, imageURI string, filter string) error {
	ctx := context.Background()
	c, err := vision.NewImageAnnotatorClient(ctx)
	if err != nil {
		return fmt.Errorf("NewImageAnnotatorClient: %v", err)
	}
	defer c.Close()

	image := vision.NewImageFromURI(imageURI)

	ictx := &visionpb.ImageContext{
		ProductSearchParams: &visionpb.ProductSearchParams{
			ProductSet:        fmt.Sprintf("projects/%s/locations/%s/productSets/%s", projectID, location, productSetID),
			ProductCategories: []string{productCategory},
			Filter:            filter,
		},
	}

	response, err := c.ProductSearch(ctx, image, ictx)
	if err != nil {
		return fmt.Errorf("ProductSearch: %v", err)
	}

	fmt.Fprintf(w, "Product set index time:\n")
	fmt.Fprintf(w, "seconds: %d\n", response.IndexTime.Seconds)
	fmt.Fprintf(w, "nanos: %d\n", response.IndexTime.Nanos)

	fmt.Fprintf(w, "Search results:\n")
	for _, result := range response.Results {
		fmt.Fprintf(w, "Score(Confidence): %f\n", result.Score)
		fmt.Fprintf(w, "Image name: %s\n", result.Image)

		fmt.Fprintf(w, "Prodcut name: %s\n", result.Product.Name)
		fmt.Fprintf(w, "Product display name: %s\n", result.Product.DisplayName)
		fmt.Fprintf(w, "Product labels: %s\n", result.Product.ProductLabels)
	}

	return nil
}

Java

/**
 * Search similar products to image in Google Cloud Storage.
 *
 * @param projectId - Id of the project.
 * @param computeRegion - Region name.
 * @param productSetId - Id of the product set.
 * @param productCategory - Category of the product.
 * @param gcsUri - GCS file path of the image to be searched
 * @param 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
 * @throws Exception - on errors.
 */
public static void getSimilarProductsGcs(
    String projectId,
    String computeRegion,
    String productSetId,
    String productCategory,
    String gcsUri,
    String filter)
    throws Exception {
  try (ImageAnnotatorClient queryImageClient = ImageAnnotatorClient.create()) {

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

    // Get the image from Google Cloud Storage
    ImageSource source = ImageSource.newBuilder().setGcsImageUri(gcsUri).build();

    // Create annotate image request along with product search feature.
    Feature featuresElement = Feature.newBuilder().setType(Type.PRODUCT_SEARCH).build();
    Image image = Image.newBuilder().setSource(source).build();
    ImageContext imageContext =
        ImageContext.newBuilder()
            .setProductSearchParams(
                ProductSearchParams.newBuilder()
                    .setProductSet(productSetPath)
                    .addProductCategories(productCategory)
                    .setFilter(filter))
            .build();

    AnnotateImageRequest annotateImageRequest =
        AnnotateImageRequest.newBuilder()
            .addFeatures(featuresElement)
            .setImage(image)
            .setImageContext(imageContext)
            .build();
    List<AnnotateImageRequest> requests = Arrays.asList(annotateImageRequest);

    // Search products similar to the image.
    BatchAnnotateImagesResponse response = queryImageClient.batchAnnotateImages(requests);

    List<Result> similarProducts =
        response.getResponses(0).getProductSearchResults().getResultsList();
    System.out.println("Similar Products: ");
    for (Result product : similarProducts) {
      System.out.println(String.format("\nProduct name: %s", product.getProduct().getName()));
      System.out.println(
          String.format("Product display name: %s", product.getProduct().getDisplayName()));
      System.out.println(
          String.format("Product description: %s", product.getProduct().getDescription()));
      System.out.println(String.format("Score(Confidence): %s", product.getScore()));
      System.out.println(String.format("Image name: %s", product.getImage()));
    }
  }
}

Node.js

async function getSimilarProductsGcs(
  projectId,
  location,
  productSetId,
  productCategory,
  filePath,
  filter
) {
  // Imports the Google Cloud client library
  const vision = require('@google-cloud/vision');
  // Creates a client
  const productSearchClient = new vision.ProductSearchClient();
  const imageAnnotatorClient = new vision.ImageAnnotatorClient();

  /**
   * TODO(developer): Uncomment the following line before running the sample.
   */
  // const projectId = 'Your Google Cloud project Id';
  // const location = 'A compute region name';
  // const productSetId = 'Id of the product set';
  // const productCategory = 'Category of the product';
  // const filePath = 'Local file path of the image to be searched';
  // const filter = 'Condition to be applied on the labels';
  const productSetPath = productSearchClient.productSetPath(
    projectId,
    location,
    productSetId
  );

  const request = {
    // The input image can be a GCS link or HTTPS link or Raw image bytes.
    // Example:
    // To use GCS link replace with below code
    // image: {source: {gcsImageUri: filePath}}    // To use HTTP link replace with below code
    // image: {source: {imageUri: filePath}}    image: {source: {gcsImageUri: filePath}},
    features: [{type: 'PRODUCT_SEARCH'}],
    imageContext: {
      productSearchParams: {
        productSet: productSetPath,
        productCategories: [productCategory],
        filter: filter,
      },
    },
  };
  console.log(request.image);

  const [response] = await imageAnnotatorClient.batchAnnotateImages({
    requests: [request],
  });
  console.log('Search Image:', filePath);
  console.log('\nSimilar product information:');

  const results = response['responses'][0]['productSearchResults']['results'];
  results.forEach(result => {
    console.log('Product id:', result['product'].name.split('/').pop(-1));
    console.log('Product display name:', result['product'].displayName);
    console.log('Product description:', result['product'].description);
    console.log('Product category:', result['product'].productCategory);
  });
}

PHP

namespace Google\Cloud\Samples\Vision;

use Google\Cloud\Vision\V1\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\ProductSearchClient;
use Google\Cloud\Vision\V1\ProductSearchParams;

/**
 * Search similar products to image
 *
 * @param string $projectId Your Google Cloud project ID
 * @param string $location Google Cloud compute region name
 * @param string $productSetId ID of the product set
 * @param string $productCategory Category of the product
 * @param string $gcs Google Cloud Storage path of the image to be searched
 * @param string $filter Condition to be applied on the labels
 */
function product_search_similar_gcs($projectId, $location, $productSetId, $productCategory, $gcsUri, $filter)
{
    $imageAnnotatorClient = new ImageAnnotatorClient();
    $productSearchClient = new ProductSearchClient();

    # get the name of the product set
    $productSetPath = $productSearchClient->productSetName($projectId, $location, $productSetId);

    # product search specific parameters
    $productSearchParams = (new ProductSearchParams())
        ->setProductSet($productSetPath)
        ->setProductCategories([$productCategory])
        ->setFilter($filter);

    # search products similar to the image
    $response = $imageAnnotatorClient->productSearch($gcsUri, $productSearchParams);

    if ($productSearchResults = $response->getProductSearchResults()) {
        $indexTime = $productSearchResults->getIndexTime();
        printf('Product set index time: %d seconds %d nanos' . PHP_EOL, $indexTime->getSeconds(), $indexTime->getNanos());

        $results = $productSearchResults->getResults();
        print('Search results: ' . PHP_EOL);
        foreach ($results as $result) {
            printf('Score (confidence): %d' . PHP_EOL, $result->getScore());

            # display the product information.
            $product = $result->getProduct();
            $productName = $product->getName();
            $productNameArray = explode('/', $productName);

            printf('Product name: %s' . PHP_EOL, $productName);
            printf('Product id: %s' . PHP_EOL, end($productNameArray));
            printf('Product display name: %s' . PHP_EOL, $product->getDisplayName());
            printf('Product description: %s' . PHP_EOL, $product->getDescription());
            printf('Product category: %s' . PHP_EOL, $product->getProductCategory());
            print('Product labels: ' . PHP_EOL);
            foreach ($product->getProductLabels() as $label) {
                printf('%s : %s' . PHP_EOL, $label->getKey(), $label->getValue());
            }
        }
    } else {
        print($response->getError()->getMessage());
    }

    $imageAnnotatorClient->close();
    $productSearchClient->close();
}

Python

from google.cloud import vision

def get_similar_products_uri(
        project_id, location, product_set_id, product_category,
        image_uri, filter):
    """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.
        file_path: Local file path of the 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.types.ImageSource(image_uri=image_uri)
    image = vision.types.Image(source=image_source)

    # product search specific parameters
    product_set_path = product_search_client.product_set_path(
        project=project_id, location=location,
        product_set=product_set_id)
    product_search_params = vision.types.ProductSearchParams(
        product_set=product_set_path,
        product_categories=[product_category],
        filter=filter)
    image_context = vision.types.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
    print('Product set index time:')
    print('  seconds: {}'.format(index_time.seconds))
    print('  nanos: {}\n'.format(index_time.nanos))

    results = response.product_search_results.results

    print('Search results:')
    for result in results:
        product = result.product

        print('Score(Confidence): {}'.format(result.score))
        print('Image name: {}'.format(result.image))

        print('Product name: {}'.format(product.name))
        print('Product display name: {}'.format(
            product.display_name))
        print('Product description: {}\n'.format(product.description))
        print('Product labels: {}\n'.format(product.product_labels))

Ruby

require "google/cloud/vision"

def product_search_get_similar_products_gcs \
    project_id       = "your-project-id",
    location         = "us-west1",
    product_set_id   = "your-product-set-id",
    product_category = "apparel",
    gcs_uri          = "gs://cloud-samples-data/vision/product_search/shoes_1.jpg",
    filter           = "(color = red OR color = blue) AND style = kids"

  product_search_client  = Google::Cloud::Vision.product_search
  image_annotator_client = Google::Cloud::Vision.image_annotator

  product_set_path = product_search_client.product_set_path(
    project:     project_id,
    location:    location,
    product_set: product_set_id
  )

  product_set = product_search_client.get_product_set name: product_set_path

  if product_set.index_time.seconds.zero?
    puts "Product set has not been indexed. Please wait and try again."
    return
  end

  product_search_params = {
    product_set:        product_set_path,
    product_categories: [product_category],
    filter:             filter
  }
  image_context = { product_search_params: product_search_params }

  response = image_annotator_client.product_search_detection(
    image:         gcs_uri,
    image_context: image_context
  )

  display_similar_products_gcs response.responses.first.product_search_results
end

def display_similar_products_gcs search_results
  index_time = search_results.index_time
  puts "Product set index time:"
  puts "\tseconds: #{index_time.seconds}"
  puts "\tnanos: #{index_time.nanos}\n"

  puts "Search results:"
  search_results.results.each_with_index do |result, index|
    puts "Result #{index + 1}:"
    product = result.product

    puts "\tScore(Confidence): #{result.score}"
    puts "\tImage name: #{result.image}"

    puts "\tProduct name: #{product.name}"
    puts "\tProduct display name: #{product.display_name}"
    puts "\tProduct description: #{product.description}"
    puts "\tProduct labels: #{product.product_labels}"
  end
end