Search in sources :

Example 1 with ColorInfo

use of com.google.cloud.retail.v2.ColorInfo in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectProperties.

/**
 * Detects image properties such as color frequency from the specified local image.
 *
 * @param filePath The path to the file to detect properties.
 * @param out A {@link PrintStream} to write
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectProperties(String filePath, PrintStream out) throws Exception, IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
    Image img = Image.newBuilder().setContent(imgBytes).build();
    Feature feat = Feature.newBuilder().setType(Type.IMAGE_PROPERTIES).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                out.printf("Error: %s\n", res.getError().getMessage());
                return;
            }
            // For full list of available annotations, see http://g.co/cloud/vision/docs
            DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors();
            for (ColorInfo color : colors.getColorsList()) {
                out.printf("fraction: %f\nr: %f, g: %f, b: %f\n", color.getPixelFraction(), color.getColor().getRed(), color.getColor().getGreen(), color.getColor().getBlue());
            }
        }
    }
}
Also used : ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) FileInputStream(java.io.FileInputStream) ColorInfo(com.google.cloud.vision.v1.ColorInfo) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) DominantColorsAnnotation(com.google.cloud.vision.v1.DominantColorsAnnotation) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 2 with ColorInfo

use of com.google.cloud.retail.v2.ColorInfo in project java-vision by googleapis.

the class DetectProperties method detectProperties.

// Detects image properties such as color frequency from the specified local image.
public static void detectProperties(String filePath) throws IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
    Image img = Image.newBuilder().setContent(imgBytes).build();
    Feature feat = Feature.newBuilder().setType(Feature.Type.IMAGE_PROPERTIES).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                System.out.format("Error: %s%n", res.getError().getMessage());
                return;
            }
            // For full list of available annotations, see http://g.co/cloud/vision/docs
            DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors();
            for (ColorInfo color : colors.getColorsList()) {
                System.out.format("fraction: %f%nr: %f, g: %f, b: %f%n", color.getPixelFraction(), color.getColor().getRed(), color.getColor().getGreen(), color.getColor().getBlue());
            }
        }
    }
}
Also used : ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) FileInputStream(java.io.FileInputStream) ColorInfo(com.google.cloud.vision.v1.ColorInfo) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) DominantColorsAnnotation(com.google.cloud.vision.v1.DominantColorsAnnotation) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 3 with ColorInfo

use of com.google.cloud.retail.v2.ColorInfo in project java-vision by googleapis.

the class ITSystemTest method detectPropertiesTest.

@Test
public void detectPropertiesTest() throws IOException {
    List<AnnotateImageResponse> responses = getResponsesList("landmark.jpg", Type.IMAGE_PROPERTIES, false);
    List<Float> actual = new ArrayList<>();
    for (AnnotateImageResponse res : responses) {
        DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors();
        for (ColorInfo color : colors.getColorsList()) {
            actual.add(color.getPixelFraction());
        }
    }
    assertThat(actual).contains((float) 0.14140345);
}
Also used : AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) DominantColorsAnnotation(com.google.cloud.vision.v1.DominantColorsAnnotation) ColorInfo(com.google.cloud.vision.v1.ColorInfo) Test(org.junit.Test)

Example 4 with ColorInfo

use of com.google.cloud.retail.v2.ColorInfo in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectPropertiesGcs.

/**
 * Detects image properties such as color frequency from the specified remote image on Google
 * Cloud Storage.
 *
 * @param gcsPath The path to the remote file on Google Cloud Storage to detect properties on.
 * @param out A {@link PrintStream} to write
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectPropertiesGcs(String gcsPath, PrintStream out) throws Exception, IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
    Image img = Image.newBuilder().setSource(imgSource).build();
    Feature feat = Feature.newBuilder().setType(Type.IMAGE_PROPERTIES).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                out.printf("Error: %s\n", res.getError().getMessage());
                return;
            }
            // For full list of available annotations, see http://g.co/cloud/vision/docs
            DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors();
            for (ColorInfo color : colors.getColorsList()) {
                out.printf("fraction: %f\nr: %f, g: %f, b: %f\n", color.getPixelFraction(), color.getColor().getRed(), color.getColor().getGreen(), color.getColor().getBlue());
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) DominantColorsAnnotation(com.google.cloud.vision.v1.DominantColorsAnnotation) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) ColorInfo(com.google.cloud.vision.v1.ColorInfo) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 5 with ColorInfo

use of com.google.cloud.retail.v2.ColorInfo in project java-retail by googleapis.

the class ImportProductsInlineSource method getProducts.

public static List<Product> getProducts() {
    List<Product> products = new ArrayList<>();
    Product product1;
    Product product2;
    float price1 = 16f;
    float originalPrice1 = 45.0f;
    float cost1 = 12.0f;
    PriceInfo priceInfo1 = PriceInfo.newBuilder().setPrice(price1).setOriginalPrice(originalPrice1).setCost(cost1).setCurrencyCode("USD").build();
    ColorInfo colorInfo1 = ColorInfo.newBuilder().addColorFamilies("Blue").addAllColors(Arrays.asList("Light blue", "Blue", "Dark blue")).build();
    FulfillmentInfo fulfillmentInfo1 = FulfillmentInfo.newBuilder().setType("pickup-in-store").addAllPlaceIds(Arrays.asList("store1", "store2")).build();
    FieldMask fieldMask1 = FieldMask.newBuilder().addAllPaths(Arrays.asList("title", "categories", "price_info", "color_info")).build();
    // TO CHECK ERROR HANDLING COMMENT OUT THE PRODUCT TITLE HERE:
    product1 = Product.newBuilder().setTitle("#IamRemarkable Pen").setId(UUID.randomUUID().toString()).addAllCategories(Collections.singletonList("Office")).setUri("https://shop.googlemerchandisestore.com/Google+Redesign/" + "Office/IamRemarkable+Pen").addBrands("#IamRemarkable").setPriceInfo(priceInfo1).setColorInfo(colorInfo1).addFulfillmentInfo(fulfillmentInfo1).setRetrievableFields(fieldMask1).build();
    float price2 = 35f;
    float originalPrice2 = 45.0f;
    float cost2 = 12.0f;
    PriceInfo priceInfo2 = PriceInfo.newBuilder().setPrice(price2).setOriginalPrice(originalPrice2).setCost(cost2).setCurrencyCode("USD").build();
    ColorInfo colorInfo2 = ColorInfo.newBuilder().addColorFamilies("Blue").addAllColors(Collections.singletonList("Sky blue")).build();
    FulfillmentInfo fulfillmentInfo2 = FulfillmentInfo.newBuilder().setType("pickup-in-store").addAllPlaceIds(Arrays.asList("store2", "store3")).build();
    FieldMask fieldMask2 = FieldMask.newBuilder().addAllPaths(Arrays.asList("title", "categories", "price_info", "color_info")).build();
    product2 = Product.newBuilder().setTitle("Android Embroidered Crewneck Sweater").setId(UUID.randomUUID().toString()).addCategories("Apparel").setUri("https://shop.googlemerchandisestore.com/Google+Redesign/" + "Apparel/Android+Embroidered+Crewneck+Sweater").addBrands("Android").setPriceInfo(priceInfo2).setColorInfo(colorInfo2).addFulfillmentInfo(fulfillmentInfo2).setRetrievableFields(fieldMask2).build();
    products.add(product1);
    products.add(product2);
    return products;
}
Also used : ArrayList(java.util.ArrayList) Product(com.google.cloud.retail.v2.Product) ColorInfo(com.google.cloud.retail.v2.ColorInfo) FulfillmentInfo(com.google.cloud.retail.v2.FulfillmentInfo) FieldMask(com.google.protobuf.FieldMask) PriceInfo(com.google.cloud.retail.v2.PriceInfo)

Aggregations

ArrayList (java.util.ArrayList)7 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)6 ColorInfo (com.google.cloud.vision.v1.ColorInfo)6 DominantColorsAnnotation (com.google.cloud.vision.v1.DominantColorsAnnotation)6 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)4 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)4 Feature (com.google.cloud.vision.v1.Feature)4 Image (com.google.cloud.vision.v1.Image)4 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)4 ImageSource (com.google.cloud.vision.v1.ImageSource)2 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)2 ByteString (com.google.protobuf.ByteString)2 FileInputStream (java.io.FileInputStream)2 Test (org.junit.Test)2 ColorInfo (com.google.cloud.retail.v2.ColorInfo)1 FulfillmentInfo (com.google.cloud.retail.v2.FulfillmentInfo)1 PriceInfo (com.google.cloud.retail.v2.PriceInfo)1 Product (com.google.cloud.retail.v2.Product)1 FieldMask (com.google.protobuf.FieldMask)1