Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vision Product Search - GA #1257

Merged
merged 13 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions vision/product-search/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-vision</artifactId>
<version>1.37.1</version>
<version>1.52.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.37.1</version>
<version>1.52.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.argparse4j</groupId>
Expand Down
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 @@ -60,7 +57,7 @@ public static void importProductSets(String projectId, String computeRegion, Str
ProductSearchClient client = ProductSearchClient.create();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm noticing this is leftover from the beta.
Should we be using the autoclosing methods for the client.

try (ProductSearchClient client = ProductSearchClient.create()) {
   // All the code
   // No need to call client.close() now. 
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think that makes sense. Will switch it out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't sure if ProductSearch is doing stuff a little differently or not. :)


// A resource that represents Google Cloud Platform location.
LocationName projectLocation = LocationName.of(projectId, computeRegion);
String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion);
Builder gcsSource = ImportProductSetsGcsSource.newBuilder().setCsvFileUri(gcsUri);

// Set the input configuration along with Google Cloud Storage URI
Expand All @@ -69,7 +66,7 @@ public static void importProductSets(String projectId, String computeRegion, Str

// Import the product sets from the input URI.
OperationFuture<ImportProductSetsResponse, BatchOperationMetadata> response =
client.importProductSetsAsync(projectLocation, inputConfig);
client.importProductSetsAsync(formattedParent, inputConfig);

System.out.println(String.format("Processing operation name: %s", response.getName()));
ImportProductSetsResponse results = response.get();
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 @@ -59,13 +54,14 @@ public static void addProductToProductSet(
ProductSearchClient client = ProductSearchClient.create();

// Get the full path of the product set.
ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId);
String formattedName =
ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId);

// Get the full path of the product.
String productPath = ProductName.of(projectId, computeRegion, productId).toString();

// Add the product to the product set.
client.addProductToProductSet(productSetPath, productPath);
client.addProductToProductSet(formattedName, productPath);

System.out.println(String.format("Product added to product set."));
}
Expand All @@ -85,11 +81,10 @@ public static void listProductsInProductSet(
ProductSearchClient client = ProductSearchClient.create();

// Get the full path of the product set.
ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId);

String formattedName =
ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId);
// List all the products available in the product set.
for (Product product :
client.listProductsInProductSet(productSetPath.toString()).iterateAll()) {
for (Product product : client.listProductsInProductSet(formattedName).iterateAll()) {
// Display the product information
System.out.println(String.format("Product name: %s", product.getName()));
System.out.println(
Expand Down Expand Up @@ -121,13 +116,15 @@ public static void removeProductFromProductSet(
ProductSearchClient client = ProductSearchClient.create();

// Get the full path of the product set.
ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId);
String formattedParent =
ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId);

// Get the full path of the product.
String productPath = ProductName.of(projectId, computeRegion, productId).toString();
String formattedName =
ProductSearchClient.formatProductName(projectId, computeRegion, productId);

// Remove the product from the product set.
client.removeProductFromProductSet(productSetPath, productPath);
client.removeProductFromProductSet(formattedParent, formattedName);

System.out.println(String.format("Product removed from product set."));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,8 +60,7 @@ public static void createProduct(
ProductSearchClient client = ProductSearchClient.create();

// A resource that represents Google Cloud Platform location.
LocationName projectLocation = LocationName.of(projectId, computeRegion);

String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion);
// Create a product with the product specification in the region.
// Multiple labels are also supported.
Product myProduct =
Expand All @@ -72,8 +69,8 @@ public static void createProduct(
.setDisplayName(productDisplayName)
.setProductCategory(productCategory)
.build();
Product product = client.createProduct(projectLocation.toString(), myProduct, productId);

Product product = client.createProduct(formattedParent, myProduct, productId);
client.close();
// Display the product information
System.out.println(String.format("Product name: %s", product.getName()));
}
Expand All @@ -91,22 +88,23 @@ public static void listProducts(String projectId, String computeRegion) throws I
ProductSearchClient client = ProductSearchClient.create();

// A resource that represents Google Cloud Platform location.
LocationName projectLocation = LocationName.of(projectId, computeRegion);
String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion);

// List all the products available in the region.
for (Product product : client.listProducts(projectLocation).iterateAll()) {
for (Product product : client.listProducts(formattedParent).iterateAll()) {
// Display the product information
System.out.println(String.format("\nProduct name: %s", product.getName()));
System.out.println(
String.format(
"Product id: %s",
product.getName().substring(product.getName().lastIndexOf('/') + 1)));
"Product id: %s",
product.getName().substring(product.getName().lastIndexOf('/') + 1)));
System.out.println(String.format("Product display name: %s", product.getDisplayName()));
System.out.println(String.format("Product category: %s", product.getProductCategory()));
System.out.println("Product labels:");
System.out.println(
String.format("Product labels: %s", product.getProductLabelsList().toString()));
}
client.close();
}
// [END vision_product_search_list_products]

