Search in sources :

Example 6 with ColorInfo

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

the class DetectPropertiesGcs method detectPropertiesGcs.

// Detects image properties such as color frequency from the specified remote image on Google
// Cloud Storage.
public static void detectPropertiesGcs(String gcsPath) throws 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(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 : 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) 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 7 with ColorInfo

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

the class ITSystemTest method detectPropertiesGcsTest.

@Test
public void detectPropertiesGcsTest() throws IOException {
    List<AnnotateImageResponse> responses = getResponsesList("landmark/pofa.jpg", Type.IMAGE_PROPERTIES, true);
    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)

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