diff --git a/vision/product-search/cloud-client/pom.xml b/vision/product-search/cloud-client/pom.xml new file mode 100644 index 00000000000..6c7a5762e50 --- /dev/null +++ b/vision/product-search/cloud-client/pom.xml @@ -0,0 +1,248 @@ + + + 4.0.0 + com.example.vision + vision-detect-product-search + jar + + + + com.google.cloud.samples + shared-configuration + 1.0.9 + + + + 1.8 + 1.8 + UTF-8 + + + + + + com.google.cloud + google-cloud-vision + 1.37.1 + + + com.google.cloud + google-cloud-storage + 1.37.1 + + + net.sourceforge.argparse4j + argparse4j + 0.8.1 + + + org.apache.commons + commons-csv + 1.5 + + + + + + junit + junit + 4.12 + test + + + + com.google.truth + truth + 0.40 + test + + + + + + ImportProductSets + + + ImportProductSets + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + + java + + + + + com.example.vision.ImportProductSets + false + + + + + + + ProductInProductSetManagement + + + ProductInProductSetManagement + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + + java + + + + + com.example.vision.ProductInProductSetManagement + false + + + + + + + ProductManagement + + + ProductManagement + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + + java + + + + + com.example.vision.ProductManagement + false + + + + + + + ProductSearch + + + ProductSearch + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + + java + + + + + com.example.vision.ProductSearch + false + + + + + + + ProductSetManagement + + + ProductSetManagement + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + + java + + + + + com.example.vision.ProductSetManagement + false + + + + + + + ReferenceImageManagement + + + ReferenceImageManagement + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + + java + + + + + com.example.vision.ReferenceImageManagement + false + + + + + + + diff --git a/vision/product-search/cloud-client/resources/indexed_product_sets.csv b/vision/product-search/cloud-client/resources/indexed_product_sets.csv new file mode 100644 index 00000000000..e31f99f44e3 --- /dev/null +++ b/vision/product-search/cloud-client/resources/indexed_product_sets.csv @@ -0,0 +1,2 @@ +"gs://java-docs-samples-testing/product-search/shoes_1.jpg","indexed_product_set_id_for_testing","indexed_product_id_for_testing_1","apparel","style=womens","0.1,0.1,0.9,0.1,0.9,0.9,0.1,0.9" +"gs://java-docs-samples-testing/product-search/shoes_2.jpg","indexed_product_set_id_for_testing","indexed_product_id_for_testing_2","apparel",, \ No newline at end of file diff --git a/vision/product-search/cloud-client/resources/product_sets.csv b/vision/product-search/cloud-client/resources/product_sets.csv new file mode 100644 index 00000000000..314cd030f6c --- /dev/null +++ b/vision/product-search/cloud-client/resources/product_sets.csv @@ -0,0 +1,2 @@ +"gs://java-docs-samples-testing/product-search/shoes_1.jpg","fake_product_set_id_for_testing","fake_product_id_for_testing_1","apparel","style=womens","0.1,0.1,0.9,0.1,0.9,0.9,0.1,0.9" +"gs://java-docs-samples-testing/product-search/shoes_2.jpg","fake_product_set_id_for_testing","fake_product_id_for_testing_2","apparel",, \ No newline at end of file diff --git a/vision/product-search/cloud-client/resources/shoes_1.jpg b/vision/product-search/cloud-client/resources/shoes_1.jpg new file mode 100644 index 00000000000..78318eeff66 Binary files /dev/null and b/vision/product-search/cloud-client/resources/shoes_1.jpg differ diff --git a/vision/product-search/cloud-client/resources/shoes_2.jpg b/vision/product-search/cloud-client/resources/shoes_2.jpg new file mode 100644 index 00000000000..cdfa80dd899 Binary files /dev/null and b/vision/product-search/cloud-client/resources/shoes_2.jpg differ 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 new file mode 100644 index 00000000000..04c0a15783e --- /dev/null +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ImportProductSets.java @@ -0,0 +1,119 @@ +/* + * Copyright 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +// [START product_search_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 java.io.PrintStream; +import javax.swing.JPanel; + +import net.sourceforge.argparse4j.ArgumentParsers; +import net.sourceforge.argparse4j.inf.ArgumentParser; +import net.sourceforge.argparse4j.inf.ArgumentParserException; +import net.sourceforge.argparse4j.inf.Namespace; +import net.sourceforge.argparse4j.inf.Subparser; +import net.sourceforge.argparse4j.inf.Subparsers; +// [END product_search_import] + +/** + * This application demonstrates how to Import Product Sets in Cloud Vision + * Product Search. + * + * For more information, see the tutorial page at + * https://cloud.google.com/vision/product-search/docs/ + */ + +public class ImportProductSets extends JPanel { + // [START product_search_import_product_sets] + /** + * Import images of different products in the product set. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param gcsUri - Google Cloud Storage URI.Target files must be in Product Search CSV format. + * @throws Exception - on client errors. + */ + public static void importProductSets(String projectId, String computeRegion, String gcsUri) + throws Exception { + ProductSearchClient client = ProductSearchClient.create(); + + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(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(); + + // Import the product sets from the input URI. + OperationFuture response = + client.importProductSetsAsync(projectLocation, 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:"); + + 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."); + } + } + } + // [END product_search_import_product_sets] + + public static void main(String[] args) throws Exception { + ImportProductSets importProductSet = new ImportProductSets(); + importProductSet.argsHelper(args, System.out); + } + + public static void argsHelper(String[] args, PrintStream out) throws Exception { + ArgumentParser parser = ArgumentParsers.newFor("Import Product Sets").build(); + Subparsers subparsers = parser.addSubparsers().dest("command"); + + Subparser importProductSetsParser = subparsers.addParser("import_product_sets"); + importProductSetsParser.addArgument("gcsUri"); + + String projectId = System.getenv("PROJECT_ID"); + String computeRegion = System.getenv("REGION_NAME"); + + Namespace ns = null; + try { + ns = parser.parseArgs(args); + if (ns.get("command").equals("import_product_sets")) { + importProductSets(projectId, computeRegion, ns.getString("gcsUri")); + } + } catch (ArgumentParserException e) { + parser.handleError(e); + } + } +} 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 new file mode 100644 index 00000000000..95c38109cdf --- /dev/null +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java @@ -0,0 +1,241 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +// [START product_search_import] +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.v1p3beta1.UpdateProductSetRequest; +import com.google.protobuf.FieldMask; + +import java.io.IOException; +import java.io.PrintStream; + +import net.sourceforge.argparse4j.ArgumentParsers; +import net.sourceforge.argparse4j.inf.ArgumentParser; +import net.sourceforge.argparse4j.inf.ArgumentParserException; +import net.sourceforge.argparse4j.inf.Namespace; +import net.sourceforge.argparse4j.inf.Subparser; +import net.sourceforge.argparse4j.inf.Subparsers; +// [END product_search_import] + +/** + * This application demonstrates how to perform basic operations with Products in a Product Set. + * + * For more information, see the tutorial page at + * https://cloud.google.com/vision/product-search/docs/ + */ + +public class ProductInProductSetManagement { + + // [START product_search_add_product_to_product_set] + /** + * Update a product set. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param productSetId - Id of the product set. + * @param productSetDisplayName - Display name of the product set. + * @throws IOException - on I/O errors. + */ + public static void addProductToProductSet( + String projectId, String computeRegion, String productSetId, String productSetDisplayName) + throws IOException { + ProductSearchClient client = ProductSearchClient.create(); + + // Get the full path of the product set. + String productSetPath = ProductSetName.of(projectId, computeRegion, productSetId).toString(); + + // Update the product set display name. + ProductSet productSet = + ProductSet.newBuilder() + .setName(productSetPath) + .setDisplayName(productSetDisplayName) + .build(); + + FieldMask updateMask = FieldMask.newBuilder().addPaths("display_name").build(); + + UpdateProductSetRequest request = + UpdateProductSetRequest.newBuilder() + .setProductSet(productSet) + .setUpdateMask(updateMask) + .build(); + + ProductSet updatedProductSet = client.updateProductSet(request); + + // Display the updated set information + System.out.println(String.format("Product set name: %s", updatedProductSet.getName())); + System.out.println( + String.format( + "Product set id: %s", + updatedProductSet + .getName() + .substring(updatedProductSet.getName().lastIndexOf('/') + 1))); + System.out.println( + String.format("Updated product set display name: %s", updatedProductSet.getDisplayName())); + } + // [END product_search_add_product_to_product_set] + + // [START product_search_remove_product_from_product_set] + /** + * Remove a product from a product set. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param productId - Id of the product. + * @param productSetId - Id of the product set. + * @throws IOException - on I/O errors. + */ + public static void removeProductFromProductSet( + String projectId, String computeRegion, String productId, String productSetId) + throws IOException { + 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. + String productPath = ProductName.of(projectId, computeRegion, productId).toString(); + + // Remove the product from the product set. + client.removeProductFromProductSet(productSetPath, productPath); + + System.out.println(String.format("Product removed from product set.")); + } + // [END product_search_remove_product_from_product_set] + + // [START product_search_list_products_in_product_set] + /** + * List all products in a product set. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param productSetId - Id of the product set. + * @throws IOException - on I/O errors. + */ + 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())); + } + } + // [END product_search_list_products_in_product_set] + + // [START product_search_list_products] + /** + * List all products. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @throws IOException - on I/O errors. + */ + public static void listProducts(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 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( + "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())); + } + } + // [END product_search_list_products] + + public static void main(String[] args) throws Exception { + ProductInProductSetManagement productInProductSetManagement = + new ProductInProductSetManagement(); + productInProductSetManagement.argsHelper(args, System.out); + } + + public static void argsHelper(String[] args, PrintStream out) throws Exception { + ArgumentParser parser = ArgumentParsers.newFor("").build(); + Subparsers subparsers = parser.addSubparsers().dest("command"); + subparsers.addParser("list_products"); + + Subparser listProductInProductSetParser = subparsers.addParser("list_products_in_product_set"); + listProductInProductSetParser.addArgument("productSetId"); + + Subparser updateProductSetParser = subparsers.addParser("update_product_set"); + updateProductSetParser.addArgument("productSetId"); + updateProductSetParser.addArgument("productSetDisplayName"); + + Subparser removeProductFromProductSetParser = + subparsers.addParser("remove_product_from_product_set"); + removeProductFromProductSetParser.addArgument("productId"); + removeProductFromProductSetParser.addArgument("productSetId"); + + String projectId = System.getenv("PROJECT_ID"); + String computeRegion = System.getenv("REGION_NAME"); + + Namespace ns = null; + try { + ns = parser.parseArgs(args); + if (ns.get("command").equals("list_products")) { + listProducts(projectId, computeRegion); + } + if (ns.get("command").equals("update_product_set")) { + addProductToProductSet( + projectId, + computeRegion, + ns.getString("productSetId"), + ns.getString("productSetDisplayName")); + } + if (ns.get("command").equals("remove_product_from_product_set")) { + removeProductFromProductSet( + projectId, computeRegion, ns.getString("productId"), ns.getString("productSetId")); + } + if (ns.get("command").equals("list_products_in_product_set")) { + listProductsInProductSet(projectId, computeRegion, ns.getString("productSetId")); + } + + } catch (ArgumentParserException e) { + parser.handleError(e); + } + } +} 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 new file mode 100644 index 00000000000..c026b4913e0 --- /dev/null +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java @@ -0,0 +1,288 @@ +/* + * Copyright 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +// [START product_search_import] +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.protobuf.FieldMask; + +import java.io.IOException; +import java.io.PrintStream; + +import net.sourceforge.argparse4j.ArgumentParsers; +import net.sourceforge.argparse4j.inf.ArgumentParser; +import net.sourceforge.argparse4j.inf.ArgumentParserException; +import net.sourceforge.argparse4j.inf.Namespace; +import net.sourceforge.argparse4j.inf.Subparser; +import net.sourceforge.argparse4j.inf.Subparsers; +// [END product_search_import] + +/** + * This application demonstrates how to perform basic operations on Products. + * + * For more information, see the tutorial page at + * https://cloud.google.com/vision/product-search/docs/ + */ + +public class ProductManagement { + + // [START product_search_create_product] + /** + * Create one product. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param productId - Id of the product. + * @param productDisplayName - Display name of the product. + * @param productCategory - Category of the product. + * @param productDescription - Description of the product. + * @param productLabels - Labels of the product. + * @throws IOException - on I/O errors. + */ + public static void createProduct( + String projectId, + String computeRegion, + String productId, + String productDisplayName, + String productCategory, + String productDescription, + String productLabels) + 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) + .setDescription(productDescription) + .addProductLabels( + KeyValue.newBuilder() + .setKey(productLabels.split(",")[0].split("=")[0]) + .setValue(productLabels.split(",")[0].split("=")[1]) + .build()) + .build(); + Product product = client.createProduct(projectLocation.toString(), myProduct, productId); + + // Display the product information + System.out.println(String.format("Product name: %s", product.getName())); + } + // [END product_search_create_product] + + // [START product_search_list_products] + /** + * List all products. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @throws IOException - on I/O errors. + */ + public static void listProducts(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 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( + "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())); + } + } + // [END product_search_list_products] + + // [START product_search_get_product] + /** + * Get information about a product. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param productId - Id of the product. + * @throws IOException - on I/O errors. + */ + public static void getProduct(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); + + // 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())); + } + // [END product_search_get_product] + + // [START product_search_update_product_labels] + /** + * Update the product labels. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param productId -Id of the product. + * @param productLabels - Labels of the product. + * @throws IOException - on I/O errors. + */ + 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: %s", updatedProduct.getProductLabelsList().toString())); + } + // [END product_search_update_product_labels] + + // [START product_search_delete_product] + /** + * Delete the product and all its reference images. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param productId - Id of the product. + * @throws IOException - on I/O errors. + */ + 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); + + // Delete a product. + client.deleteProduct(productPath); + + System.out.println("Product deleted."); + } + // [END product_search_delete_product] + + public static void main(String[] args) throws Exception { + ProductManagement productManagement = new ProductManagement(); + productManagement.argsHelper(args, System.out); + } + + public void argsHelper(String[] args, PrintStream out) throws Exception { + ArgumentParser parser = ArgumentParsers.newFor("Product Management").build(); + Subparsers subparsers = parser.addSubparsers().dest("command"); + + Subparser createProductParser = subparsers.addParser("create_product"); + createProductParser.addArgument("productId"); + createProductParser.addArgument("productDisplayName"); + createProductParser.addArgument("productCategory"); + createProductParser.addArgument("productDescription"); + createProductParser.addArgument("productLabels").nargs("?").setDefault(""); + + subparsers.addParser("list_products"); + + Subparser getProductParser = subparsers.addParser("get_product"); + getProductParser.addArgument("productId"); + + Subparser updateProductLabelsParser = subparsers.addParser("update_product_labels"); + updateProductLabelsParser.addArgument("productId"); + updateProductLabelsParser.addArgument("productLabels"); + + Subparser deleteProductParser = subparsers.addParser("delete_product"); + deleteProductParser.addArgument("productId"); + + String projectId = System.getenv("PROJECT_ID"); + String computeRegion = System.getenv("REGION_NAME"); + + Namespace ns = null; + try { + ns = parser.parseArgs(args); + if (ns.get("command").equals("create_product")) { + createProduct( + projectId, + computeRegion, + ns.getString("productId"), + ns.getString("productDisplayName"), + ns.getString("productCategory"), + ns.getString("productDescription"), + ns.getString("productLabels")); + } + if (ns.get("command").equals("list_products")) { + listProducts(projectId, computeRegion); + } + if (ns.get("command").equals("get_product")) { + getProduct(projectId, computeRegion, ns.getString("productId")); + } + if (ns.get("command").equals("update_product_labels")) { + updateProductLabels( + projectId, computeRegion, ns.getString("productId"), ns.getString("productLabels")); + } + if (ns.get("command").equals("delete_product")) { + deleteProduct(projectId, computeRegion, ns.getString("productId")); + } + + } catch (ArgumentParserException e) { + parser.handleError(e); + } + } +} 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 new file mode 100644 index 00000000000..91abb208331 --- /dev/null +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSearch.java @@ -0,0 +1,170 @@ +/* + * Copyright 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +// [START product_search_import] +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.ProductSearchParams; +import com.google.cloud.vision.v1p3beta1.ProductSearchResults.Result; +import com.google.cloud.vision.v1p3beta1.ProductSetName; +import com.google.protobuf.ByteString; + +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.List; + +import net.sourceforge.argparse4j.ArgumentParsers; +import net.sourceforge.argparse4j.inf.ArgumentParser; +import net.sourceforge.argparse4j.inf.ArgumentParserException; +import net.sourceforge.argparse4j.inf.Namespace; +import net.sourceforge.argparse4j.inf.Subparser; +import net.sourceforge.argparse4j.inf.Subparsers; +// [END product_search_import] + +/** + * This application demonstrates how to perform similar product search operation in Cloud Vision + * Product Search. + * + * For more information, see the tutorial page at + * https://cloud.google.com/vision/product-search/docs/ + */ + +public class ProductSearch { + + // [START product_search_get_similar_products_file] + /** + * Search similar products to image. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param productSetId - Id of the product set. + * @param productCategory - Category of the product. + * @param filePath - Local file path of the image to be searched + * @param filter - Condition to be applied on the labels. Example for filter: (color = red OR + * color = blue) AND style = kids It will search on all products with the following labels: + * color:red AND style:kids color:blue AND style:kids + * @throws IOException - on I/O errors. + */ + public static void getSimilarProducts( + String projectId, + String computeRegion, + String productSetId, + String productCategory, + 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 GCS link or HTTPS link or Raw image bytes. + // Example: + // To use GCS link replace with below code + // ImageSource source = ImageSource.newBuilder().setGcsImageUri(gcsUri).build(); + // Image image = Image.newBuilder().setSource(source).build(); + // 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 product_search_get_similar_products_file] + + public static void main(String[] args) throws Exception { + ProductSearch productSearch = new ProductSearch(); + productSearch.argsHelper(args, System.out); + } + + public static void argsHelper(String[] args, PrintStream out) throws Exception { + ArgumentParser parser = ArgumentParsers.newFor("Product Search").build(); + Subparsers subparsers = parser.addSubparsers().dest("command"); + + Subparser getSimilarProductsParser = subparsers.addParser("get_similar_products"); + getSimilarProductsParser.addArgument("productSetId"); + getSimilarProductsParser.addArgument("productCategory"); + getSimilarProductsParser.addArgument("filePath"); + getSimilarProductsParser.addArgument("filter").nargs("?").setDefault(""); + + String projectId = System.getenv("PROJECT_ID"); + String computeRegion = System.getenv("REGION_NAME"); + + Namespace ns = null; + try { + ns = parser.parseArgs(args); + if (ns.get("command").equals("get_similar_products")) { + getSimilarProducts( + projectId, + computeRegion, + ns.getString("productSetId"), + ns.getString("productCategory"), + ns.getString("filePath"), + ns.getString("filter")); + } + + } catch (ArgumentParserException e) { + parser.handleError(e); + } + } +} 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 new file mode 100644 index 00000000000..ea362c43c91 --- /dev/null +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java @@ -0,0 +1,291 @@ +/* + * Copyright 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +// [START product_search_import] +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 java.io.IOException; +import java.io.PrintStream; + +import net.sourceforge.argparse4j.ArgumentParsers; +import net.sourceforge.argparse4j.inf.ArgumentParser; +import net.sourceforge.argparse4j.inf.ArgumentParserException; +import net.sourceforge.argparse4j.inf.Namespace; +import net.sourceforge.argparse4j.inf.Subparser; +import net.sourceforge.argparse4j.inf.Subparsers; +// [END product_search_import] + +/** + * This application demonstrates how to perform basic operations on Product Sets. + * + * For more information, see the tutorial page at + * https://cloud.google.com/vision/product-search/docs/ + */ + +public class ProductSetManagement { + + // [START product_search_create_product_set] + /** + * Create a product set + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param productSetId - Id of the product set. + * @param productSetDisplayName - Display name of the product set. + * @throws IOException - on I/O errors. + */ + 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())); + } + // [END product_search_create_product_set] + + // [START product_search_list_product_sets] + /** + * List all product sets + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @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())); + } + } + // [END product_search_list_product_sets] + + // [START product_search_get_product_set] + /** + * Get info about the product set. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param productSetId - Id of the product set. + * @throws IOException - on I/O errors. + */ + 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())); + } + // [END product_search_get_product_set] + + // [START product_search_list_products_in_product_set] + /** + * List all products in a product set. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param productSetId - Id of the product set. + * @throws IOException - on I/O errors. + */ + public static void listProductsInSet(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())); + } + } + // [END product_search_list_products_in_product_set] + + // [START product_search_add_product_to_product_set] + /** + * Add a product to a product set. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param productId - Id of the product. + * @param productSetId - Id of the product set. + * @throws IOException - on I/O errors. + */ + public static void addProductToSet( + String projectId, String computeRegion, String productId, String productSetId) + throws IOException { + 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. + String productPath = ProductName.of(projectId, computeRegion, productId).toString(); + + // Add the product to the product set. + client.addProductToProductSet(productSetPath, productPath); + + System.out.println(String.format("Product added to product set.")); + } + // [END product_search_add_product_to_product_set] + + // [START product_search_delete_product_set] + /** + * Delete a product set. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @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")); + } + // [END product_search_delete_product_set] + + public static void main(String[] args) throws Exception { + ProductSetManagement productSetManagement = new ProductSetManagement(); + productSetManagement.argsHelper(args, System.out); + } + + public static void argsHelper(String[] args, PrintStream out) throws Exception { + ArgumentParser parser = ArgumentParsers.newFor("Product Set Management").build(); + Subparsers subparsers = parser.addSubparsers().dest("command"); + + Subparser createProductSetParser = subparsers.addParser("create_product_set"); + createProductSetParser.addArgument("productSetId"); + createProductSetParser.addArgument("productSetDisplayName"); + + Subparser addProductParser = subparsers.addParser("add_product_to_product_set"); + addProductParser.addArgument("productSetId"); + addProductParser.addArgument("productId"); + + subparsers.addParser("list_product_sets"); + + Subparser getProductSetParser = subparsers.addParser("get_product_set"); + getProductSetParser.addArgument("productSetId"); + + Subparser listProductsInProductSetParser = subparsers.addParser("list_products_in_product_set"); + listProductsInProductSetParser.addArgument("productSetId"); + + Subparser deleteProductSetParser = subparsers.addParser("delete_product_set"); + deleteProductSetParser.addArgument("productSetId"); + + String projectId = System.getenv("PROJECT_ID"); + String computeRegion = System.getenv("REGION_NAME"); + + Namespace ns = null; + try { + ns = parser.parseArgs(args); + if (ns.get("command").equals("create_product_set")) { + createProductSet( + projectId, + computeRegion, + ns.getString("productSetId"), + ns.getString("productSetDisplayName")); + } + if (ns.get("command").equals("list_product_sets")) { + listProductSets(projectId, computeRegion); + } + if (ns.get("command").equals("get_product_set")) { + getProductSet(projectId, computeRegion, ns.getString("productSetId")); + } + if (ns.get("command").equals("add_product_to_product_set")) { + addProductToSet( + projectId, computeRegion, ns.getString("productId"), ns.getString("productSetId")); + } + if (ns.get("command").equals("list_products_in_product_set")) { + listProductsInSet(projectId, computeRegion, ns.getString("productSetId")); + } + if (ns.get("command").equals("delete_product_set")) { + deleteProductSet(projectId, computeRegion, ns.getString("productSetId")); + } + + } catch (ArgumentParserException e) { + parser.handleError(e); + } + } +} 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 new file mode 100644 index 00000000000..11a6164e5ec --- /dev/null +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java @@ -0,0 +1,228 @@ +/* + * Copyright 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package com.example.vision; + +// [START product_search_import] +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 java.io.IOException; +import java.io.PrintStream; + +import net.sourceforge.argparse4j.ArgumentParsers; +import net.sourceforge.argparse4j.inf.ArgumentParser; +import net.sourceforge.argparse4j.inf.ArgumentParserException; +import net.sourceforge.argparse4j.inf.Namespace; +import net.sourceforge.argparse4j.inf.Subparser; +import net.sourceforge.argparse4j.inf.Subparsers; +// [END product_search_import] + +/** + * This application demonstrates how to perform basic operations on Reference Images in Cloud Vision + * Product Search. + * + * For more information, see the tutorial page at + * https://cloud.google.com/vision/product-search/docs/ + */ + +public class ReferenceImageManagement { + + // [START product_search_create_reference_image] + /** + * Create a reference image. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param productId - Id of the product. + * @param referenceImageId - Id of the image. + * @param gcsUri - Google Cloud Storage path of the input image. + * @throws IOException - on I/O errors. + */ + public static void createReferenceImage( + String projectId, + String computeRegion, + String productId, + String referenceImageId, + String gcsUri) + throws IOException { + ProductSearchClient client = ProductSearchClient.create(); + + // Get the full path of the product. + ProductName productPath = ProductName.of(projectId, computeRegion, productId); + + // Create a reference image. + ReferenceImage referenceImage = ReferenceImage.newBuilder().setUri(gcsUri).build(); + ReferenceImage image = + client.createReferenceImage(productPath, 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 product_search_create_reference_image] + + // [START product_search_list_reference_images] + /** + * List all images in a product. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param productId - Id of the product. + * @throws IOException - on I/O errors. + */ + 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())); + } + } + // [END product_search_list_reference_images] + + // [START product_search_get_reference_image] + /** + * Get info about a reference image. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param productId - Id of the product. + * @param referenceImageId - Id of the image. + * @throws IOException - on I/O errors. + */ + 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())); + } + // [END product_search_get_reference_image] + + // [START product_search_delete_reference_image] + /** + * Delete a reference image. + * + * @param projectId - Id of the project. + * @param computeRegion - Region name. + * @param productId - Id of the product. + * @param referenceImageId - Id of the image. + * @throws IOException - on I/O errors. + */ + 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."); + } + // [END product_search_delete_reference_image] + + public static void main(String[] args) throws Exception { + ReferenceImageManagement referenceImageManagement = new ReferenceImageManagement(); + referenceImageManagement.argsHelper(args, System.out); + } + + public static void argsHelper(String[] args, PrintStream out) throws Exception { + ArgumentParser parser = ArgumentParsers.newFor("Reference Image Management").build(); + Subparsers subparsers = parser.addSubparsers().dest("command"); + + Subparser createReferenceImageParser = subparsers.addParser("create_reference_image"); + createReferenceImageParser.addArgument("productId"); + createReferenceImageParser.addArgument("referenceImageId"); + createReferenceImageParser.addArgument("gcsUri"); + + Subparser listReferenceImagesOfProductParser = + subparsers.addParser("list_reference_images_of_product"); + listReferenceImagesOfProductParser.addArgument("productId"); + + Subparser getReferenceImageParser = subparsers.addParser("get_reference_image"); + getReferenceImageParser.addArgument("productId"); + getReferenceImageParser.addArgument("referenceImageId"); + + Subparser deleteReferenceImageParser = subparsers.addParser("delete_reference_image"); + deleteReferenceImageParser.addArgument("productId"); + deleteReferenceImageParser.addArgument("referenceImageId"); + + String projectId = System.getenv("PROJECT_ID"); + String computeRegion = System.getenv("REGION_NAME"); + + Namespace ns = null; + try { + ns = parser.parseArgs(args); + if (ns.get("command").equals("create_reference_image")) { + createReferenceImage( + projectId, + computeRegion, + ns.getString("productId"), + ns.getString("referenceImageId"), + ns.getString("gcsUri")); + } + if (ns.get("command").equals("list_reference_images_of_product")) { + listReferenceImagesOfProduct(projectId, computeRegion, ns.getString("productId")); + } + if (ns.get("command").equals("get_reference_image")) { + getReferenceImage( + projectId, computeRegion, ns.getString("productId"), ns.getString("referenceImageId")); + } + if (ns.get("command").equals("delete_reference_image")) { + deleteReferenceImage( + projectId, computeRegion, ns.getString("productId"), ns.getString("referenceImageId")); + } + + } catch (ArgumentParserException e) { + parser.handleError(e); + } + } +}