Skip to content

Commit 7378792

Browse files
nirupa-kumarnnegrey
authored andcommitted
Added the Product Search tests and the src file updates (#1196)
* Adding the Product Search tests. * Fixing review issues. * Product Search tests and src file updates to include GCS image file. * Fixing issues after review. * Inc. to LLC
1 parent bfd4f2a commit 7378792

11 files changed

+218
-23
lines changed

vision/product-search/cloud-client/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<parent>
2727
<groupId>com.google.cloud.samples</groupId>
2828
<artifactId>shared-configuration</artifactId>
29-
<version>1.0.9</version>
29+
<version>1.0.10</version>
3030
</parent>
3131

3232
<properties>

vision/product-search/cloud-client/src/main/java/com/example/vision/ImportProductSets.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.

vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 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.

vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.

vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java

+93-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
@@ -23,6 +23,7 @@
2323
import com.google.cloud.vision.v1p3beta1.Image;
2424
import com.google.cloud.vision.v1p3beta1.ImageAnnotatorClient;
2525
import com.google.cloud.vision.v1p3beta1.ImageContext;
26+
import com.google.cloud.vision.v1p3beta1.ImageSource;
2627
import com.google.cloud.vision.v1p3beta1.ProductSearchParams;
2728
import com.google.cloud.vision.v1p3beta1.ProductSearchResults.Result;
2829
import 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("\nProduct 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) {

vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.

vision/product-search/cloud-client/src/test/java/com/example/vision/ImportProductSetsIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.

vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.

vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.vision;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.PrintStream;
24+
import java.util.List;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
/** Integration (system) tests for {@link ProductSearch}. */
32+
@RunWith(JUnit4.class)
33+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
34+
public class ProductSearchIT {
35+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
36+
private static final String COMPUTE_REGION = "us-west1";
37+
private static final String GCS_URI =
38+
"gs://java-docs-samples-testing/product-search/indexed_product_sets.csv";
39+
private static final String PRODUCT_SET_ID = "indexed_product_set_id_for_testing";
40+
private static final String PRODUCT_CATEGORY = "apparel";
41+
private static final String PRODUCT_ID_1 = "indexed_product_id_for_testing_1";
42+
private static final String PRODUCT_ID_2 = "indexed_product_id_for_testing_2";
43+
private static final String IMAGE_URI_1 =
44+
"gs://java-docs-samples-testing/product-search/shoes_1.jpg";
45+
private static final String FILE_PATH_1 = "./resources/shoes_1.jpg";
46+
private static final String FILTER = "style=womens";
47+
private static final String BUCKET = "java-docs-samples-testing";
48+
private ByteArrayOutputStream bout;
49+
private PrintStream out;
50+
51+
@Before
52+
public void setUp() throws Exception {
53+
54+
bout = new ByteArrayOutputStream();
55+
out = new PrintStream(bout);
56+
System.setOut(out);
57+
ImportProductSets.importProductSets(PROJECT_ID, COMPUTE_REGION, GCS_URI);
58+
bout.reset();
59+
}
60+
61+
@After
62+
public void tearDown() throws Exception {
63+
ProductManagement.deleteProduct(PROJECT_ID,COMPUTE_REGION,PRODUCT_ID_1);
64+
ProductManagement.deleteProduct(PROJECT_ID,COMPUTE_REGION,PRODUCT_ID_2);
65+
ProductSetManagement.deleteProductSet(PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID);
66+
System.setOut(null);
67+
}
68+
69+
@Test
70+
public void testGetSimilarProductsFile() throws Exception {
71+
// Act
72+
ProductSearch.getSimilarProductsFile(
73+
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, PRODUCT_CATEGORY, FILE_PATH_1, "");
74+
75+
// Assert
76+
String got = bout.toString();
77+
assertThat(got).contains(PRODUCT_ID_1);
78+
assertThat(got).contains(PRODUCT_ID_2);
79+
}
80+
81+
@Test
82+
public void testGetSimilarProductsGcs() throws Exception {
83+
// Act
84+
ProductSearch.getSimilarProductsGcs(
85+
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, PRODUCT_CATEGORY, IMAGE_URI_1, "");
86+
87+
// Assert
88+
String got = bout.toString();
89+
assertThat(got).contains(PRODUCT_ID_1);
90+
assertThat(got).contains(PRODUCT_ID_2);
91+
}
92+
93+
@Test
94+
public void testGetSimilarProductsFileWithFilter() throws Exception {
95+
// Act
96+
ProductSearch.getSimilarProductsFile(
97+
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, PRODUCT_CATEGORY, FILE_PATH_1, FILTER);
98+
99+
// Assert
100+
String got = bout.toString();
101+
assertThat(got).contains(PRODUCT_ID_1);
102+
assertThat(got).doesNotContain(PRODUCT_ID_2);
103+
}
104+
105+
@Test
106+
public void testGetSimilarProductsGcsWithFilter() throws Exception {
107+
// Act
108+
ProductSearch.getSimilarProductsGcs(
109+
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, PRODUCT_CATEGORY, IMAGE_URI_1, FILTER);
110+
111+
// Assert
112+
String got = bout.toString();
113+
assertThat(got).contains(PRODUCT_ID_1);
114+
assertThat(got).doesNotContain(PRODUCT_ID_2);
115+
}
116+
}

vision/product-search/cloud-client/src/test/java/com/example/vision/ProductSetManagementIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.

0 commit comments

Comments
 (0)