From d913e76384f0cf16a7b1585bc6e5990cbd44bcb7 Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Fri, 9 Nov 2018 14:33:35 -0800 Subject: [PATCH 1/8] Vision Product Search - GA --- vision/product-search/cloud-client/pom.xml | 4 +- .../com/example/vision/ImportProductSets.java | 25 +++++------- .../vision/ProductInProductSetManagement.java | 33 +++++++-------- .../com/example/vision/ProductManagement.java | 37 +++++++++-------- .../com/example/vision/ProductSearch.java | 22 +++++----- .../example/vision/ProductSetManagement.java | 40 ++++++++----------- .../vision/ReferenceImageManagement.java | 38 ++++++++---------- .../com/example/vision/ProductSearchIT.java | 3 -- 8 files changed, 89 insertions(+), 113 deletions(-) diff --git a/vision/product-search/cloud-client/pom.xml b/vision/product-search/cloud-client/pom.xml index b62548089ca..d9e8353748a 100644 --- a/vision/product-search/cloud-client/pom.xml +++ b/vision/product-search/cloud-client/pom.xml @@ -40,12 +40,12 @@ com.google.cloud google-cloud-vision - 1.37.1 + 1.52.0 com.google.cloud google-cloud-storage - 1.37.1 + 1.52.0 net.sourceforge.argparse4j diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ImportProductSets.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ImportProductSets.java index ae8f08b2c9c..b1d5d457591 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ImportProductSets.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ImportProductSets.java @@ -18,14 +18,13 @@ // [START vision_product_search_tutorial_import] import com.google.api.gax.longrunning.OperationFuture; -import com.google.cloud.vision.v1p3beta1.BatchOperationMetadata; -import com.google.cloud.vision.v1p3beta1.ImportProductSetsGcsSource; -import com.google.cloud.vision.v1p3beta1.ImportProductSetsGcsSource.Builder; -import com.google.cloud.vision.v1p3beta1.ImportProductSetsInputConfig; -import com.google.cloud.vision.v1p3beta1.ImportProductSetsResponse; -import com.google.cloud.vision.v1p3beta1.LocationName; -import com.google.cloud.vision.v1p3beta1.ProductSearchClient; -import com.google.cloud.vision.v1p3beta1.ReferenceImage; +import com.google.cloud.vision.v1.BatchOperationMetadata; +import com.google.cloud.vision.v1.ImportProductSetsGcsSource; +import com.google.cloud.vision.v1.ImportProductSetsGcsSource.Builder; +import com.google.cloud.vision.v1.ImportProductSetsInputConfig; +import com.google.cloud.vision.v1.ImportProductSetsResponse; +import com.google.cloud.vision.v1.ProductSearchClient; +import com.google.cloud.vision.v1.ReferenceImage; // [END vision_product_search_tutorial_import] import java.io.PrintStream; import javax.swing.JPanel; @@ -38,13 +37,11 @@ import net.sourceforge.argparse4j.inf.Subparsers; /** - * This application demonstrates how to Import Product Sets in Cloud Vision - * Product Search. + * This application demonstrates how to Import Product Sets in Cloud Vision Product Search. * - * For more information, see the tutorial page at + *

For more information, see the tutorial page at * https://cloud.google.com/vision/product-search/docs/ */ - public class ImportProductSets extends JPanel { // [START vision_product_search_import_product_images] /** @@ -60,7 +57,7 @@ public static void importProductSets(String projectId, String computeRegion, Str ProductSearchClient client = ProductSearchClient.create(); // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); + String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion); Builder gcsSource = ImportProductSetsGcsSource.newBuilder().setCsvFileUri(gcsUri); // Set the input configuration along with Google Cloud Storage URI @@ -69,7 +66,7 @@ public static void importProductSets(String projectId, String computeRegion, Str // Import the product sets from the input URI. OperationFuture response = - client.importProductSetsAsync(projectLocation, inputConfig); + client.importProductSetsAsync(formattedParent, inputConfig); System.out.println(String.format("Processing operation name: %s", response.getName())); ImportProductSetsResponse results = response.get(); diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java index 06f686ec998..272ed1d5ffd 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java @@ -16,13 +16,9 @@ package com.example.vision; -import com.google.cloud.vision.v1p3beta1.LocationName; -import com.google.cloud.vision.v1p3beta1.Product; -import com.google.cloud.vision.v1p3beta1.ProductName; -import com.google.cloud.vision.v1p3beta1.ProductSearchClient; -import com.google.cloud.vision.v1p3beta1.ProductSet; -import com.google.cloud.vision.v1p3beta1.ProductSetName; -import com.google.protobuf.FieldMask; +import com.google.cloud.vision.v1.Product; +import com.google.cloud.vision.v1.ProductName; +import com.google.cloud.vision.v1.ProductSearchClient; import java.io.IOException; import java.io.PrintStream; @@ -37,10 +33,9 @@ /** * This application demonstrates how to perform basic operations with Products in a Product Set. * - * For more information, see the tutorial page at + *

