diff --git a/vision/snippets/src/main/java/com/example/vision/ImportProductSets.java b/vision/snippets/src/main/java/com/example/vision/ImportProductSets.java index ae8f08b2c9c..76b8e172d95 100644 --- a/vision/snippets/src/main/java/com/example/vision/ImportProductSets.java +++ b/vision/snippets/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] /** @@ -57,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. - LocationName projectLocation = LocationName.of(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(projectLocation, 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/snippets/src/main/java/com/example/vision/ProductInProductSetManagement.java b/vision/snippets/src/main/java/com/example/vision/ProductInProductSetManagement.java index 06f686ec998..d770c71a44d 100644 --- a/vision/snippets/src/main/java/com/example/vision/ProductInProductSetManagement.java +++ b/vision/snippets/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] @@ -56,18 +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. - ProductSetName productSetPath = ProductSetName.of(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(productSetPath, 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] @@ -82,25 +79,27 @@ 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. - ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId); - - // List all the products available in the product set. - for (Product product : - client.listProductsInProductSet(productSetPath.toString()).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("Product labels: "); + for (Product.KeyValue element : product.getProductLabelsList()) { + System.out.println(String.format("%s: %s", element.getKey(), element.getValue())); + } + } } } // [END vision_product_search_list_products_in_product_set] @@ -118,18 +117,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. - ProductSetName productSetPath = ProductSetName.of(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 productPath = ProductName.of(projectId, computeRegion, productId).toString(); + // Get the full path of the product. + String formattedName = + ProductSearchClient.formatProductName(projectId, computeRegion, productId); - // Remove the product from the product set. - client.removeProductFromProductSet(productSetPath, productPath); + // 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/snippets/src/main/java/com/example/vision/ProductManagement.java b/vision/snippets/src/main/java/com/example/vision/ProductManagement.java index 4f5fc7dbb07..198ab5525e5 100644 --- a/vision/snippets/src/main/java/com/example/vision/ProductManagement.java +++ b/vision/snippets/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; @@ -59,23 +57,22 @@ public static void createProduct( String productDisplayName, String productCategory) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); - - // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(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(projectLocation.toString(), myProduct, productId); - - // Display the product information - System.out.println(String.format("Product name: %s", product.getName())); + 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); + // Display the product information + System.out.println(String.format("Product name: %s", product.getName())); + } } // [END vision_product_search_create_product] @@ -88,24 +85,25 @@ 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. - LocationName projectLocation = LocationName.of(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(projectLocation).iterateAll()) { - // Display the product information - System.out.println(String.format("\nProduct name: %s", product.getName())); - System.out.println( - String.format( + // 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())); + 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())); + } } } // [END vision_product_search_list_products] @@ -121,25 +119,27 @@ 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. - ProductName productPath = ProductName.of(projectId, computeRegion, productId); - - // Get complete detail of the product. - Product product = client.getProduct(productPath.toString()); - - // 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("Product labels:"); - System.out.println( - String.format("Product labels: %s", product.getProductLabelsList().toString())); + // 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] @@ -156,34 +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(); - - // Get the full path of the product. - String productPath = ProductName.of(projectId, computeRegion, productId).toString(); - - // Set product name, product labels and product display name. - // Multiple labels are also supported. - Product product = - Product.newBuilder() - .setName(productPath) - .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(); - - // 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())); + try (ProductSearchClient client = ProductSearchClient.create()) { + + // 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 update field name. + FieldMask updateMask = FieldMask.newBuilder().addPaths("product_labels").build(); + + // 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,15 +200,16 @@ public static void updateProductLabels( */ public static void deleteProduct(String projectId, String computeRegion, String productId) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); - - // Get the full path of the product. - ProductName productPath = ProductName.of(projectId, computeRegion, productId); + try (ProductSearchClient client = ProductSearchClient.create()) { - // Delete a product. - client.deleteProduct(productPath); + // Get the full path of the product. + String formattedName = + ProductSearchClient.formatProductName(projectId, computeRegion, productId); - 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/snippets/src/main/java/com/example/vision/ProductSearch.java b/vision/snippets/src/main/java/com/example/vision/ProductSearch.java index 2e18e806226..41a383161a6 100644 --- a/vision/snippets/src/main/java/com/example/vision/ProductSearch.java +++ b/vision/snippets/src/main/java/com/example/vision/ProductSearch.java @@ -16,17 +16,18 @@ 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.ProductSearchClient; +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; @@ -75,55 +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 = ProductSetName.of(projectId, computeRegion, productSetId).toString(); - - // 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())); + 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] @@ -142,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(); - - 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/snippets/src/main/java/com/example/vision/ProductSetManagement.java b/vision/snippets/src/main/java/com/example/vision/ProductSetManagement.java index 1b3b9c29560..f54b5ae8fff 100644 --- a/vision/snippets/src/main/java/com/example/vision/ProductSetManagement.java +++ b/vision/snippets/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] @@ -56,24 +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. - LocationName projectLocation = LocationName.of(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()) - .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())); - + 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] @@ -86,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. - LocationName projectLocation = LocationName.of(projectId, computeRegion); - // List all the product sets available in the region. - for (ProductSet productSet : client.listProductSets(projectLocation).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())); + } } - } // [END vision_product_search_list_product_sets] @@ -118,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. - ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId); - - // Get complete detail of the product set. - ProductSet productSet = client.getProductSet(productSetPath); - - // 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] @@ -148,17 +144,17 @@ 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 { - ProductSearchClient client = ProductSearchClient.create(); - - // Get the full path of the product set. - ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId); - - // Delete the product set. - client.deleteProductSet(productSetPath.toString()); - - System.out.println(String.format("Product set deleted")); + public static void deleteProductSet(String projectId, String computeRegion, String productSetId) + throws IOException { + 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/snippets/src/main/java/com/example/vision/ReferenceImageManagement.java b/vision/snippets/src/main/java/com/example/vision/ReferenceImageManagement.java index c9413259d5b..cf04dfc85f2 100644 --- a/vision/snippets/src/main/java/com/example/vision/ReferenceImageManagement.java +++ b/vision/snippets/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] @@ -60,19 +56,20 @@ public static void createReferenceImage( String referenceImageId, String gcsUri) throws IOException { - ProductSearchClient client = ProductSearchClient.create(); - - // Get the full path of the product. - ProductName productPath = ProductName.of(projectId, computeRegion, productId); + try (ProductSearchClient client = ProductSearchClient.create()) { - // Create a reference image. - ReferenceImage referenceImage = ReferenceImage.newBuilder().setUri(gcsUri).build(); - ReferenceImage image = - client.createReferenceImage(productPath, referenceImage, referenceImageId); + // 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(); - // 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())); + 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] @@ -87,22 +84,24 @@ 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. - ProductName productPath = ProductName.of(projectId, computeRegion, productId); - - for (ReferenceImage image : client.listReferenceImages(productPath.toString()).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())); + } } } // [END vision_product_search_list_reference_images] @@ -120,25 +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. - ReferenceImageName referenceImagePath = - ReferenceImageName.of(projectId, computeRegion, productId, referenceImageId); - - // Get complete detail of the reference image. - ReferenceImage image = client.getReferenceImage(referenceImagePath); - - // 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] @@ -155,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. - ReferenceImageName referenceImagePath = - ReferenceImageName.of(projectId, computeRegion, productId, referenceImageId); - - // Delete the reference image. - client.deleteReferenceImage(referenceImagePath.toString()); - 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] diff --git a/vision/snippets/src/test/java/com/example/vision/ImportProductSetsIT.java b/vision/snippets/src/test/java/com/example/vision/ImportProductSetsIT.java index a398dfd0037..deea049c678 100644 --- a/vision/snippets/src/test/java/com/example/vision/ImportProductSetsIT.java +++ b/vision/snippets/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 diff --git a/vision/snippets/src/test/java/com/example/vision/ProductInProductSetManagementIT.java b/vision/snippets/src/test/java/com/example/vision/ProductInProductSetManagementIT.java index 67bc1713148..4143b7abb74 100644 --- a/vision/snippets/src/test/java/com/example/vision/ProductInProductSetManagementIT.java +++ b/vision/snippets/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/snippets/src/test/java/com/example/vision/ProductManagementIT.java b/vision/snippets/src/test/java/com/example/vision/ProductManagementIT.java index 27db617513a..9f3fecd1d70 100644 --- a/vision/snippets/src/test/java/com/example/vision/ProductManagementIT.java +++ b/vision/snippets/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; @@ -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 diff --git a/vision/snippets/src/test/java/com/example/vision/ProductSearchIT.java b/vision/snippets/src/test/java/com/example/vision/ProductSearchIT.java index 812ab556c24..367c4965eef 100644 --- a/vision/snippets/src/test/java/com/example/vision/ProductSearchIT.java +++ b/vision/snippets/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; diff --git a/vision/snippets/src/test/java/com/example/vision/ProductSetManagementIT.java b/vision/snippets/src/test/java/com/example/vision/ProductSetManagementIT.java index 9d92d606bf2..48b3325f08d 100644 --- a/vision/snippets/src/test/java/com/example/vision/ProductSetManagementIT.java +++ b/vision/snippets/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/snippets/src/test/java/com/example/vision/ReferenceImageManagementIT.java b/vision/snippets/src/test/java/com/example/vision/ReferenceImageManagementIT.java index 744a1d505d3..6acb2dac3a0 100644 --- a/vision/snippets/src/test/java/com/example/vision/ReferenceImageManagementIT.java +++ b/vision/snippets/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";