Skip to content

Commit

Permalink
samples: Vision region tag update (#1182)
Browse files Browse the repository at this point in the history
* updates region tags for detecting-crop-hints page

* updates region tags for detecting-faces page

* updates region tags for detecting-fulltext page

* updates region tags for detecting-labels page

* updates region tags for detecting-landmarks page

* update region tags for detect-logos page

* update region tags for detecting-properties page

* update region tags for detecting-safe-search page

* update region tags for detecting-text page

* update region tags for detecting-web page

* update beta tags to standard

* update PDF detection region tags to standard

* updates product search region tags to standard

* fix label detection tag

* remove region tags from imports

* updates to mirror python files

* Fix indentation
  • Loading branch information
alixhami authored and chingor13 committed Aug 13, 2020
1 parent 6e92e60 commit 3ab6fff
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.example.vision;

// [START product_search_import]
// [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;
Expand All @@ -26,7 +26,7 @@
import com.google.cloud.vision.v1p3beta1.LocationName;
import com.google.cloud.vision.v1p3beta1.ProductSearchClient;
import com.google.cloud.vision.v1p3beta1.ReferenceImage;

// [END vision_product_search_tutorial_import]
import java.io.PrintStream;
import javax.swing.JPanel;

Expand All @@ -36,7 +36,6 @@
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
Expand All @@ -47,7 +46,7 @@
*/

public class ImportProductSets extends JPanel {
// [START product_search_import_product_sets]
// [START vision_product_search_import_product_images]
/**
* Import images of different products in the product set.
*
Expand Down Expand Up @@ -89,7 +88,7 @@ public static void importProductSets(String projectId, String computeRegion, Str
}
}
}
// [END product_search_import_product_sets]
// [END vision_product_search_import_product_images]

public static void main(String[] args) throws Exception {
ImportProductSets importProductSet = new ImportProductSets();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@

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;
Expand All @@ -35,7 +33,6 @@
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.
Expand All @@ -46,65 +43,17 @@

public class ProductInProductSetManagement {

// [START product_search_add_product_to_product_set]
// [START vision_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.
* 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 removeProductFromProductSet(
public static void addProductToSet(
String projectId, String computeRegion, String productId, String productSetId)
throws IOException {
ProductSearchClient client = ProductSearchClient.create();
Expand All @@ -115,14 +64,14 @@ public static void removeProductFromProductSet(
// 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);
// Add the product to the product set.
client.addProductToProductSet(productSetPath, productPath);

System.out.println(String.format("Product removed from product set."));
System.out.println(String.format("Product added to product set."));
}
// [END product_search_remove_product_from_product_set]
// [END vision_product_search_add_product_to_product_set]

// [START product_search_list_products_in_product_set]
// [START vision_product_search_list_products_in_product_set]
/**
* List all products in a product set.
*
Expand Down Expand Up @@ -154,38 +103,35 @@ public static void listProductsInProductSet(
String.format("Product labels: %s\n", product.getProductLabelsList().toString()));
}
}
// [END product_search_list_products_in_product_set]
// [END vision_product_search_list_products_in_product_set]

// [START product_search_list_products]
// [START vision_product_search_remove_product_from_product_set]
/**
* List all products.
* 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 listProducts(String projectId, String computeRegion) throws IOException {
public static void removeProductFromProductSet(
String projectId, String computeRegion, String productId, String productSetId)
throws IOException {
ProductSearchClient client = ProductSearchClient.create();

// A resource that represents Google Cloud Platform location.
LocationName projectLocation = LocationName.of(projectId, computeRegion);
// 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();

// 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()));
}
// Remove the product from the product set.
client.removeProductFromProductSet(productSetPath, productPath);

System.out.println(String.format("Product removed from product set."));
}
// [END product_search_list_products]
// [END vision_product_search_remove_product_from_product_set]

public static void main(String[] args) throws Exception {
ProductInProductSetManagement productInProductSetManagement =
Expand All @@ -196,15 +142,14 @@ public static void main(String[] args) throws Exception {
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 addProductParser = subparsers.addParser("add_product_to_product_set");
addProductParser.addArgument("productSetId");
addProductParser.addArgument("productId");

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");
Expand All @@ -216,23 +161,17 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception {
Namespace ns = null;
try {
ns = parser.parseArgs(args);
if (ns.get("command").equals("list_products")) {
listProducts(projectId, computeRegion);
if (ns.get("command").equals("add_product_to_product_set")) {
addProductToSet(
projectId, computeRegion, ns.getString("productId"), ns.getString("productSetId"));
}
if (ns.get("command").equals("update_product_set")) {
addProductToProductSet(
projectId,
computeRegion,
ns.getString("productSetId"),
ns.getString("productSetDisplayName"));
if (ns.get("command").equals("list_products_in_product_set")) {
listProductsInProductSet(projectId, computeRegion, ns.getString("productSetId"));
}
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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;
Expand All @@ -33,7 +32,6 @@
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.
Expand All @@ -44,7 +42,7 @@

public class ProductManagement {

// [START product_search_create_product]
// [START vision_product_search_create_product]
/**
* Create one product.
*
Expand Down Expand Up @@ -90,9 +88,9 @@ public static void createProduct(
// Display the product information
System.out.println(String.format("Product name: %s", product.getName()));
}
// [END product_search_create_product]
// [END vision_product_search_create_product]

// [START product_search_list_products]
// [START vision_product_search_list_products]
/**
* List all products.
*
Expand Down Expand Up @@ -121,9 +119,9 @@ public static void listProducts(String projectId, String computeRegion) throws I
String.format("Product labels: %s", product.getProductLabelsList().toString()));
}
}
// [END product_search_list_products]
// [END vision_product_search_list_products]

// [START product_search_get_product]
// [START vision_product_search_get_product]
/**
* Get information about a product.
*
Expand Down Expand Up @@ -154,9 +152,9 @@ public static void getProduct(String projectId, String computeRegion, String pro
System.out.println(
String.format("Product labels: %s", product.getProductLabelsList().toString()));
}
// [END product_search_get_product]
// [END vision_product_search_get_product]

// [START product_search_update_product_labels]
// [START vision_product_search_update_product_labels]
/**
* Update the product labels.
*
Expand Down Expand Up @@ -198,9 +196,9 @@ public static void updateProductLabels(
String.format(
"Updated product labels: %s", updatedProduct.getProductLabelsList().toString()));
}
// [END product_search_update_product_labels]
// [END vision_product_search_update_product_labels]

// [START product_search_delete_product]
// [START vision_product_search_delete_product]
/**
* Delete the product and all its reference images.
*
Expand All @@ -221,7 +219,7 @@ public static void deleteProduct(String projectId, String computeRegion, String

System.out.println("Product deleted.");
}
// [END product_search_delete_product]
// [END vision_product_search_delete_product]

public static void main(String[] args) throws Exception {
ProductManagement productManagement = new ProductManagement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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;
Expand All @@ -42,7 +41,6 @@
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
Expand All @@ -54,7 +52,7 @@

public class ProductSearch {

// [START product_search_get_similar_products_file]
// [START vision_product_search_get_similar_products]
/**
* Search similar products to image.
*
Expand Down Expand Up @@ -130,7 +128,7 @@ public static void getSimilarProducts(
System.out.println(String.format("Image name: %s", product.getImage()));
}
}
// [END product_search_get_similar_products_file]
// [END vision_product_search_get_similar_products]

public static void main(String[] args) throws Exception {
ProductSearch productSearch = new ProductSearch();
Expand Down
Loading

0 comments on commit 3ab6fff

Please sign in to comment.