For more information, see the tutorial page at * https://cloud.google.com/vision/product-search/docs/ */ - public class ProductInProductSetManagement { // [START vision_product_search_add_product_to_product_set] @@ -59,13 +54,14 @@ public static void addProductToProductSet( ProductSearchClient client = ProductSearchClient.create(); // Get the full path of the product set. - ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId); + String formattedName = + ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); // Get the full path of the product. String productPath = ProductName.of(projectId, computeRegion, productId).toString(); // Add the product to the product set. - client.addProductToProductSet(productSetPath, productPath); + client.addProductToProductSet(formattedName, productPath); System.out.println(String.format("Product added to product set.")); } @@ -85,11 +81,10 @@ public static void listProductsInProductSet( ProductSearchClient client = ProductSearchClient.create(); // Get the full path of the product set. - ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId); - + String formattedName = + ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); // List all the products available in the product set. - for (Product product : - client.listProductsInProductSet(productSetPath.toString()).iterateAll()) { + for (Product product : client.listProductsInProductSet(formattedName).iterateAll()) { // Display the product information System.out.println(String.format("Product name: %s", product.getName())); System.out.println( @@ -121,13 +116,15 @@ public static void removeProductFromProductSet( ProductSearchClient client = ProductSearchClient.create(); // Get the full path of the product set. - ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId); + String formattedParent = + ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); // Get the full path of the product. - String productPath = ProductName.of(projectId, computeRegion, productId).toString(); + String formattedName = + ProductSearchClient.formatProductName(projectId, computeRegion, productId); // Remove the product from the product set. - client.removeProductFromProductSet(productSetPath, productPath); + client.removeProductFromProductSet(formattedParent, formattedName); System.out.println(String.format("Product removed from product set.")); } diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java index 4f5fc7dbb07..45b1bd38fde 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java @@ -16,11 +16,9 @@ package com.example.vision; -import com.google.cloud.vision.v1p3beta1.LocationName; -import com.google.cloud.vision.v1p3beta1.Product; -import com.google.cloud.vision.v1p3beta1.Product.KeyValue; -import com.google.cloud.vision.v1p3beta1.ProductName; -import com.google.cloud.vision.v1p3beta1.ProductSearchClient; +import com.google.cloud.vision.v1.Product; +import com.google.cloud.vision.v1.Product.KeyValue; +import com.google.cloud.vision.v1.ProductSearchClient; import com.google.protobuf.FieldMask; import java.io.IOException; @@ -62,8 +60,7 @@ public static void createProduct( ProductSearchClient client = ProductSearchClient.create(); // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); - + String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion); // Create a product with the product specification in the region. // Multiple labels are also supported. Product myProduct = @@ -72,7 +69,7 @@ public static void createProduct( .setDisplayName(productDisplayName) .setProductCategory(productCategory) .build(); - Product product = client.createProduct(projectLocation.toString(), myProduct, productId); + Product product = client.createProduct(formattedParent, myProduct, productId); // Display the product information System.out.println(String.format("Product name: %s", product.getName())); @@ -91,16 +88,16 @@ public static void listProducts(String projectId, String computeRegion) throws I ProductSearchClient client = ProductSearchClient.create(); // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); + String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion); // List all the products available in the region. - for (Product product : client.listProducts(projectLocation).iterateAll()) { + for (Product product : client.listProducts(formattedParent).iterateAll()) { // Display the product information System.out.println(String.format("\nProduct name: %s", product.getName())); System.out.println( String.format( - "Product id: %s", - product.getName().substring(product.getName().lastIndexOf('/') + 1))); + "Product id: %s", + product.getName().substring(product.getName().lastIndexOf('/') + 1))); System.out.println(String.format("Product display name: %s", product.getDisplayName())); System.out.println(String.format("Product category: %s", product.getProductCategory())); System.out.println("Product labels:"); @@ -124,10 +121,10 @@ public static void getProduct(String projectId, String computeRegion, String pro ProductSearchClient client = ProductSearchClient.create(); // Get the full path of the product. - ProductName productPath = ProductName.of(projectId, computeRegion, productId); - + String formattedName = + ProductSearchClient.formatProductName(projectId, computeRegion, "[PRODUCT]"); // Get complete detail of the product. - Product product = client.getProduct(productPath.toString()); + Product product = client.getProduct(formattedName); // Display the product information System.out.println(String.format("Product name: %s", product.getName())); @@ -159,13 +156,14 @@ public static void updateProductLabels( ProductSearchClient client = ProductSearchClient.create(); // Get the full path of the product. - String productPath = ProductName.of(projectId, computeRegion, productId).toString(); + String formattedName = + ProductSearchClient.formatProductName(projectId, computeRegion, productId); // Set product name, product labels and product display name. // Multiple labels are also supported. Product product = Product.newBuilder() - .setName(productPath) + .setName(formattedName) .addProductLabels( KeyValue.newBuilder() .setKey(productLabels.split(",")[0].split("=")[0]) @@ -202,10 +200,11 @@ public static void deleteProduct(String projectId, String computeRegion, String ProductSearchClient client = ProductSearchClient.create(); // Get the full path of the product. - ProductName productPath = ProductName.of(projectId, computeRegion, productId); + String formattedName = + ProductSearchClient.formatProductName(projectId, computeRegion, productId); // Delete a product. - client.deleteProduct(productPath); + client.deleteProduct(formattedName); System.out.println("Product deleted."); } diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java index 2e18e806226..58938ccc8b3 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java @@ -16,17 +16,17 @@ package com.example.vision; -import com.google.cloud.vision.v1p3beta1.AnnotateImageRequest; -import com.google.cloud.vision.v1p3beta1.BatchAnnotateImagesResponse; -import com.google.cloud.vision.v1p3beta1.Feature; -import com.google.cloud.vision.v1p3beta1.Feature.Type; -import com.google.cloud.vision.v1p3beta1.Image; -import com.google.cloud.vision.v1p3beta1.ImageAnnotatorClient; -import com.google.cloud.vision.v1p3beta1.ImageContext; -import com.google.cloud.vision.v1p3beta1.ImageSource; -import com.google.cloud.vision.v1p3beta1.ProductSearchParams; -import com.google.cloud.vision.v1p3beta1.ProductSearchResults.Result; -import com.google.cloud.vision.v1p3beta1.ProductSetName; +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Feature.Type; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.ImageContext; +import com.google.cloud.vision.v1.ImageSource; +import com.google.cloud.vision.v1.ProductSearchParams; +import com.google.cloud.vision.v1.ProductSearchResults.Result; +import com.google.cloud.vision.v1.ProductSetName; import com.google.protobuf.ByteString; import java.io.File; diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java index 1b3b9c29560..1a732727f8e 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java @@ -16,13 +16,9 @@ package com.example.vision; -import com.google.cloud.vision.v1p3beta1.CreateProductSetRequest; -import com.google.cloud.vision.v1p3beta1.LocationName; -import com.google.cloud.vision.v1p3beta1.Product; -import com.google.cloud.vision.v1p3beta1.ProductName; -import com.google.cloud.vision.v1p3beta1.ProductSearchClient; -import com.google.cloud.vision.v1p3beta1.ProductSet; -import com.google.cloud.vision.v1p3beta1.ProductSetName; +import com.google.cloud.vision.v1.CreateProductSetRequest; +import com.google.cloud.vision.v1.ProductSearchClient; +import com.google.cloud.vision.v1.ProductSet; import java.io.IOException; import java.io.PrintStream; @@ -37,10 +33,9 @@ /** * This application demonstrates how to perform basic operations on Product Sets. * - * For more information, see the tutorial page at + *

For more information, see the tutorial page at * https://cloud.google.com/vision/product-search/docs/ */ - public class ProductSetManagement { // [START vision_product_search_create_product_set] @@ -59,13 +54,13 @@ public static void createProductSet( ProductSearchClient client = ProductSearchClient.create(); // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); + String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion); // Create a product set with the product set specification in the region. ProductSet myProductSet = ProductSet.newBuilder().setDisplayName(productSetDisplayName).build(); CreateProductSetRequest request = CreateProductSetRequest.newBuilder() - .setParent(projectLocation.toString()) + .setParent(formattedParent) .setProductSet(myProductSet) .setProductSetId(productSetId) .build(); @@ -73,7 +68,6 @@ public static void createProductSet( // Display the product set information System.out.println(String.format("Product set name: %s", productSet.getName())); - } // [END vision_product_search_create_product_set] @@ -88,9 +82,9 @@ public static void createProductSet( public static void listProductSets(String projectId, String computeRegion) throws IOException { ProductSearchClient client = ProductSearchClient.create(); // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); + String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion); // List all the product sets available in the region. - for (ProductSet productSet : client.listProductSets(projectLocation).iterateAll()) { + for (ProductSet productSet : client.listProductSets(formattedParent).iterateAll()) { // Display the product set information System.out.println(String.format("Product set name: %s", productSet.getName())); System.out.println( @@ -103,7 +97,6 @@ public static void listProductSets(String projectId, String computeRegion) throw System.out.println(String.format("\tseconds: %s", productSet.getIndexTime().getSeconds())); System.out.println(String.format("\tnanos: %s", productSet.getIndexTime().getNanos())); } - } // [END vision_product_search_list_product_sets] @@ -121,11 +114,10 @@ public static void getProductSet(String projectId, String computeRegion, String ProductSearchClient client = ProductSearchClient.create(); // Get the full path of the product set. - ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId); - + String formattedName = + ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); // Get complete detail of the product set. - ProductSet productSet = client.getProductSet(productSetPath); - + ProductSet productSet = client.getProductSet(formattedName); // Display the product set information System.out.println(String.format("Product set name: %s", productSet.getName())); System.out.println( @@ -148,15 +140,15 @@ public static void getProductSet(String projectId, String computeRegion, String * @param productSetId - Id of the product set. * @throws IOException - on I/O errors. */ - public static void deleteProductSet( - String projectId, String computeRegion, String productSetId) throws IOException { + public static void deleteProductSet(String projectId, String computeRegion, String productSetId) + throws IOException { ProductSearchClient client = ProductSearchClient.create(); // Get the full path of the product set. - ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId); - + String formattedName = + ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); // Delete the product set. - client.deleteProductSet(productSetPath.toString()); + client.deleteProductSet(formattedName); System.out.println(String.format("Product set deleted")); } diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java index c9413259d5b..cd20f8bd819 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java @@ -14,13 +14,10 @@ * limitations under the License. */ - package com.example.vision; -import com.google.cloud.vision.v1p3beta1.ProductName; -import com.google.cloud.vision.v1p3beta1.ProductSearchClient; -import com.google.cloud.vision.v1p3beta1.ReferenceImage; -import com.google.cloud.vision.v1p3beta1.ReferenceImageName; +import com.google.cloud.vision.v1.ProductSearchClient; +import com.google.cloud.vision.v1.ReferenceImage; import java.io.IOException; import java.io.PrintStream; @@ -36,10 +33,9 @@ * This application demonstrates how to perform basic operations on Reference Images in Cloud Vision * Product Search. * - * For more information, see the tutorial page at + *

