use of com.google.api.services.vision.v1.model.Image in project java-docs-samples by GoogleCloudPlatform.
the class FaceDetectApp method detectFaces.
// [START detect_face]
/**
* Gets up to {@code maxResults} faces for an image stored at {@code path}.
*/
public List<FaceAnnotation> detectFaces(Path path, int maxResults) throws IOException {
byte[] data = Files.readAllBytes(path);
AnnotateImageRequest request = new AnnotateImageRequest().setImage(new Image().encodeContent(data)).setFeatures(ImmutableList.of(new Feature().setType("FACE_DETECTION").setMaxResults(maxResults)));
Vision.Images.Annotate annotate = vision.images().annotate(new BatchAnnotateImagesRequest().setRequests(ImmutableList.of(request)));
// Due to a bug: requests to Vision API containing large images fail when GZipped.
annotate.setDisableGZipContent(true);
BatchAnnotateImagesResponse batchResponse = annotate.execute();
assert batchResponse.getResponses().size() == 1;
AnnotateImageResponse response = batchResponse.getResponses().get(0);
if (response.getFaceAnnotations() == null) {
throw new IOException(response.getError() != null ? response.getError().getMessage() : "Unknown error getting image annotations");
}
return response.getFaceAnnotations();
}
use of com.google.api.services.vision.v1.model.Image in project java-docs-samples by GoogleCloudPlatform.
the class DetectLandmark method main.
// [START run_application]
/**
* Annotates an image using the Vision API.
*/
public static void main(String[] args) throws IOException, GeneralSecurityException {
if (args.length != 1) {
System.err.println("Usage:");
System.err.printf("\tjava %s gs://<bucket_name>/<object_name>\n", DetectLandmark.class.getCanonicalName());
System.exit(1);
} else if (!args[0].toLowerCase().startsWith("gs://")) {
System.err.println("Google Cloud Storage url must start with 'gs://'.");
System.exit(1);
}
DetectLandmark app = new DetectLandmark(getVisionService());
List<EntityAnnotation> landmarks = app.identifyLandmark(args[0], MAX_RESULTS);
System.out.printf("Found %d landmark%s\n", landmarks.size(), landmarks.size() == 1 ? "" : "s");
for (EntityAnnotation annotation : landmarks) {
System.out.printf("\t%s\n", annotation.getDescription());
}
}
use of com.google.api.services.vision.v1.model.Image in project java-docs-samples by GoogleCloudPlatform.
the class TextApp method extractDescriptions.
/**
* Extracts as a combinded string, all the descriptions from text annotations on an {@code image}.
*/
public Word extractDescriptions(ImageText image) {
String document = "";
for (EntityAnnotation text : image.textAnnotations()) {
document += text.getDescription();
}
if (document.equals("")) {
System.out.printf("%s had no discernible text.\n", image.path());
}
// Output a progress indicator.
System.out.print('.');
System.out.flush();
return Word.builder().path(image.path()).word(document).build();
}
use of com.google.api.services.vision.v1.model.Image in project java-docs-samples by GoogleCloudPlatform.
the class LabelApp method labelImage.
/**
* Gets up to {@code maxResults} labels for an image stored at {@code path}.
*/
public List<EntityAnnotation> labelImage(Path path, int maxResults) throws IOException {
// [START construct_request]
byte[] data = Files.readAllBytes(path);
AnnotateImageRequest request = new AnnotateImageRequest().setImage(new Image().encodeContent(data)).setFeatures(ImmutableList.of(new Feature().setType("LABEL_DETECTION").setMaxResults(maxResults)));
Vision.Images.Annotate annotate = vision.images().annotate(new BatchAnnotateImagesRequest().setRequests(ImmutableList.of(request)));
// Due to a bug: requests to Vision API containing large images fail when GZipped.
annotate.setDisableGZipContent(true);
// [END construct_request]
// [START parse_response]
BatchAnnotateImagesResponse batchResponse = annotate.execute();
assert batchResponse.getResponses().size() == 1;
AnnotateImageResponse response = batchResponse.getResponses().get(0);
if (response.getLabelAnnotations() == null) {
throw new IOException(response.getError() != null ? response.getError().getMessage() : "Unknown error getting image annotations");
}
return response.getLabelAnnotations();
// [END parse_response]
}
use of com.google.api.services.vision.v1.model.Image in project photon-model by vmware.
the class TestAWSImageEnumerationTask method lookupAwsImage.
// Kind of overhead cause it loads almost all images just to get the first one.
// Still we need that to make the tests STABLE.
private String lookupAwsImage(AmazonEC2AsyncClient client, String virtualizationType) {
DescribeImagesRequest request = new DescribeImagesRequest().withFilters(new Filter(AWSConstants.AWS_IMAGE_STATE_FILTER).withValues(AWSConstants.AWS_IMAGE_STATE_AVAILABLE)).withFilters(new Filter(AWSConstants.AWS_IMAGE_IS_PUBLIC_FILTER).withValues(Boolean.TRUE.toString())).withFilters(new Filter("root-device-type").withValues("ebs")).withFilters(new Filter(AWSConstants.AWS_IMAGE_VIRTUALIZATION_TYPE_FILTER).withValues(virtualizationType));
DescribeImagesResult describeImages = client.describeImages(request);
Image image = describeImages.getImages().stream().filter(img -> {
for (BlockDeviceMapping blockDeviceMapping : img.getBlockDeviceMappings()) {
// blockDeviceMapping can be with noDevice
EbsBlockDevice ebs = blockDeviceMapping.getEbs();
if (ebs != null) {
return true;
}
}
return false;
}).findFirst().get();
getHost().log(Level.INFO, "AWS '%s' image loaded (out of %s): %s [%s]", virtualizationType, describeImages.getImages().size(), image.getName(), image);
return image.getName();
}
Aggregations