Skip to content

Commit a66361b

Browse files
nirupa-kumarnnegrey
authored andcommitted
Vision Product Search - GA (GoogleCloudPlatform#1257)
* Vision Product Search - GA * Vision Product Search - GA * Vision Product Search - GA * Vision Product Search - GA * fixing failing tests * fixing failing tests * fixing code review comments * product label printing fix
1 parent dd07e56 commit a66361b

13 files changed

+425
-425
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040
<dependency>
4141
<groupId>com.google.cloud</groupId>
4242
<artifactId>google-cloud-vision</artifactId>
43-
<version>1.37.1</version>
43+
<version>1.52.0</version>
4444
</dependency>
4545
<dependency>
4646
<groupId>com.google.cloud</groupId>
4747
<artifactId>google-cloud-storage</artifactId>
48-
<version>1.37.1</version>
48+
<version>1.52.0</version>
4949
</dependency>
5050
<dependency>
5151
<groupId>net.sourceforge.argparse4j</groupId>

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

+34-35
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818

1919
// [START vision_product_search_tutorial_import]
2020
import com.google.api.gax.longrunning.OperationFuture;
21-
import com.google.cloud.vision.v1p3beta1.BatchOperationMetadata;
22-
import com.google.cloud.vision.v1p3beta1.ImportProductSetsGcsSource;
23-
import com.google.cloud.vision.v1p3beta1.ImportProductSetsGcsSource.Builder;
24-
import com.google.cloud.vision.v1p3beta1.ImportProductSetsInputConfig;
25-
import com.google.cloud.vision.v1p3beta1.ImportProductSetsResponse;
26-
import com.google.cloud.vision.v1p3beta1.LocationName;
27-
import com.google.cloud.vision.v1p3beta1.ProductSearchClient;
28-
import com.google.cloud.vision.v1p3beta1.ReferenceImage;
21+
import com.google.cloud.vision.v1.BatchOperationMetadata;
22+
import com.google.cloud.vision.v1.ImportProductSetsGcsSource;
23+
import com.google.cloud.vision.v1.ImportProductSetsGcsSource.Builder;
24+
import com.google.cloud.vision.v1.ImportProductSetsInputConfig;
25+
import com.google.cloud.vision.v1.ImportProductSetsResponse;
26+
import com.google.cloud.vision.v1.ProductSearchClient;
27+
import com.google.cloud.vision.v1.ReferenceImage;
2928
// [END vision_product_search_tutorial_import]
3029
import java.io.PrintStream;
3130
import javax.swing.JPanel;
@@ -38,13 +37,11 @@
3837
import net.sourceforge.argparse4j.inf.Subparsers;
3938

4039
/**
41-
* This application demonstrates how to Import Product Sets in Cloud Vision
42-
* Product Search.
40+
* This application demonstrates how to Import Product Sets in Cloud Vision Product Search.
4341
*
44-
* For more information, see the tutorial page at
42+
* <p>For more information, see the tutorial page at
4543
* https://cloud.google.com/vision/product-search/docs/
4644
*/
47-
4845
public class ImportProductSets extends JPanel {
4946
// [START vision_product_search_import_product_images]
5047
/**
@@ -57,34 +54,36 @@ public class ImportProductSets extends JPanel {
5754
*/
5855
public static void importProductSets(String projectId, String computeRegion, String gcsUri)
5956
throws Exception {
60-
ProductSearchClient client = ProductSearchClient.create();
57+
try (ProductSearchClient client = ProductSearchClient.create()) {
6158

62-
// A resource that represents Google Cloud Platform location.
63-
LocationName projectLocation = LocationName.of(projectId, computeRegion);
64-
Builder gcsSource = ImportProductSetsGcsSource.newBuilder().setCsvFileUri(gcsUri);
59+
// A resource that represents Google Cloud Platform location.
60+
String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion);
61+
Builder gcsSource = ImportProductSetsGcsSource.newBuilder().setCsvFileUri(gcsUri);
6562

66-
// Set the input configuration along with Google Cloud Storage URI
67-
ImportProductSetsInputConfig inputConfig =
68-
ImportProductSetsInputConfig.newBuilder().setGcsSource(gcsSource).build();
63+
// Set the input configuration along with Google Cloud Storage URI
64+
ImportProductSetsInputConfig inputConfig =
65+
ImportProductSetsInputConfig.newBuilder().setGcsSource(gcsSource).build();
6966

70-
// Import the product sets from the input URI.
71-
OperationFuture<ImportProductSetsResponse, BatchOperationMetadata> response =
72-
client.importProductSetsAsync(projectLocation, inputConfig);
67+
// Import the product sets from the input URI.
68+
OperationFuture<ImportProductSetsResponse, BatchOperationMetadata> response =
69+
client.importProductSetsAsync(formattedParent, inputConfig);
7370

74-
System.out.println(String.format("Processing operation name: %s", response.getName()));
75-
ImportProductSetsResponse results = response.get();
76-
System.out.println("Processing done.");
77-
System.out.println("Results of the processing:");
71+
System.out.println(String.format("Processing operation name: %s", response.getName()));
72+
ImportProductSetsResponse results = response.get();
73+
System.out.println("Processing done.");
74+
System.out.println("Results of the processing:");
7875

79-
for (int i = 0; i < results.getStatusesCount(); i++) {
80-
System.out.println(
81-
String.format("Status of processing line %s of the csv: %s", i, results.getStatuses(i)));
82-
// Check the status of reference image.
83-
if (results.getStatuses(i).getCode() == 0) {
84-
ReferenceImage referenceImage = results.getReferenceImages(i);
85-
System.out.println(referenceImage);
86-
} else {
87-
System.out.println("No reference image.");
76+
for (int i = 0; i < results.getStatusesCount(); i++) {
77+
System.out.println(
78+
String.format(
79+
"Status of processing line %s of the csv: %s", i, results.getStatuses(i)));
80+
// Check the status of reference image.
81+
if (results.getStatuses(i).getCode() == 0) {
82+
ReferenceImage referenceImage = results.getReferenceImages(i);
83+
System.out.println(referenceImage);
84+
} else {
85+
System.out.println("No reference image.");
86+
}
8887
}
8988
}
9089
}

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

+46-44
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@
1616

1717
package com.example.vision;
1818

19-
import com.google.cloud.vision.v1p3beta1.LocationName;
20-
import com.google.cloud.vision.v1p3beta1.Product;
21-
import com.google.cloud.vision.v1p3beta1.ProductName;
22-
import com.google.cloud.vision.v1p3beta1.ProductSearchClient;
23-
import com.google.cloud.vision.v1p3beta1.ProductSet;
24-
import com.google.cloud.vision.v1p3beta1.ProductSetName;
25-
import com.google.protobuf.FieldMask;
19+
import com.google.cloud.vision.v1.Product;
20+
import com.google.cloud.vision.v1.ProductName;
21+
import com.google.cloud.vision.v1.ProductSearchClient;
2622

2723
import java.io.IOException;
2824
import java.io.PrintStream;
@@ -37,10 +33,9 @@
3733
/**
3834
* This application demonstrates how to perform basic operations with Products in a Product Set.
3935
*
40-
* For more information, see the tutorial page at
36+
* <p>For more information, see the tutorial page at
4137
* https://cloud.google.com/vision/product-search/docs/
4238
*/
43-
4439
public class ProductInProductSetManagement {
4540

4641
// [START vision_product_search_add_product_to_product_set]
@@ -56,18 +51,20 @@ public class ProductInProductSetManagement {
5651
public static void addProductToProductSet(
5752
String projectId, String computeRegion, String productId, String productSetId)
5853
throws IOException {
59-
ProductSearchClient client = ProductSearchClient.create();
54+
try (ProductSearchClient client = ProductSearchClient.create()) {
6055

61-
// Get the full path of the product set.
62-
ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId);
56+
// Get the full path of the product set.
57+
String formattedName =
58+
ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId);
6359

64-
// Get the full path of the product.
65-
String productPath = ProductName.of(projectId, computeRegion, productId).toString();
60+
// Get the full path of the product.
61+
String productPath = ProductName.of(projectId, computeRegion, productId).toString();
6662

67-
// Add the product to the product set.
68-
client.addProductToProductSet(productSetPath, productPath);
63+
// Add the product to the product set.
64+
client.addProductToProductSet(formattedName, productPath);
6965

70-
System.out.println(String.format("Product added to product set."));
66+
System.out.println(String.format("Product added to product set."));
67+
}
7168
}
7269
// [END vision_product_search_add_product_to_product_set]
7370

@@ -82,25 +79,27 @@ public static void addProductToProductSet(
8279
*/
8380
public static void listProductsInProductSet(
8481
String projectId, String computeRegion, String productSetId) throws IOException {
85-
ProductSearchClient client = ProductSearchClient.create();
86-
87-
// Get the full path of the product set.
88-
ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId);
89-
90-
// List all the products available in the product set.
91-
for (Product product :
92-
client.listProductsInProductSet(productSetPath.toString()).iterateAll()) {
93-
// Display the product information
94-
System.out.println(String.format("Product name: %s", product.getName()));
95-
System.out.println(
96-
String.format(
97-
"Product id: %s",
98-
product.getName().substring(product.getName().lastIndexOf('/') + 1)));
99-
System.out.println(String.format("Product display name: %s", product.getDisplayName()));
100-
System.out.println(String.format("Product description: %s", product.getDescription()));
101-
System.out.println(String.format("Product category: %s", product.getProductCategory()));
102-
System.out.println(
103-
String.format("Product labels: %s\n", product.getProductLabelsList().toString()));
82+
try (ProductSearchClient client = ProductSearchClient.create()) {
83+
84+
// Get the full path of the product set.
85+
String formattedName =
86+
ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId);
87+
// List all the products available in the product set.
88+
for (Product product : client.listProductsInProductSet(formattedName).iterateAll()) {
89+
// Display the product information
90+
System.out.println(String.format("Product name: %s", product.getName()));
91+
System.out.println(
92+
String.format(
93+
"Product id: %s",
94+
product.getName().substring(product.getName().lastIndexOf('/') + 1)));
95+
System.out.println(String.format("Product display name: %s", product.getDisplayName()));
96+
System.out.println(String.format("Product description: %s", product.getDescription()));
97+
System.out.println(String.format("Product category: %s", product.getProductCategory()));
98+
System.out.println("Product labels: ");
99+
for (Product.KeyValue element : product.getProductLabelsList()) {
100+
System.out.println(String.format("%s: %s", element.getKey(), element.getValue()));
101+
}
102+
}
104103
}
105104
}
106105
// [END vision_product_search_list_products_in_product_set]
@@ -118,18 +117,21 @@ public static void listProductsInProductSet(
118117
public static void removeProductFromProductSet(
119118
String projectId, String computeRegion, String productId, String productSetId)
120119
throws IOException {
121-
ProductSearchClient client = ProductSearchClient.create();
120+
try (ProductSearchClient client = ProductSearchClient.create()) {
122121

123-
// Get the full path of the product set.
124-
ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId);
122+
// Get the full path of the product set.
123+
String formattedParent =
124+
ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId);
125125

126-
// Get the full path of the product.
127-
String productPath = ProductName.of(projectId, computeRegion, productId).toString();
126+
// Get the full path of the product.
127+
String formattedName =
128+
ProductSearchClient.formatProductName(projectId, computeRegion, productId);
128129

129-
// Remove the product from the product set.
130-
client.removeProductFromProductSet(productSetPath, productPath);
130+
// Remove the product from the product set.
131+
client.removeProductFromProductSet(formattedParent, formattedName);
131132

132-
System.out.println(String.format("Product removed from product set."));
133+
System.out.println(String.format("Product removed from product set."));
134+
}
133135
}
134136
// [END vision_product_search_remove_product_from_product_set]
135137

0 commit comments

Comments
 (0)