use of com.google.cloud.vision.v1.Product in project java-vision by googleapis.
the class ReferenceImageManagement method listReferenceImagesOfProduct.
// [END vision_product_search_create_reference_image]
// [START vision_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 {
try (ProductSearchClient client = ProductSearchClient.create()) {
// Get the full path of the product.
String formattedParent = ProductSearchClient.formatProductName(projectId, computeRegion, productId);
for (ReferenceImage image : client.listReferenceImages(formattedParent).iterateAll()) {
// Display the reference image information.
System.out.println(String.format("Reference image name: %s", image.getName()));
System.out.println(String.format("Reference image id: %s", image.getName().substring(image.getName().lastIndexOf('/') + 1)));
System.out.println(String.format("Reference image uri: %s", image.getUri()));
System.out.println(String.format("Reference image bounding polygons: %s \n", image.getBoundingPolysList().toString()));
}
}
}
use of com.google.cloud.vision.v1.Product in project java-vision by googleapis.
the class ProductInProductSetManagement method addProductToProductSet.
// [START vision_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 addProductToProductSet(String projectId, String computeRegion, String productId, String productSetId) throws IOException {
try (ProductSearchClient client = ProductSearchClient.create()) {
// 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();
// Add the product to the product set.
client.addProductToProductSet(formattedName, productPath);
System.out.println(String.format("Product added to product set."));
}
}
use of com.google.cloud.vision.v1.Product in project java-vision by googleapis.
the class ProductInProductSetManagement method removeProductFromProductSet.
// [END vision_product_search_list_products_in_product_set]
// [START vision_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 {
try (ProductSearchClient client = ProductSearchClient.create()) {
// Get the full path of the product set.
String formattedParent = ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId);
// Get the full path of the product.
String formattedName = ProductSearchClient.formatProductName(projectId, computeRegion, productId);
// Remove the product from the product set.
client.removeProductFromProductSet(formattedParent, formattedName);
System.out.println(String.format("Product removed from product set."));
}
}
use of com.google.cloud.vision.v1.Product in project java-vision by googleapis.
the class ProductInProductSetManagement method listProductsInProductSet.
// [END vision_product_search_add_product_to_product_set]
// [START vision_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 {
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()));
}
}
}
}
use of com.google.cloud.vision.v1.Product in project java-vision by googleapis.
the class ProductSearch method getSimilarProductsGcs.
// [END vision_product_search_get_similar_products]
// [START vision_product_search_get_similar_products_gcs]
/**
* Search similar products to image in Google Cloud Storage.
*
* @param projectId - Id of the project.
* @param computeRegion - Region name.
* @param productSetId - Id of the product set.
* @param productCategory - Category of the product.
* @param gcsUri - GCS 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 Exception - on errors.
*/
public static void getSimilarProductsGcs(String projectId, String computeRegion, String productSetId, String productCategory, String gcsUri, String filter) throws Exception {
try (ImageAnnotatorClient queryImageClient = ImageAnnotatorClient.create()) {
// Get the full path of the product set.
String productSetPath = ProductSetName.of(projectId, computeRegion, productSetId).toString();
// Get the image from Google Cloud Storage
ImageSource source = ImageSource.newBuilder().setGcsImageUri(gcsUri).build();
// Create annotate image request along with product search feature.
Feature featuresElement = Feature.newBuilder().setType(Type.PRODUCT_SEARCH).build();
Image image = Image.newBuilder().setSource(source).build();
ImageContext imageContext = ImageContext.newBuilder().setProductSearchParams(ProductSearchParams.newBuilder().setProductSet(productSetPath).addProductCategories(productCategory).setFilter(filter)).build();
AnnotateImageRequest annotateImageRequest = AnnotateImageRequest.newBuilder().addFeatures(featuresElement).setImage(image).setImageContext(imageContext).build();
List<AnnotateImageRequest> requests = Arrays.asList(annotateImageRequest);
// Search products similar to the image.
BatchAnnotateImagesResponse response = queryImageClient.batchAnnotateImages(requests);
List<Result> 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()));
}
}
}
Aggregations