Skip to content

Commit

Permalink
samples: Vision Product Search - GA (#1257)
Browse files Browse the repository at this point in the history
* Vision Product Search - GA

* Vision Product Search - GA

* Vision Product Search - GA

* Vision Product Search - GA

* fixing failing tests

* fixing failing tests

* fixing code review comments

* product label printing fix
  • Loading branch information
nirupa-kumar authored and chingor13 committed Aug 13, 2020
1 parent f1f8c14 commit 7cfeb1a
Show file tree
Hide file tree
Showing 12 changed files with 423 additions and 423 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
* <p>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]
/**
Expand All @@ -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<ImportProductSetsResponse, BatchOperationMetadata> response =
client.importProductSetsAsync(projectLocation, inputConfig);
// Import the product sets from the input URI.
OperationFuture<ImportProductSetsResponse, BatchOperationMetadata> 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.");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
* <p>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]
Expand All @@ -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]

Expand All @@ -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]
Expand All @@ -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]

Expand Down
Loading

0 comments on commit 7cfeb1a

Please sign in to comment.