Search in sources :

Example 36 with Product

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.");
            }
        }
    }
}
Also used : ImportProductSetsResponse(com.google.cloud.vision.v1.ImportProductSetsResponse) ProductSearchClient(com.google.cloud.vision.v1.ProductSearchClient) Builder(com.google.cloud.vision.v1.ImportProductSetsGcsSource.Builder) BatchOperationMetadata(com.google.cloud.vision.v1.BatchOperationMetadata) ReferenceImage(com.google.cloud.vision.v1.ReferenceImage) ImportProductSetsInputConfig(com.google.cloud.vision.v1.ImportProductSetsInputConfig)

Example 37 with Product

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()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListProductsInProductSetPagedResponse(com.google.cloud.vision.v1.ProductSearchClient.ListProductsInProductSetPagedResponse) Test(org.junit.Test)

Example 38 with Product

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()));
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ByteString(com.google.protobuf.ByteString) Image(com.google.cloud.vision.v1.Image) File(java.io.File) Feature(com.google.cloud.vision.v1.Feature) ImageContext(com.google.cloud.vision.v1.ImageContext) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) Result(com.google.cloud.vision.v1.ProductSearchResults.Result)

Example 39 with Product

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.");
    }
}
Also used : ProductSearchClient(com.google.cloud.vision.v1.ProductSearchClient)

Example 40 with Product

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"));
    }
}
Also used : ProductSearchClient(com.google.cloud.vision.v1.ProductSearchClient)

Aggregations

ProductSearchClient (com.google.cloud.vision.v1.ProductSearchClient)19 Product (com.google.cloud.retail.v2.Product)14 Product (com.google.cloud.vision.v1.Product)10 Test (org.junit.Test)9 SetupCleanup.deleteProduct (setup.SetupCleanup.deleteProduct)6 ReferenceImage (com.google.cloud.vision.v1.ReferenceImage)5 AbstractMessage (com.google.protobuf.AbstractMessage)5 ProductSet (com.google.cloud.vision.v1.ProductSet)4 Product (com.haulmont.cuba.testmodel.sales_1.Product)4 Test (org.junit.jupiter.api.Test)4 NotFoundException (com.google.api.gax.rpc.NotFoundException)3 CreateProductRequest (com.google.cloud.retail.v2.CreateProductRequest)3 GetProductRequest (com.google.cloud.retail.v2.GetProductRequest)3 QueryRunner (com.haulmont.bali.db.QueryRunner)3 Id (com.haulmont.cuba.core.entity.contracts.Id)3 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)3 TestContainer (com.haulmont.cuba.testsupport.TestContainer)3 Assertions (org.junit.jupiter.api.Assertions)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)3