For more information, see the tutorial page at * https://cloud.google.com/vision/product-search/docs/ */ - public class ReferenceImageManagement { // [START vision_product_search_create_reference_image] @@ -63,13 +59,13 @@ public static void createReferenceImage( ProductSearchClient client = ProductSearchClient.create(); // Get the full path of the product. - ProductName productPath = ProductName.of(projectId, computeRegion, productId); - + String formattedParent = + ProductSearchClient.formatProductName(projectId, computeRegion, productId); // Create a reference image. ReferenceImage referenceImage = ReferenceImage.newBuilder().setUri(gcsUri).build(); - ReferenceImage image = - client.createReferenceImage(productPath, referenceImage, referenceImageId); + ReferenceImage image = + client.createReferenceImage(formattedParent, referenceImage, referenceImageId); // Display the reference image information. System.out.println(String.format("Reference image name: %s", image.getName())); System.out.println(String.format("Reference image uri: %s", image.getUri())); @@ -90,9 +86,9 @@ public static void listReferenceImagesOfProduct( ProductSearchClient client = ProductSearchClient.create(); // Get the full path of the product. - ProductName productPath = ProductName.of(projectId, computeRegion, productId); - - for (ReferenceImage image : client.listReferenceImages(productPath.toString()).iterateAll()) { + String formattedParent = + ProductSearchClient.formatProductName(projectId, computeRegion, productId); + for (ReferenceImage image : client.listReferenceImages(formattedParent).iterateAll()) { // Display the reference image information. System.out.println(String.format("Reference image name: %s", image.getName())); System.out.println( @@ -123,11 +119,10 @@ public static void getReferenceImage( ProductSearchClient client = ProductSearchClient.create(); // Get the full path of the reference image. - ReferenceImageName referenceImagePath = - ReferenceImageName.of(projectId, computeRegion, productId, referenceImageId); - + String formattedName = + ProductSearchClient.formatImageName(projectId, computeRegion, productId, referenceImageId); // Get complete detail of the reference image. - ReferenceImage image = client.getReferenceImage(referenceImagePath); + ReferenceImage image = client.getReferenceImage(formattedName); // Display the reference image information. System.out.println(String.format("Reference image name: %s", image.getName())); @@ -158,11 +153,10 @@ public static void deleteReferenceImage( ProductSearchClient client = ProductSearchClient.create(); // Get the full path of the reference image. - ReferenceImageName referenceImagePath = - ReferenceImageName.of(projectId, computeRegion, productId, referenceImageId); - + String formattedName = + ProductSearchClient.formatImageName(projectId, computeRegion, productId, referenceImageId); // Delete the reference image. - client.deleteReferenceImage(referenceImagePath.toString()); + client.deleteReferenceImage(formattedName); System.out.println("Reference image deleted from product."); } // [END vision_product_search_delete_reference_image] diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductSearchIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductSearchIT.java index 812ab556c24..367c4965eef 100644 --- a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductSearchIT.java +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductSearchIT.java @@ -19,9 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.PrintStream; -import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -44,7 +42,6 @@ public class ProductSearchIT { "gs://java-docs-samples-testing/product-search/shoes_1.jpg"; private static final String FILE_PATH_1 = "./resources/shoes_1.jpg"; private static final String FILTER = "style=womens"; - private static final String BUCKET = "java-docs-samples-testing"; private ByteArrayOutputStream bout; private PrintStream out; From b19b768cce1948875d3d48dd4d14368572110455 Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Mon, 19 Nov 2018 16:05:53 -0800 Subject: [PATCH 2/8] Vision Product Search - GA --- .../com/example/vision/ProductManagement.java | 18 ++++++++++-------- .../java/com/example/vision/ProductSearch.java | 4 ++-- .../example/vision/ProductSetManagement.java | 6 ++++-- .../vision/ReferenceImageManagement.java | 5 ++++- .../example/vision/ProductManagementIT.java | 2 +- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java index 45b1bd38fde..2ce65178694 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java @@ -70,7 +70,7 @@ public static void createProduct( .setProductCategory(productCategory) .build(); Product product = client.createProduct(formattedParent, myProduct, productId); - + client.close(); // Display the product information System.out.println(String.format("Product name: %s", product.getName())); } @@ -104,6 +104,7 @@ public static void listProducts(String projectId, String computeRegion) throws I System.out.println( String.format("Product labels: %s", product.getProductLabelsList().toString())); } + client.close(); } // [END vision_product_search_list_products] @@ -122,10 +123,10 @@ public static void getProduct(String projectId, String computeRegion, String pro // Get the full path of the product. String formattedName = - ProductSearchClient.formatProductName(projectId, computeRegion, "[PRODUCT]"); + ProductSearchClient.formatProductName(projectId, computeRegion,productId); // Get complete detail of the product. Product product = client.getProduct(formattedName); - + client.close(); // Display the product information System.out.println(String.format("Product name: %s", product.getName())); System.out.println( @@ -134,9 +135,10 @@ public static void getProduct(String projectId, String computeRegion, String pro System.out.println(String.format("Product display name: %s", product.getDisplayName())); System.out.println(String.format("Product description: %s", product.getDescription())); System.out.println(String.format("Product category: %s", product.getProductCategory())); - System.out.println("Product labels:"); - System.out.println( - String.format("Product labels: %s", product.getProductLabelsList().toString())); + System.out.println(String.format("Product labels: ")); + for (Product.KeyValue element : product.getProductLabelsList()) { + System.out.println(String.format("%s: %s", element.getKey(), element.getValue())); + } } // [END vision_product_search_get_product] @@ -176,7 +178,7 @@ public static void updateProductLabels( // Update the product. Product updatedProduct = client.updateProduct(product, updateMask); - + client.close(); // Display the product information System.out.println(String.format("Product name: %s", updatedProduct.getName())); System.out.println(String.format("Updated product labels: ")); @@ -205,7 +207,7 @@ public static void deleteProduct(String projectId, String computeRegion, String // Delete a product. client.deleteProduct(formattedName); - + client.close(); System.out.println("Product deleted."); } // [END vision_product_search_delete_product] diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java index 58938ccc8b3..3d3295e6bb8 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java @@ -114,7 +114,7 @@ public static void getSimilarProductsFile( List similarProducts = response.getResponses(0).getProductSearchResults().getResultsList(); - + queryImageClient.close(); System.out.println("Similar Products: "); for (Result product : similarProducts) { System.out.println(String.format("\nProduct name: %s", product.getProduct().getName())); @@ -181,7 +181,7 @@ public static void getSimilarProductsGcs(String projectId, List similarProducts = response.getResponses(0).getProductSearchResults().getResultsList(); - + queryImageClient.close(); System.out.println("Similar Products: "); for (Result product : similarProducts) { System.out.println(String.format("\nProduct name: %s", product.getProduct().getName())); diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java index 1a732727f8e..8d004a73daf 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java @@ -65,7 +65,7 @@ public static void createProductSet( .setProductSetId(productSetId) .build(); ProductSet productSet = client.createProductSet(request); - + client.close(); // Display the product set information System.out.println(String.format("Product set name: %s", productSet.getName())); } @@ -97,6 +97,7 @@ public static void listProductSets(String projectId, String computeRegion) throw System.out.println(String.format("\tseconds: %s", productSet.getIndexTime().getSeconds())); System.out.println(String.format("\tnanos: %s", productSet.getIndexTime().getNanos())); } + client.close(); } // [END vision_product_search_list_product_sets] @@ -118,6 +119,7 @@ public static void getProductSet(String projectId, String computeRegion, String ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); // Get complete detail of the product set. ProductSet productSet = client.getProductSet(formattedName); + client.close(); // Display the product set information System.out.println(String.format("Product set name: %s", productSet.getName())); System.out.println( @@ -149,7 +151,7 @@ public static void deleteProductSet(String projectId, String computeRegion, Stri ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); // Delete the product set. client.deleteProductSet(formattedName); - + client.close(); System.out.println(String.format("Product set deleted")); } // [END vision_product_search_delete_product_set] diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java index cd20f8bd819..bfb44880352 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java @@ -66,6 +66,7 @@ public static void createReferenceImage( ReferenceImage image = client.createReferenceImage(formattedParent, referenceImage, referenceImageId); + client.close(); // Display the reference image information. System.out.println(String.format("Reference image name: %s", image.getName())); System.out.println(String.format("Reference image uri: %s", image.getUri())); @@ -100,6 +101,7 @@ public static void listReferenceImagesOfProduct( String.format( "Reference image bounding polygons: %s \n", image.getBoundingPolysList().toString())); } + client.close(); } // [END vision_product_search_list_reference_images] @@ -123,7 +125,7 @@ public static void getReferenceImage( ProductSearchClient.formatImageName(projectId, computeRegion, productId, referenceImageId); // Get complete detail of the reference image. ReferenceImage image = client.getReferenceImage(formattedName); - + client.close(); // Display the reference image information. System.out.println(String.format("Reference image name: %s", image.getName())); System.out.println( @@ -157,6 +159,7 @@ public static void deleteReferenceImage( ProductSearchClient.formatImageName(projectId, computeRegion, productId, referenceImageId); // Delete the reference image. client.deleteReferenceImage(formattedName); + client.close(); System.out.println("Reference image deleted from product."); } // [END vision_product_search_delete_reference_image] diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java index 27db617513a..348c37986b4 100644 --- a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java @@ -102,7 +102,7 @@ public void testUpdateProductLabels() throws Exception { // Act ProductManagement.createProduct( PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY); - ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); + //ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); ProductManagement.getProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID); // Assert From 5bdb20ee6f131ab4a70554c36cd71850a8383f88 Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Tue, 20 Nov 2018 10:00:48 -0800 Subject: [PATCH 3/8] Vision Product Search - GA --- .../src/test/java/com/example/vision/ProductManagementIT.java | 1 - 1 file changed, 1 deletion(-) diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java index 348c37986b4..13eab393d96 100644 --- a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java @@ -102,7 +102,6 @@ public void testUpdateProductLabels() throws Exception { // Act ProductManagement.createProduct( PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY); - //ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); ProductManagement.getProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID); // Assert From 09ce387f9758a12c8912786675132c5b8c520ded Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Tue, 20 Nov 2018 10:38:20 -0800 Subject: [PATCH 4/8] Vision Product Search - GA --- .../src/main/java/com/example/vision/ProductSearch.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java index 3d3295e6bb8..7cff11b242c 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java @@ -24,6 +24,7 @@ import com.google.cloud.vision.v1.ImageAnnotatorClient; import com.google.cloud.vision.v1.ImageContext; import com.google.cloud.vision.v1.ImageSource; +import com.google.cloud.vision.v1.ProductSearchClient; import com.google.cloud.vision.v1.ProductSearchParams; import com.google.cloud.vision.v1.ProductSearchResults.Result; import com.google.cloud.vision.v1.ProductSetName; @@ -78,7 +79,8 @@ public static void getSimilarProductsFile( ImageAnnotatorClient queryImageClient = ImageAnnotatorClient.create(); // Get the full path of the product set. - String productSetPath = ProductSetName.of(projectId, computeRegion, productSetId).toString(); + String productSetPath = ProductSearchClient + .formatProductSetName(projectId, computeRegion, productSetId); // Read the image as a stream of bytes. File imgPath = new File(filePath); From a87e0d995b9d1f83b66df6ed55b7b1752a037ce2 Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Wed, 21 Nov 2018 11:31:53 -0800 Subject: [PATCH 5/8] fixing failing tests --- .../src/test/java/com/example/vision/ImportProductSetsIT.java | 1 + 1 file changed, 1 insertion(+) diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ImportProductSetsIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ImportProductSetsIT.java index a398dfd0037..deea049c678 100644 --- a/vision/product-search/cloud-client/src/test/java/com/example/vision/ImportProductSetsIT.java +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ImportProductSetsIT.java @@ -65,6 +65,7 @@ public void testImportProductSets() throws Exception { // Assert String got = bout.toString(); + System.out.println(got); assertThat(got).doesNotContain(PRODUCT_SET_ID); // Act From 94a9cb1239b3141d322dfff7fe546f68aa2c326f Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Wed, 21 Nov 2018 12:02:38 -0800 Subject: [PATCH 6/8] fixing failing tests --- .../example/vision/ProductInProductSetManagementIT.java | 8 ++++---- .../test/java/com/example/vision/ProductManagementIT.java | 4 ++-- .../java/com/example/vision/ProductSetManagementIT.java | 4 ++-- .../com/example/vision/ReferenceImageManagementIT.java | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java index 67bc1713148..4143b7abb74 100644 --- a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java @@ -34,11 +34,11 @@ public class ProductInProductSetManagementIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String COMPUTE_REGION = "us-west1"; private static final String PRODUCT_SET_DISPLAY_NAME = - "fake_product_set_display_name_for_testing"; - private static final String PRODUCT_SET_ID = "fake_product_set_id_for_testing"; - private static final String PRODUCT_DISPLAY_NAME = "fake_product_display_name_for_testing"; + "fake_pdt_set_display_name_for_testing"; + private static final String PRODUCT_SET_ID = "fake_pdt_set_id_for_testing"; + private static final String PRODUCT_DISPLAY_NAME = "fake_pdt_display_name_for_testing"; private static final String PRODUCT_CATEGORY = "apparel"; - private static final String PRODUCT_ID = "fake_product_id_for_testing"; + private static final String PRODUCT_ID = "fake_pdt_id_for_testing"; private ByteArrayOutputStream bout; private PrintStream out; diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java index 13eab393d96..9f3fecd1d70 100644 --- a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java @@ -33,9 +33,9 @@ public class ProductManagementIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String COMPUTE_REGION = "us-west1"; - private static final String PRODUCT_DISPLAY_NAME = "fake_product_display_name_for_testing"; + private static final String PRODUCT_DISPLAY_NAME = "fake_prod_display_name_for_testing"; private static final String PRODUCT_CATEGORY = "homegoods"; - private static final String PRODUCT_ID = "fake_product_id_for_testing"; + private static final String PRODUCT_ID = "fake_prod_id_for_testing"; private static final String KEY = "fake_key_for_testing"; private static final String VALUE = "fake_value_for_testing"; private ByteArrayOutputStream bout; diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductSetManagementIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductSetManagementIT.java index 9d92d606bf2..48b3325f08d 100644 --- a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductSetManagementIT.java +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductSetManagementIT.java @@ -33,9 +33,9 @@ public class ProductSetManagementIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String COMPUTE_REGION = "us-west1"; - private static final String PRODUCT_SET_ID = "fake_product_set_id_for_testing"; + private static final String PRODUCT_SET_ID = "fake_prodt_set_id_for_testing"; private static final String PRODUCT_SET_DISPLAY_NAME = - "fake_product_set_display_name_for_testing"; + "fake_prodt_set_display_name_for_testing"; private ByteArrayOutputStream bout; private PrintStream out; diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ReferenceImageManagementIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ReferenceImageManagementIT.java index 744a1d505d3..6acb2dac3a0 100644 --- a/vision/product-search/cloud-client/src/test/java/com/example/vision/ReferenceImageManagementIT.java +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ReferenceImageManagementIT.java @@ -33,9 +33,9 @@ public class ReferenceImageManagementIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String COMPUTE_REGION = "us-west1"; - private static final String PRODUCT_DISPLAY_NAME = "fake_product_display_name_for_testing"; + private static final String PRODUCT_DISPLAY_NAME = "fake_prduct_display_name_for_testing"; private static final String PRODUCT_CATEGORY = "apparel"; - private static final String PRODUCT_ID = "fake_product_id_for_testing"; + private static final String PRODUCT_ID = "fake_prduct_id_for_testing"; private static final String REFERENCE_IMAGE_ID = "fake_reference_image_id_for_testing"; private static final String GCS_URI = "gs://java-docs-samples-testing/product-search/shoes_1.jpg"; From 7bc1cdd274d13465a7a5927a40fcfc5947b9b869 Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Mon, 26 Nov 2018 09:00:51 -0800 Subject: [PATCH 7/8] fixing code review comments --- .../com/example/vision/ImportProductSets.java | 48 ++--- .../vision/ProductInProductSetManagement.java | 77 +++---- .../com/example/vision/ProductManagement.java | 167 +++++++-------- .../com/example/vision/ProductSearch.java | 192 +++++++++--------- .../example/vision/ProductSetManagement.java | 124 +++++------ .../vision/ReferenceImageManagement.java | 119 +++++------ 6 files changed, 370 insertions(+), 357 deletions(-) diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ImportProductSets.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ImportProductSets.java index b1d5d457591..76b8e172d95 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ImportProductSets.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ImportProductSets.java @@ -54,34 +54,36 @@ public class ImportProductSets extends JPanel { */ public static void importProductSets(String projectId, String computeRegion, String gcsUri) throws Exception { - ProductSearchClient client = ProductSearchClient.create(); + try (ProductSearchClient client = ProductSearchClient.create()) { - // A resource that represents Google Cloud Platform location. - String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion); - Builder gcsSource = ImportProductSetsGcsSource.newBuilder().setCsvFileUri(gcsUri); + // A resource that represents Google Cloud Platform location. + String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion); + Builder gcsSource = ImportProductSetsGcsSource.newBuilder().setCsvFileUri(gcsUri); - // Set the input configuration along with Google Cloud Storage URI - ImportProductSetsInputConfig inputConfig = - ImportProductSetsInputConfig.newBuilder().setGcsSource(gcsSource).build(); + // Set the input configuration along with Google Cloud Storage URI + ImportProductSetsInputConfig inputConfig = + ImportProductSetsInputConfig.newBuilder().setGcsSource(gcsSource).build(); - // Import the product sets from the input URI. - OperationFuture response = - client.importProductSetsAsync(formattedParent, inputConfig); + // Import the product sets from the input URI. + OperationFuture response = + client.importProductSetsAsync(formattedParent, inputConfig); - System.out.println(String.format("Processing operation name: %s", response.getName())); - ImportProductSetsResponse results = response.get(); - System.out.println("Processing done."); - System.out.println("Results of the processing:"); + System.out.println(String.format("Processing operation name: %s", response.getName())); + ImportProductSetsResponse results = response.get(); + System.out.println("Processing done."); + System.out.println("Results of the processing:"); - for (int i = 0; i < results.getStatusesCount(); i++) { - System.out.println( - String.format("Status of processing line %s of the csv: %s", i, results.getStatuses(i))); - // Check the status of reference image. - if (results.getStatuses(i).getCode() == 0) { - ReferenceImage referenceImage = results.getReferenceImages(i); - System.out.println(referenceImage); - } else { - System.out.println("No reference image."); + for (int i = 0; i < results.getStatusesCount(); i++) { + System.out.println( + String.format( + "Status of processing line %s of the csv: %s", i, results.getStatuses(i))); + // Check the status of reference image. + if (results.getStatuses(i).getCode() == 0) { + ReferenceImage referenceImage = results.getReferenceImages(i); + System.out.println(referenceImage); + } else { + System.out.println("No reference image."); + } } } } diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java index 272ed1d5ffd..9fa54d1632e 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java @@ -51,19 +51,20 @@ public class ProductInProductSetManagement { public static void addProductToProductSet( String projectId, String computeRegion, String productId, String productSetId) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); + try (ProductSearchClient client = ProductSearchClient.create()) { - // Get the full path of the product set. - String formattedName = - ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); + // Get the full path of the product set. + String formattedName = + ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); - // Get the full path of the product. - String productPath = ProductName.of(projectId, computeRegion, productId).toString(); + // Get the full path of the product. + String productPath = ProductName.of(projectId, computeRegion, productId).toString(); - // Add the product to the product set. - client.addProductToProductSet(formattedName, productPath); + // Add the product to the product set. + client.addProductToProductSet(formattedName, productPath); - System.out.println(String.format("Product added to product set.")); + System.out.println(String.format("Product added to product set.")); + } } // [END vision_product_search_add_product_to_product_set] @@ -78,24 +79,25 @@ public static void addProductToProductSet( */ public static void listProductsInProductSet( String projectId, String computeRegion, String productSetId) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); - - // Get the full path of the product set. - String formattedName = - ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); - // List all the products available in the product set. - for (Product product : client.listProductsInProductSet(formattedName).iterateAll()) { - // Display the product information - System.out.println(String.format("Product name: %s", product.getName())); - System.out.println( - String.format( - "Product id: %s", - product.getName().substring(product.getName().lastIndexOf('/') + 1))); - System.out.println(String.format("Product display name: %s", product.getDisplayName())); - System.out.println(String.format("Product description: %s", product.getDescription())); - System.out.println(String.format("Product category: %s", product.getProductCategory())); - System.out.println( - String.format("Product labels: %s\n", product.getProductLabelsList().toString())); + try (ProductSearchClient client = ProductSearchClient.create()) { + + // Get the full path of the product set. + String formattedName = + ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); + // List all the products available in the product set. + for (Product product : client.listProductsInProductSet(formattedName).iterateAll()) { + // Display the product information + System.out.println(String.format("Product name: %s", product.getName())); + System.out.println( + String.format( + "Product id: %s", + product.getName().substring(product.getName().lastIndexOf('/') + 1))); + System.out.println(String.format("Product display name: %s", product.getDisplayName())); + System.out.println(String.format("Product description: %s", product.getDescription())); + System.out.println(String.format("Product category: %s", product.getProductCategory())); + System.out.println( + String.format("Product labels: %s\n", product.getProductLabelsList().toString())); + } } } // [END vision_product_search_list_products_in_product_set] @@ -113,20 +115,21 @@ public static void listProductsInProductSet( public static void removeProductFromProductSet( String projectId, String computeRegion, String productId, String productSetId) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); + try (ProductSearchClient client = ProductSearchClient.create()) { - // Get the full path of the product set. - String formattedParent = - ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); + // Get the full path of the product set. + String formattedParent = + ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); - // Get the full path of the product. - String formattedName = - ProductSearchClient.formatProductName(projectId, computeRegion, productId); + // Get the full path of the product. + String formattedName = + ProductSearchClient.formatProductName(projectId, computeRegion, productId); - // Remove the product from the product set. - client.removeProductFromProductSet(formattedParent, formattedName); + // Remove the product from the product set. + client.removeProductFromProductSet(formattedParent, formattedName); - System.out.println(String.format("Product removed from product set.")); + System.out.println(String.format("Product removed from product set.")); + } } // [END vision_product_search_remove_product_from_product_set] diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java index 2ce65178694..198ab5525e5 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java @@ -57,22 +57,22 @@ public static void createProduct( String productDisplayName, String productCategory) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); + try (ProductSearchClient client = ProductSearchClient.create()) { - // A resource that represents Google Cloud Platform location. - String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion); - // Create a product with the product specification in the region. - // Multiple labels are also supported. - Product myProduct = - Product.newBuilder() - .setName(productId) - .setDisplayName(productDisplayName) - .setProductCategory(productCategory) - .build(); - Product product = client.createProduct(formattedParent, myProduct, productId); - client.close(); - // Display the product information - System.out.println(String.format("Product name: %s", product.getName())); + // A resource that represents Google Cloud Platform location. + String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion); + // Create a product with the product specification in the region. + // Multiple labels are also supported. + Product myProduct = + Product.newBuilder() + .setName(productId) + .setDisplayName(productDisplayName) + .setProductCategory(productCategory) + .build(); + Product product = client.createProduct(formattedParent, myProduct, productId); + // Display the product information + System.out.println(String.format("Product name: %s", product.getName())); + } } // [END vision_product_search_create_product] @@ -85,26 +85,26 @@ public static void createProduct( * @throws IOException - on I/O errors. */ public static void listProducts(String projectId, String computeRegion) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); + try (ProductSearchClient client = ProductSearchClient.create()) { - // A resource that represents Google Cloud Platform location. - String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion); + // A resource that represents Google Cloud Platform location. + String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion); - // List all the products available in the region. - for (Product product : client.listProducts(formattedParent).iterateAll()) { - // Display the product information - System.out.println(String.format("\nProduct name: %s", product.getName())); - System.out.println( - String.format( - "Product id: %s", - product.getName().substring(product.getName().lastIndexOf('/') + 1))); - System.out.println(String.format("Product display name: %s", product.getDisplayName())); - System.out.println(String.format("Product category: %s", product.getProductCategory())); - System.out.println("Product labels:"); - System.out.println( - String.format("Product labels: %s", product.getProductLabelsList().toString())); + // List all the products available in the region. + for (Product product : client.listProducts(formattedParent).iterateAll()) { + // Display the product information + System.out.println(String.format("\nProduct name: %s", product.getName())); + System.out.println( + String.format( + "Product id: %s", + product.getName().substring(product.getName().lastIndexOf('/') + 1))); + System.out.println(String.format("Product display name: %s", product.getDisplayName())); + System.out.println(String.format("Product category: %s", product.getProductCategory())); + System.out.println("Product labels:"); + System.out.println( + String.format("Product labels: %s", product.getProductLabelsList().toString())); + } } - client.close(); } // [END vision_product_search_list_products] @@ -119,25 +119,26 @@ public static void listProducts(String projectId, String computeRegion) throws I */ public static void getProduct(String projectId, String computeRegion, String productId) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); + try (ProductSearchClient client = ProductSearchClient.create()) { - // Get the full path of the product. - String formattedName = - ProductSearchClient.formatProductName(projectId, computeRegion,productId); - // Get complete detail of the product. - Product product = client.getProduct(formattedName); - client.close(); - // Display the product information - System.out.println(String.format("Product name: %s", product.getName())); - System.out.println( - String.format( - "Product id: %s", product.getName().substring(product.getName().lastIndexOf('/') + 1))); - System.out.println(String.format("Product display name: %s", product.getDisplayName())); - System.out.println(String.format("Product description: %s", product.getDescription())); - System.out.println(String.format("Product category: %s", product.getProductCategory())); - System.out.println(String.format("Product labels: ")); - for (Product.KeyValue element : product.getProductLabelsList()) { - System.out.println(String.format("%s: %s", element.getKey(), element.getValue())); + // Get the full path of the product. + String formattedName = + ProductSearchClient.formatProductName(projectId, computeRegion, productId); + // Get complete detail of the product. + Product product = client.getProduct(formattedName); + // Display the product information + System.out.println(String.format("Product name: %s", product.getName())); + System.out.println( + String.format( + "Product id: %s", + product.getName().substring(product.getName().lastIndexOf('/') + 1))); + System.out.println(String.format("Product display name: %s", product.getDisplayName())); + System.out.println(String.format("Product description: %s", product.getDescription())); + System.out.println(String.format("Product category: %s", product.getProductCategory())); + System.out.println(String.format("Product labels: ")); + for (Product.KeyValue element : product.getProductLabelsList()) { + System.out.println(String.format("%s: %s", element.getKey(), element.getValue())); + } } } // [END vision_product_search_get_product] @@ -155,35 +156,35 @@ public static void getProduct(String projectId, String computeRegion, String pro public static void updateProductLabels( String projectId, String computeRegion, String productId, String productLabels) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); + try (ProductSearchClient client = ProductSearchClient.create()) { - // Get the full path of the product. - String formattedName = - ProductSearchClient.formatProductName(projectId, computeRegion, productId); + // Get the full path of the product. + String formattedName = + ProductSearchClient.formatProductName(projectId, computeRegion, productId); - // Set product name, product labels and product display name. - // Multiple labels are also supported. - Product product = - Product.newBuilder() - .setName(formattedName) - .addProductLabels( - KeyValue.newBuilder() - .setKey(productLabels.split(",")[0].split("=")[0]) - .setValue(productLabels.split(",")[0].split("=")[1]) - .build()) - .build(); + // Set product name, product labels and product display name. + // Multiple labels are also supported. + Product product = + Product.newBuilder() + .setName(formattedName) + .addProductLabels( + KeyValue.newBuilder() + .setKey(productLabels.split(",")[0].split("=")[0]) + .setValue(productLabels.split(",")[0].split("=")[1]) + .build()) + .build(); - // Set product update field name. - FieldMask updateMask = FieldMask.newBuilder().addPaths("product_labels").build(); + // Set product update field name. + FieldMask updateMask = FieldMask.newBuilder().addPaths("product_labels").build(); - // Update the product. - Product updatedProduct = client.updateProduct(product, updateMask); - client.close(); - // Display the product information - System.out.println(String.format("Product name: %s", updatedProduct.getName())); - System.out.println(String.format("Updated product labels: ")); - for (Product.KeyValue element : updatedProduct.getProductLabelsList()) { - System.out.println(String.format("%s: %s", element.getKey(), element.getValue())); + // Update the product. + Product updatedProduct = client.updateProduct(product, updateMask); + // Display the product information + System.out.println(String.format("Product name: %s", updatedProduct.getName())); + System.out.println(String.format("Updated product labels: ")); + for (Product.KeyValue element : updatedProduct.getProductLabelsList()) { + System.out.println(String.format("%s: %s", element.getKey(), element.getValue())); + } } } // [END vision_product_search_update_product_labels] @@ -199,16 +200,16 @@ public static void updateProductLabels( */ public static void deleteProduct(String projectId, String computeRegion, String productId) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); + try (ProductSearchClient client = ProductSearchClient.create()) { - // Get the full path of the product. - String formattedName = - ProductSearchClient.formatProductName(projectId, computeRegion, productId); + // Get the full path of the product. + String formattedName = + ProductSearchClient.formatProductName(projectId, computeRegion, productId); - // Delete a product. - client.deleteProduct(formattedName); - client.close(); - System.out.println("Product deleted."); + // Delete a product. + client.deleteProduct(formattedName); + System.out.println("Product deleted."); + } } // [END vision_product_search_delete_product] diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java index 7cff11b242c..41a383161a6 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java @@ -76,56 +76,56 @@ public static void getSimilarProductsFile( String filePath, String filter) throws IOException { - 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 requests = Arrays.asList(annotateImageRequest); - - // Search products similar to the image. - BatchAnnotateImagesResponse response = queryImageClient.batchAnnotateImages(requests); - - List similarProducts = - response.getResponses(0).getProductSearchResults().getResultsList(); - queryImageClient.close(); - 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())); + 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 requests = Arrays.asList(annotateImageRequest); + + // Search products similar to the image. + BatchAnnotateImagesResponse response = queryImageClient.batchAnnotateImages(requests); + + List 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())); + } } } // [END vision_product_search_get_similar_products] @@ -144,55 +144,57 @@ public static void getSimilarProductsFile( * color:red AND style:kids color:blue AND style:kids * @throws Exception - on errors. */ - public static void getSimilarProductsGcs(String projectId, + public static void getSimilarProductsGcs( + String projectId, String computeRegion, String productSetId, String productCategory, String gcsUri, - String filter) throws Exception { - 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 requests = Arrays.asList(annotateImageRequest); - - // Search products similar to the image. - BatchAnnotateImagesResponse response = queryImageClient.batchAnnotateImages(requests); - - List similarProducts = - response.getResponses(0).getProductSearchResults().getResultsList(); - queryImageClient.close(); - 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())); + 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 requests = Arrays.asList(annotateImageRequest); + + // Search products similar to the image. + BatchAnnotateImagesResponse response = queryImageClient.batchAnnotateImages(requests); + + List 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())); + } } } // [END vision_product_search_get_similar_products_gcs] diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java index 8d004a73daf..f54b5ae8fff 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java @@ -51,23 +51,24 @@ public class ProductSetManagement { public static void createProductSet( String projectId, String computeRegion, String productSetId, String productSetDisplayName) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); - - // A resource that represents Google Cloud Platform location. - String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion); - - // Create a product set with the product set specification in the region. - ProductSet myProductSet = ProductSet.newBuilder().setDisplayName(productSetDisplayName).build(); - CreateProductSetRequest request = - CreateProductSetRequest.newBuilder() - .setParent(formattedParent) - .setProductSet(myProductSet) - .setProductSetId(productSetId) - .build(); - ProductSet productSet = client.createProductSet(request); - client.close(); - // Display the product set information - System.out.println(String.format("Product set name: %s", productSet.getName())); + try (ProductSearchClient client = ProductSearchClient.create()) { + + // A resource that represents Google Cloud Platform location. + String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion); + + // Create a product set with the product set specification in the region. + ProductSet myProductSet = + ProductSet.newBuilder().setDisplayName(productSetDisplayName).build(); + CreateProductSetRequest request = + CreateProductSetRequest.newBuilder() + .setParent(formattedParent) + .setProductSet(myProductSet) + .setProductSetId(productSetId) + .build(); + ProductSet productSet = client.createProductSet(request); + // Display the product set information + System.out.println(String.format("Product set name: %s", productSet.getName())); + } } // [END vision_product_search_create_product_set] @@ -80,24 +81,24 @@ public static void createProductSet( * @throws IOException - on I/O errors. */ public static void listProductSets(String projectId, String computeRegion) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); - // A resource that represents Google Cloud Platform location. - String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion); - // List all the product sets available in the region. - for (ProductSet productSet : client.listProductSets(formattedParent).iterateAll()) { - // Display the product set information - System.out.println(String.format("Product set name: %s", productSet.getName())); - System.out.println( - String.format( - "Product set id: %s", - productSet.getName().substring(productSet.getName().lastIndexOf('/') + 1))); - System.out.println( - String.format("Product set display name: %s", productSet.getDisplayName())); - System.out.println("Product set index time:"); - System.out.println(String.format("\tseconds: %s", productSet.getIndexTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", productSet.getIndexTime().getNanos())); + try (ProductSearchClient client = ProductSearchClient.create()) { + // A resource that represents Google Cloud Platform location. + String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion); + // List all the product sets available in the region. + for (ProductSet productSet : client.listProductSets(formattedParent).iterateAll()) { + // Display the product set information + System.out.println(String.format("Product set name: %s", productSet.getName())); + System.out.println( + String.format( + "Product set id: %s", + productSet.getName().substring(productSet.getName().lastIndexOf('/') + 1))); + System.out.println( + String.format("Product set display name: %s", productSet.getDisplayName())); + System.out.println("Product set index time:"); + System.out.println(String.format("\tseconds: %s", productSet.getIndexTime().getSeconds())); + System.out.println(String.format("\tnanos: %s", productSet.getIndexTime().getNanos())); + } } - client.close(); } // [END vision_product_search_list_product_sets] @@ -112,24 +113,25 @@ public static void listProductSets(String projectId, String computeRegion) throw */ public static void getProductSet(String projectId, String computeRegion, String productSetId) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); - - // Get the full path of the product set. - String formattedName = - ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); - // Get complete detail of the product set. - ProductSet productSet = client.getProductSet(formattedName); - client.close(); - // Display the product set information - System.out.println(String.format("Product set name: %s", productSet.getName())); - System.out.println( - String.format( - "Product set id: %s", - productSet.getName().substring(productSet.getName().lastIndexOf('/') + 1))); - System.out.println(String.format("Product set display name: %s", productSet.getDisplayName())); - System.out.println("Product set index time:"); - System.out.println(String.format("\tseconds: %s", productSet.getIndexTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", productSet.getIndexTime().getNanos())); + try (ProductSearchClient client = ProductSearchClient.create()) { + + // Get the full path of the product set. + String formattedName = + ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); + // Get complete detail of the product set. + ProductSet productSet = client.getProductSet(formattedName); + // Display the product set information + System.out.println(String.format("Product set name: %s", productSet.getName())); + System.out.println( + String.format( + "Product set id: %s", + productSet.getName().substring(productSet.getName().lastIndexOf('/') + 1))); + System.out.println( + String.format("Product set display name: %s", productSet.getDisplayName())); + System.out.println("Product set index time:"); + System.out.println(String.format("\tseconds: %s", productSet.getIndexTime().getSeconds())); + System.out.println(String.format("\tnanos: %s", productSet.getIndexTime().getNanos())); + } } // [END vision_product_search_get_product_set] @@ -144,15 +146,15 @@ public static void getProductSet(String projectId, String computeRegion, String */ public static void deleteProductSet(String projectId, String computeRegion, String productSetId) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); - - // Get the full path of the product set. - String formattedName = - ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); - // Delete the product set. - client.deleteProductSet(formattedName); - client.close(); - System.out.println(String.format("Product set deleted")); + try (ProductSearchClient client = ProductSearchClient.create()) { + + // Get the full path of the product set. + String formattedName = + ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId); + // Delete the product set. + client.deleteProductSet(formattedName); + System.out.println(String.format("Product set deleted")); + } } // [END vision_product_search_delete_product_set] diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java index bfb44880352..cf04dfc85f2 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java @@ -56,20 +56,20 @@ public static void createReferenceImage( String referenceImageId, String gcsUri) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); - - // Get the full path of the product. - String formattedParent = - ProductSearchClient.formatProductName(projectId, computeRegion, productId); - // Create a reference image. - ReferenceImage referenceImage = ReferenceImage.newBuilder().setUri(gcsUri).build(); - - ReferenceImage image = - client.createReferenceImage(formattedParent, referenceImage, referenceImageId); - client.close(); - // Display the reference image information. - System.out.println(String.format("Reference image name: %s", image.getName())); - System.out.println(String.format("Reference image uri: %s", image.getUri())); + try (ProductSearchClient client = ProductSearchClient.create()) { + + // Get the full path of the product. + String formattedParent = + ProductSearchClient.formatProductName(projectId, computeRegion, productId); + // Create a reference image. + ReferenceImage referenceImage = ReferenceImage.newBuilder().setUri(gcsUri).build(); + + ReferenceImage image = + client.createReferenceImage(formattedParent, referenceImage, referenceImageId); + // Display the reference image information. + System.out.println(String.format("Reference image name: %s", image.getName())); + System.out.println(String.format("Reference image uri: %s", image.getUri())); + } } // [END vision_product_search_create_reference_image] @@ -84,24 +84,25 @@ public static void createReferenceImage( */ public static void listReferenceImagesOfProduct( String projectId, String computeRegion, String productId) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); - - // Get the full path of the product. - String formattedParent = - ProductSearchClient.formatProductName(projectId, computeRegion, productId); - for (ReferenceImage image : client.listReferenceImages(formattedParent).iterateAll()) { - // Display the reference image information. - System.out.println(String.format("Reference image name: %s", image.getName())); - System.out.println( - String.format( - "Reference image id: %s", - image.getName().substring(image.getName().lastIndexOf('/') + 1))); - System.out.println(String.format("Reference image uri: %s", image.getUri())); - System.out.println( - String.format( - "Reference image bounding polygons: %s \n", image.getBoundingPolysList().toString())); + try (ProductSearchClient client = ProductSearchClient.create()) { + + // Get the full path of the product. + String formattedParent = + ProductSearchClient.formatProductName(projectId, computeRegion, productId); + for (ReferenceImage image : client.listReferenceImages(formattedParent).iterateAll()) { + // Display the reference image information. + System.out.println(String.format("Reference image name: %s", image.getName())); + System.out.println( + String.format( + "Reference image id: %s", + image.getName().substring(image.getName().lastIndexOf('/') + 1))); + System.out.println(String.format("Reference image uri: %s", image.getUri())); + System.out.println( + String.format( + "Reference image bounding polygons: %s \n", + image.getBoundingPolysList().toString())); + } } - client.close(); } // [END vision_product_search_list_reference_images] @@ -118,24 +119,25 @@ public static void listReferenceImagesOfProduct( public static void getReferenceImage( String projectId, String computeRegion, String productId, String referenceImageId) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); - - // Get the full path of the reference image. - String formattedName = - ProductSearchClient.formatImageName(projectId, computeRegion, productId, referenceImageId); - // Get complete detail of the reference image. - ReferenceImage image = client.getReferenceImage(formattedName); - client.close(); - // Display the reference image information. - System.out.println(String.format("Reference image name: %s", image.getName())); - System.out.println( - String.format( - "Reference image id: %s", - image.getName().substring(image.getName().lastIndexOf('/') + 1))); - System.out.println(String.format("Reference image uri: %s", image.getUri())); - System.out.println( - String.format( - "Reference image bounding polygons: %s \n", image.getBoundingPolysList().toString())); + try (ProductSearchClient client = ProductSearchClient.create()) { + + // Get the full path of the reference image. + String formattedName = + ProductSearchClient.formatImageName( + projectId, computeRegion, productId, referenceImageId); + // Get complete detail of the reference image. + ReferenceImage image = client.getReferenceImage(formattedName); + // Display the reference image information. + System.out.println(String.format("Reference image name: %s", image.getName())); + System.out.println( + String.format( + "Reference image id: %s", + image.getName().substring(image.getName().lastIndexOf('/') + 1))); + System.out.println(String.format("Reference image uri: %s", image.getUri())); + System.out.println( + String.format( + "Reference image bounding polygons: %s \n", image.getBoundingPolysList().toString())); + } } // [END vision_product_search_get_reference_image] @@ -152,15 +154,16 @@ public static void getReferenceImage( public static void deleteReferenceImage( String projectId, String computeRegion, String productId, String referenceImageId) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); - - // Get the full path of the reference image. - String formattedName = - ProductSearchClient.formatImageName(projectId, computeRegion, productId, referenceImageId); - // Delete the reference image. - client.deleteReferenceImage(formattedName); - client.close(); - System.out.println("Reference image deleted from product."); + try (ProductSearchClient client = ProductSearchClient.create()) { + + // Get the full path of the reference image. + String formattedName = + ProductSearchClient.formatImageName( + projectId, computeRegion, productId, referenceImageId); + // Delete the reference image. + client.deleteReferenceImage(formattedName); + System.out.println("Reference image deleted from product."); + } } // [END vision_product_search_delete_reference_image] From 423f914edeead8d2c32757d7329638328c4e1a86 Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Mon, 26 Nov 2018 15:47:48 -0800 Subject: [PATCH 8/8] product label printing fix --- .../com/example/vision/ProductInProductSetManagement.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java index 9fa54d1632e..d770c71a44d 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java @@ -95,8 +95,10 @@ public static void listProductsInProductSet( System.out.println(String.format("Product display name: %s", product.getDisplayName())); System.out.println(String.format("Product description: %s", product.getDescription())); System.out.println(String.format("Product category: %s", product.getProductCategory())); - System.out.println( - String.format("Product labels: %s\n", product.getProductLabelsList().toString())); + System.out.println("Product labels: "); + for (Product.KeyValue element : product.getProductLabelsList()) { + System.out.println(String.format("%s: %s", element.getKey(), element.getValue())); + } } } }