1
1
/*
2
- * Copyright 2018 Google Inc.
2
+ * Copyright 2018 Google LLC
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
23
23
import com .google .cloud .vision .v1p3beta1 .Image ;
24
24
import com .google .cloud .vision .v1p3beta1 .ImageAnnotatorClient ;
25
25
import com .google .cloud .vision .v1p3beta1 .ImageContext ;
26
+ import com .google .cloud .vision .v1p3beta1 .ImageSource ;
26
27
import com .google .cloud .vision .v1p3beta1 .ProductSearchParams ;
27
28
import com .google .cloud .vision .v1p3beta1 .ProductSearchResults .Result ;
28
29
import com .google .cloud .vision .v1p3beta1 .ProductSetName ;
@@ -54,7 +55,7 @@ public class ProductSearch {
54
55
55
56
// [START vision_product_search_get_similar_products]
56
57
/**
57
- * Search similar products to image.
58
+ * Search similar products to image in local file .
58
59
*
59
60
* @param projectId - Id of the project.
60
61
* @param computeRegion - Region name.
@@ -66,7 +67,7 @@ public class ProductSearch {
66
67
* color:red AND style:kids color:blue AND style:kids
67
68
* @throws IOException - on I/O errors.
68
69
*/
69
- public static void getSimilarProducts (
70
+ public static void getSimilarProductsFile (
70
71
String projectId ,
71
72
String computeRegion ,
72
73
String productSetId ,
@@ -85,11 +86,8 @@ public static void getSimilarProducts(
85
86
86
87
// Create annotate image request along with product search feature.
87
88
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.
89
90
// 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();
93
91
// To use HTTP link replace with below code
94
92
// ImageSource source = ImageSource.newBuilder().setImageUri(imageUri).build();
95
93
// Image image = Image.newBuilder().setSource(source).build();
@@ -130,6 +128,73 @@ public static void getSimilarProducts(
130
128
}
131
129
// [END vision_product_search_get_similar_products]
132
130
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
+
133
198
public static void main (String [] args ) throws Exception {
134
199
ProductSearch productSearch = new ProductSearch ();
135
200
productSearch .argsHelper (args , System .out );
@@ -139,26 +204,40 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception {
139
204
ArgumentParser parser = ArgumentParsers .newFor ("Product Search" ).build ();
140
205
Subparsers subparsers = parser .addSubparsers ().dest ("command" );
141
206
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 ("" );
147
218
148
219
String projectId = System .getenv ("PROJECT_ID" );
149
220
String computeRegion = System .getenv ("REGION_NAME" );
150
221
151
222
Namespace ns = null ;
152
223
try {
153
224
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 (
156
227
projectId ,
157
228
computeRegion ,
158
229
ns .getString ("productSetId" ),
159
230
ns .getString ("productCategory" ),
160
231
ns .getString ("filePath" ),
161
232
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" ));
162
241
}
163
242
164
243
} catch (ArgumentParserException e ) {
0 commit comments