Search in sources :

Example 6 with Image

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();
}
Also used : BatchAnnotateImagesRequest(com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest) AnnotateImageRequest(com.google.api.services.vision.v1.model.AnnotateImageRequest) AnnotateImageResponse(com.google.api.services.vision.v1.model.AnnotateImageResponse) IOException(java.io.IOException) Image(com.google.api.services.vision.v1.model.Image) BufferedImage(java.awt.image.BufferedImage) Feature(com.google.api.services.vision.v1.model.Feature) BatchAnnotateImagesResponse(com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse)

Example 7 with Image

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());
    }
}
Also used : EntityAnnotation(com.google.api.services.vision.v1.model.EntityAnnotation)

Example 8 with Image

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();
}
Also used : EntityAnnotation(com.google.api.services.vision.v1.model.EntityAnnotation)

Example 9 with Image

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]
}
Also used : BatchAnnotateImagesRequest(com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest) AnnotateImageRequest(com.google.api.services.vision.v1.model.AnnotateImageRequest) AnnotateImageResponse(com.google.api.services.vision.v1.model.AnnotateImageResponse) IOException(java.io.IOException) Image(com.google.api.services.vision.v1.model.Image) Feature(com.google.api.services.vision.v1.model.Feature) BatchAnnotateImagesResponse(com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse)

Example 10 with Image

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();
}
Also used : Filter(com.amazonaws.services.ec2.model.Filter) DescribeImagesResult(com.amazonaws.services.ec2.model.DescribeImagesResult) DescribeImagesRequest(com.amazonaws.services.ec2.model.DescribeImagesRequest) EbsBlockDevice(com.amazonaws.services.ec2.model.EbsBlockDevice) BlockDeviceMapping(com.amazonaws.services.ec2.model.BlockDeviceMapping) Image(com.amazonaws.services.ec2.model.Image)

Aggregations

Image (com.amazonaws.services.ec2.model.Image)5 AnnotateImageRequest (com.google.api.services.vision.v1.model.AnnotateImageRequest)4 AnnotateImageResponse (com.google.api.services.vision.v1.model.AnnotateImageResponse)4 BatchAnnotateImagesRequest (com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest)4 BatchAnnotateImagesResponse (com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse)4 Feature (com.google.api.services.vision.v1.model.Feature)4 Image (com.google.api.services.vision.v1.model.Image)4 IOException (java.io.IOException)4 BlockDeviceMapping (com.amazonaws.services.ec2.model.BlockDeviceMapping)3 DescribeImagesRequest (com.amazonaws.services.ec2.model.DescribeImagesRequest)3 DescribeImagesResult (com.amazonaws.services.ec2.model.DescribeImagesResult)3 EbsBlockDevice (com.amazonaws.services.ec2.model.EbsBlockDevice)3 EntityAnnotation (com.google.api.services.vision.v1.model.EntityAnnotation)2 Path (java.nio.file.Path)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)1 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)1 Filter (com.amazonaws.services.ec2.model.Filter)1 InstanceBlockDeviceMapping (com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping)1 Placement (com.amazonaws.services.ec2.model.Placement)1