use of com.google.cloud.automl.v1.Image 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.automl.v1.Image in project java-docs-samples by GoogleCloudPlatform.
the class Detect method detectLogosGcs.
/**
* Detects logos in the specified remote image on Google Cloud Storage.
*
* @param gcsPath The path to the remote file on Google Cloud Storage to perform logo detection
* on.
* @param out A {@link PrintStream} to write detected logos to.
* @throws Exception on errors while closing the client.
* @throws IOException on Input/Output errors.
*/
public static void detectLogosGcs(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.LOGO_DETECTION).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
for (EntityAnnotation annotation : res.getLogoAnnotationsList()) {
out.println(annotation.getDescription());
}
}
}
}
use of com.google.cloud.automl.v1.Image in project java-docs-samples by GoogleCloudPlatform.
the class QuickstartSample method main.
public static void main(String... args) throws Exception {
// Instantiates a client
try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) {
// The path to the image file to annotate
String fileName = "./resources/wakeupcat.jpg";
// Reads the image file into memory
Path path = Paths.get(fileName);
byte[] data = Files.readAllBytes(path);
ByteString imgBytes = ByteString.copyFrom(data);
// Builds the image annotation request
List<AnnotateImageRequest> requests = new ArrayList<>();
Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);
// Performs label detection on the image file
BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.printf("Error: %s\n", res.getError().getMessage());
return;
}
for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
annotation.getAllFields().forEach((k, v) -> System.out.printf("%s : %s\n", k, v.toString()));
}
}
}
}
use of com.google.cloud.automl.v1.Image in project spring-cloud-gcp by spring-cloud.
the class CloudVisionTemplate method analyzeImage.
/**
* Analyze an image and extract the features of the image specified by
* {@code featureTypes}.
* <p>A feature describes the kind of Cloud Vision analysis one wishes to perform on an
* image, such as text detection, image labelling, facial detection, etc. A full list of
* feature types can be found in {@link Feature.Type}.
* @param imageResource the image one wishes to analyze. The Cloud Vision APIs support
* image formats described here: https://cloud.google.com/vision/docs/supported-files
* @param imageContext the image context used to customize the Vision API request
* @param featureTypes the types of image analysis to perform on the image
* @return the results of image analyses
* @throws CloudVisionException if the image could not be read or if a malformed response
* is received from the Cloud Vision APIs
*/
public AnnotateImageResponse analyzeImage(Resource imageResource, ImageContext imageContext, Feature.Type... featureTypes) {
ByteString imgBytes;
try {
imgBytes = ByteString.readFrom(imageResource.getInputStream());
} catch (IOException ex) {
throw new CloudVisionException("Failed to read image bytes from provided resource.", ex);
}
Image image = Image.newBuilder().setContent(imgBytes).build();
List<Feature> featureList = Arrays.stream(featureTypes).map((featureType) -> Feature.newBuilder().setType(featureType).build()).collect(Collectors.toList());
BatchAnnotateImagesRequest request = BatchAnnotateImagesRequest.newBuilder().addRequests(AnnotateImageRequest.newBuilder().addAllFeatures(featureList).setImageContext(imageContext).setImage(image)).build();
BatchAnnotateImagesResponse batchResponse = this.imageAnnotatorClient.batchAnnotateImages(request);
List<AnnotateImageResponse> annotateImageResponses = batchResponse.getResponsesList();
if (!annotateImageResponses.isEmpty()) {
return annotateImageResponses.get(0);
} else {
throw new CloudVisionException("Failed to receive valid response Vision APIs; empty response received.");
}
}
use of com.google.cloud.automl.v1.Image in project ovirt-engine by oVirt.
the class OpenStackImageProviderProxy method getImageAsDiskImage.
public DiskImage getImageAsDiskImage(String id) {
DiskImage diskImage = new DiskImage();
Image glanceImage;
try {
glanceImage = getClient().images().show(id).execute();
} catch (OpenStackResponseException e) {
log.debug("Exception:", e);
throw new OpenStackImageException(OpenStackImageException.ErrorType.IMAGE_NOT_FOUND, "Cannot find the specified image.");
} catch (RuntimeException rte) {
log.error("Exception:", rte);
throw new RuntimeException("Failed to import from the repository.");
}
validateContainerFormat(glanceImage);
String shortHash = glanceImage.getId().substring(0, 7);
if (glanceImage.getName() != null) {
diskImage.setDiskDescription(glanceImage.getName() + " (" + shortHash + ")");
} else {
diskImage.setDiskDescription("Glance disk: " + shortHash);
}
setDiskAttributes(diskImage, glanceImage);
if (glanceImage.getDiskFormat().equals(GlanceImageFormat.RAW.getValue())) {
diskImage.setVolumeFormat(VolumeFormat.RAW);
} else if (glanceImage.getDiskFormat().equals(GlanceImageFormat.COW.getValue())) {
diskImage.setVolumeFormat(VolumeFormat.COW);
} else {
throw new OpenStackImageException(OpenStackImageException.ErrorType.UNSUPPORTED_DISK_FORMAT, "Unknown disk format: " + glanceImage.getDiskFormat());
}
return diskImage;
}
Aggregations