Expand All @@ -124,11 +122,11 @@ public static void getProduct(String projectId, String computeRegion, String pro
ProductSearchClient client = ProductSearchClient.create();

// Get the full path of the product.
ProductName productPath = ProductName.of(projectId, computeRegion, productId);

String formattedName =
ProductSearchClient.formatProductName(projectId, computeRegion,productId);
// Get complete detail of the product.
Product product = client.getProduct(productPath.toString());

Product product = client.getProduct(formattedName);
client.close();
// Display the product information
System.out.println(String.format("Product name: %s", product.getName()));
System.out.println(
Expand All @@ -137,9 +135,10 @@ public static void getProduct(String projectId, String computeRegion, String pro
System.out.println(String.format("Product display name: %s", product.getDisplayName()));
System.out.println(String.format("Product description: %s", product.getDescription()));
System.out.println(String.format("Product category: %s", product.getProductCategory()));
System.out.println("Product labels:");
System.out.println(
String.format("Product labels: %s", product.getProductLabelsList().toString()));
System.out.println(String.format("Product labels: "));
for (Product.KeyValue element : product.getProductLabelsList()) {
System.out.println(String.format("%s: %s", element.getKey(), element.getValue()));
}
}
// [END vision_product_search_get_product]

Expand All @@ -159,13 +158,14 @@ public static void updateProductLabels(
ProductSearchClient client = ProductSearchClient.create();

// Get the full path of the product.
String productPath = ProductName.of(projectId, computeRegion, productId).toString();
String formattedName =
ProductSearchClient.formatProductName(projectId, computeRegion, productId);

// Set product name, product labels and product display name.
// Multiple labels are also supported.
Product product =
Product.newBuilder()
.setName(productPath)
.setName(formattedName)
.addProductLabels(
KeyValue.newBuilder()
.setKey(productLabels.split(",")[0].split("=")[0])
Expand All @@ -178,7 +178,7 @@ public static void updateProductLabels(

// Update the product.
Product updatedProduct = client.updateProduct(product, updateMask);

client.close();
// Display the product information
System.out.println(String.format("Product name: %s", updatedProduct.getName()));
System.out.println(String.format("Updated product labels: "));
Expand All @@ -202,11 +202,12 @@ public static void deleteProduct(String projectId, String computeRegion, String
ProductSearchClient client = ProductSearchClient.create();

// Get the full path of the product.
ProductName productPath = ProductName.of(projectId, computeRegion, productId);
String formattedName =
ProductSearchClient.formatProductName(projectId, computeRegion, productId);

// Delete a product.
client.deleteProduct(productPath);

client.deleteProduct(formattedName);
client.close();
System.out.println("Product deleted.");
}
// [END vision_product_search_delete_product]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,7 +79,8 @@ public static void getSimilarProductsFile(
ImageAnnotatorClient queryImageClient = ImageAnnotatorClient.create();

// Get the full path of the product set.
String productSetPath = ProductSetName.of(projectId, computeRegion, productSetId).toString();
String productSetPath = ProductSearchClient
.formatProductSetName(projectId, computeRegion, productSetId);

// Read the image as a stream of bytes.
File imgPath = new File(filePath);
Expand Down Expand Up @@ -114,7 +116,7 @@ public static void getSimilarProductsFile(

List<Result> similarProducts =
response.getResponses(0).getProductSearchResults().getResultsList();

queryImageClient.close();
System.out.println("Similar Products: ");
for (Result product : similarProducts) {
System.out.println(String.format("\nProduct name: %s", product.getProduct().getName()));
Expand Down Expand Up @@ -181,7 +183,7 @@ public static void getSimilarProductsGcs(String projectId,

List<Result> similarProducts =
response.getResponses(0).getProductSearchResults().getResultsList();

queryImageClient.close();
System.out.println("Similar Products: ");
for (Result product : similarProducts) {
System.out.println(String.format("\nProduct name: %s", product.getProduct().getName()));
Expand Down
Loading