use of com.google.cloud.vision.v1.Product in project java-vision by googleapis.
the class ImportProductSets method importProductSets.
// [START vision_product_search_import_product_images]
/**
* 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 {
try (ProductSearchClient client = ProductSearchClient.create()) {
// 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();
// 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:");
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.");
}
}
}
}
use of com.google.cloud.vision.v1.Product in project java-vision by googleapis.
the class ProductSearchClientTest method listProductsInProductSetTest2.
@Test
public void listProductsInProductSetTest2() throws Exception {
Product responsesElement = Product.newBuilder().build();
ListProductsInProductSetResponse expectedResponse = ListProductsInProductSetResponse.newBuilder().setNextPageToken("").addAllProducts(Arrays.asList(responsesElement)).build();
mockProductSearch.addResponse(expectedResponse);
String name = "name3373707";
ListProductsInProductSetPagedResponse pagedListResponse = client.listProductsInProductSet(name);
List<Product> resources = Lists.newArrayList(pagedListResponse.iterateAll());
Assert.assertEquals(1, resources.size());
Assert.assertEquals(expectedResponse.getProductsList().get(0), resources.get(0));
List<AbstractMessage> actualRequests = mockProductSearch.getRequests();
Assert.assertEquals(1, actualRequests.size());
ListProductsInProductSetRequest actualRequest = ((ListProductsInProductSetRequest) actualRequests.get(0));
Assert.assertEquals(name, actualRequest.getName());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.cloud.vision.v1.Product in project java-vision by googleapis.
the class ProductSearch method getSimilarProductsFile.
// [START vision_product_search_get_similar_products]
/**
* Search similar products to image in local file.
*
* @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 getSimilarProductsFile(String projectId, String computeRegion, String productSetId, String productCategory, String filePath, String filter) throws IOException {
try (ImageAnnotatorClient queryImageClient = ImageAnnotatorClient.create()) {
// Get the full path of the product set.
String productSetPath = ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId);
// 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 HTTPS link or Raw image bytes.
// Example:
// 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<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()));
}
}
}
use of com.google.cloud.vision.v1.Product in project java-vision by googleapis.
the class ProductManagement method deleteProduct.
// [END vision_product_search_update_product_labels]
// [START vision_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 {
try (ProductSearchClient client = ProductSearchClient.create()) {
// Get the full path of the product.
String formattedName = ProductSearchClient.formatProductName(projectId, computeRegion, productId);
// Delete a product.
client.deleteProduct(formattedName);
System.out.println("Product deleted.");
}
}
use of com.google.cloud.vision.v1.Product in project java-vision by googleapis.
the class ProductSetManagement method deleteProductSet.
// [END vision_product_search_get_product_set]
// [START vision_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 {
try (ProductSearchClient client = ProductSearchClient.create()) {
// Get the full path of the product set.
String formattedName = ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId);
// Delete the product set.
client.deleteProductSet(formattedName);
System.out.println(String.format("Product set deleted"));
}
}
Aggregations