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());
}
}
}
}
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());
}
}
}
}
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);
}
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());
}
}
}
}
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;
}
Aggregations