11/*
2- * Copyright 2018 Google Inc.
2+ * Copyright 2018 Google LLC
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2323import com .google .cloud .vision .v1p3beta1 .Image ;
2424import com .google .cloud .vision .v1p3beta1 .ImageAnnotatorClient ;
2525import com .google .cloud .vision .v1p3beta1 .ImageContext ;
26+ import com .google .cloud .vision .v1p3beta1 .ImageSource ;
2627import com .google .cloud .vision .v1p3beta1 .ProductSearchParams ;
2728import com .google .cloud .vision .v1p3beta1 .ProductSearchResults .Result ;
2829import com .google .cloud .vision .v1p3beta1 .ProductSetName ;
@@ -54,7 +55,7 @@ public class ProductSearch {
5455
5556 // [START vision_product_search_get_similar_products]
5657 /**
57- * Search similar products to image.
58+ * Search similar products to image in local file .
5859 *
5960 * @param projectId - Id of the project.
6061 * @param computeRegion - Region name.
@@ -66,7 +67,7 @@ public class ProductSearch {
6667 * color:red AND style:kids color:blue AND style:kids
6768 * @throws IOException - on I/O errors.
6869 */
69- public static void getSimilarProducts (
70+ public static void getSimilarProductsFile (
7071 String projectId ,
7172 String computeRegion ,
7273 String productSetId ,
@@ -85,11 +86,8 @@ public static void getSimilarProducts(
8586
8687 // Create annotate image request along with product search feature.
8788 Feature featuresElement = Feature .newBuilder ().setType (Type .PRODUCT_SEARCH ).build ();
88- // The input image can be a GCS link or HTTPS link or Raw image bytes.
89+ // The input image can be a HTTPS link or Raw image bytes.
8990 // Example:
90- // To use GCS link replace with below code
91- // ImageSource source = ImageSource.newBuilder().setGcsImageUri(gcsUri).build();
92- // Image image = Image.newBuilder().setSource(source).build();
9391 // To use HTTP link replace with below code
9492 // ImageSource source = ImageSource.newBuilder().setImageUri(imageUri).build();
9593 // Image image = Image.newBuilder().setSource(source).build();
@@ -130,6 +128,73 @@ public static void getSimilarProducts(
130128 }
131129 // [END vision_product_search_get_similar_products]
132130
131+ // [START vision_product_search_get_similar_products_gcs]
132+ /**
133+ * Search similar products to image in Google Cloud Storage.
134+ *
135+ * @param projectId - Id of the project.
136+ * @param computeRegion - Region name.
137+ * @param productSetId - Id of the product set.
138+ * @param productCategory - Category of the product.
139+ * @param gcsUri - GCS file path of the image to be searched
140+ * @param filter - Condition to be applied on the labels. Example for filter: (color = red OR
141+ * color = blue) AND style = kids It will search on all products with the following labels:
142+ * color:red AND style:kids color:blue AND style:kids
143+ * @throws Exception - on errors.
144+ */
145+ public static void getSimilarProductsGcs (String projectId ,
146+ String computeRegion ,
147+ String productSetId ,
148+ String productCategory ,
149+ String gcsUri ,
150+ String filter ) throws Exception {
151+ ImageAnnotatorClient queryImageClient = ImageAnnotatorClient .create ();
152+
153+ // Get the full path of the product set.
154+ String productSetPath = ProductSetName .of (projectId , computeRegion , productSetId ).toString ();
155+
156+ // Get the image from Google Cloud Storage
157+ ImageSource source = ImageSource .newBuilder ().setGcsImageUri (gcsUri ).build ();
158+
159+ // Create annotate image request along with product search feature.
160+ Feature featuresElement = Feature .newBuilder ().setType (Type .PRODUCT_SEARCH ).build ();
161+ Image image = Image .newBuilder ().setSource (source ).build ();
162+ ImageContext imageContext =
163+ ImageContext .newBuilder ()
164+ .setProductSearchParams (
165+ ProductSearchParams .newBuilder ()
166+ .setProductSet (productSetPath )
167+ .addProductCategories (productCategory )
168+ .setFilter (filter ))
169+ .build ();
170+
171+ AnnotateImageRequest annotateImageRequest =
172+ AnnotateImageRequest .newBuilder ()
173+ .addFeatures (featuresElement )
174+ .setImage (image )
175+ .setImageContext (imageContext )
176+ .build ();
177+ List <AnnotateImageRequest > requests = Arrays .asList (annotateImageRequest );
178+
179+ // Search products similar to the image.
180+ BatchAnnotateImagesResponse response = queryImageClient .batchAnnotateImages (requests );
181+
182+ List <Result > similarProducts =
183+ response .getResponses (0 ).getProductSearchResults ().getResultsList ();
184+
185+ System .out .println ("Similar Products: " );
186+ for (Result product : similarProducts ) {
187+ System .out .println (String .format ("\n Product name: %s" , product .getProduct ().getName ()));
188+ System .out .println (
189+ String .format ("Product display name: %s" , product .getProduct ().getDisplayName ()));
190+ System .out .println (
191+ String .format ("Product description: %s" , product .getProduct ().getDescription ()));
192+ System .out .println (String .format ("Score(Confidence): %s" , product .getScore ()));
193+ System .out .println (String .format ("Image name: %s" , product .getImage ()));
194+ }
195+ }
196+ // [END vision_product_search_get_similar_products_gcs]
197+
133198 public static void main (String [] args ) throws Exception {
134199 ProductSearch productSearch = new ProductSearch ();
135200 productSearch .argsHelper (args , System .out );
@@ -139,26 +204,40 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception {
139204 ArgumentParser parser = ArgumentParsers .newFor ("Product Search" ).build ();
140205 Subparsers subparsers = parser .addSubparsers ().dest ("command" );
141206
142- Subparser getSimilarProductsParser = subparsers .addParser ("get_similar_products" );
143- getSimilarProductsParser .addArgument ("productSetId" );
144- getSimilarProductsParser .addArgument ("productCategory" );
145- getSimilarProductsParser .addArgument ("filePath" );
146- getSimilarProductsParser .addArgument ("filter" ).nargs ("?" ).setDefault ("" );
207+ Subparser getSimilarProductsFileParser = subparsers .addParser ("get_similar_products_file" );
208+ getSimilarProductsFileParser .addArgument ("productSetId" );
209+ getSimilarProductsFileParser .addArgument ("productCategory" );
210+ getSimilarProductsFileParser .addArgument ("filePath" );
211+ getSimilarProductsFileParser .addArgument ("filter" ).nargs ("?" ).setDefault ("" );
212+
213+ Subparser getSimilarProductsGcsParser = subparsers .addParser ("get_similar_products_gcs" );
214+ getSimilarProductsGcsParser .addArgument ("productSetId" );
215+ getSimilarProductsGcsParser .addArgument ("productCategory" );
216+ getSimilarProductsGcsParser .addArgument ("gcsUri" );
217+ getSimilarProductsGcsParser .addArgument ("filter" ).nargs ("?" ).setDefault ("" );
147218
148219 String projectId = System .getenv ("PROJECT_ID" );
149220 String computeRegion = System .getenv ("REGION_NAME" );
150221
151222 Namespace ns = null ;
152223 try {
153224 ns = parser .parseArgs (args );
154- if (ns .get ("command" ).equals ("get_similar_products " )) {
155- getSimilarProducts (
225+ if (ns .get ("command" ).equals ("get_similar_products_file " )) {
226+ getSimilarProductsFile (
156227 projectId ,
157228 computeRegion ,
158229 ns .getString ("productSetId" ),
159230 ns .getString ("productCategory" ),
160231 ns .getString ("filePath" ),
161232 ns .getString ("filter" ));
233+ } else if (ns .get ("command" ).equals ("get_similar_products_gcs" )) {
234+ getSimilarProductsGcs (
235+ projectId ,
236+ computeRegion ,
237+ ns .getString ("productSetId" ),
238+ ns .getString ("productCategory" ),
239+ ns .getString ("gcsUri" ),
240+ ns .getString ("filter" ));
162241 }
163242
164243 } catch (ArgumentParserException e ) {
0 commit comments