Search in sources :

Example 1 with Image

use of com.google.api.services.vision.v1.model.Image in project java-docs-samples by GoogleCloudPlatform.

the class FaceDetectApp method main.

// [START main]
/**
 * Annotates an image using the Vision API.
 */
public static void main(String[] args) throws IOException, GeneralSecurityException {
    if (args.length != 2) {
        System.err.println("Usage:");
        System.err.printf("\tjava %s inputImagePath outputImagePath\n", FaceDetectApp.class.getCanonicalName());
        System.exit(1);
    }
    Path inputPath = Paths.get(args[0]);
    Path outputPath = Paths.get(args[1]);
    if (!outputPath.toString().toLowerCase().endsWith(".jpg")) {
        System.err.println("outputImagePath must have the file extension 'jpg'.");
        System.exit(1);
    }
    FaceDetectApp app = new FaceDetectApp(getVisionService());
    List<FaceAnnotation> faces = app.detectFaces(inputPath, MAX_RESULTS);
    System.out.printf("Found %d face%s\n", faces.size(), faces.size() == 1 ? "" : "s");
    System.out.printf("Writing to file %s\n", outputPath);
    app.writeWithFaces(inputPath, outputPath, faces);
}
Also used : Path(java.nio.file.Path) FaceAnnotation(com.google.api.services.vision.v1.model.FaceAnnotation)

Example 2 with Image

use of com.google.api.services.vision.v1.model.Image in project java-docs-samples by GoogleCloudPlatform.

the class DetectLandmark method identifyLandmark.

/**
 * Gets up to {@code maxResults} landmarks for an image stored at {@code uri}.
 */
public List<EntityAnnotation> identifyLandmark(String uri, int maxResults) throws IOException {
    AnnotateImageRequest request = new AnnotateImageRequest().setImage(new Image().setSource(new ImageSource().setGcsImageUri(uri))).setFeatures(ImmutableList.of(new Feature().setType("LANDMARK_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.getLandmarkAnnotations() == null) {
        throw new IOException(response.getError() != null ? response.getError().getMessage() : "Unknown error getting image annotations");
    }
    return response.getLandmarkAnnotations();
}
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) ImageSource(com.google.api.services.vision.v1.model.ImageSource) 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 3 with Image

use of com.google.api.services.vision.v1.model.Image in project java-docs-samples by GoogleCloudPlatform.

the class TextApp method detectText.

/**
 * Gets up to {@code maxResults} text annotations for images stored at {@code paths}.
 */
public ImmutableList<ImageText> detectText(List<Path> paths) {
    ImmutableList.Builder<AnnotateImageRequest> requests = ImmutableList.builder();
    try {
        for (Path path : paths) {
            byte[] data;
            data = Files.readAllBytes(path);
            requests.add(new AnnotateImageRequest().setImage(new Image().encodeContent(data)).setFeatures(ImmutableList.of(new Feature().setType("TEXT_DETECTION").setMaxResults(MAX_RESULTS))));
        }
        Vision.Images.Annotate annotate = vision.images().annotate(new BatchAnnotateImagesRequest().setRequests(requests.build()));
        // 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() == paths.size();
        ImmutableList.Builder<ImageText> output = ImmutableList.builder();
        for (int i = 0; i < paths.size(); i++) {
            Path path = paths.get(i);
            AnnotateImageResponse response = batchResponse.getResponses().get(i);
            output.add(ImageText.builder().path(path).textAnnotations(MoreObjects.firstNonNull(response.getTextAnnotations(), ImmutableList.<EntityAnnotation>of())).error(response.getError()).build());
        }
        return output.build();
    } catch (IOException ex) {
        // Got an exception, which means the whole batch had an error.
        ImmutableList.Builder<ImageText> output = ImmutableList.builder();
        for (Path path : paths) {
            output.add(ImageText.builder().path(path).textAnnotations(ImmutableList.<EntityAnnotation>of()).error(new Status().setMessage(ex.getMessage())).build());
        }
        return output.build();
    }
}
Also used : Path(java.nio.file.Path) Status(com.google.api.services.vision.v1.model.Status) BatchAnnotateImagesRequest(com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) Image(com.google.api.services.vision.v1.model.Image) Feature(com.google.api.services.vision.v1.model.Feature) AnnotateImageRequest(com.google.api.services.vision.v1.model.AnnotateImageRequest) AnnotateImageResponse(com.google.api.services.vision.v1.model.AnnotateImageResponse) BatchAnnotateImagesResponse(com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse)

Example 4 with Image

use of com.google.api.services.vision.v1.model.Image in project SimianArmy by Netflix.

the class EBSSnapshotJanitorCrawler method refreshSnapshotToAMIs.

private void refreshSnapshotToAMIs() {
    snapshotToAMIs.clear();
    for (Image image : getAWSClient().describeImages()) {
        for (BlockDeviceMapping bdm : image.getBlockDeviceMappings()) {
            EbsBlockDevice ebd = bdm.getEbs();
            if (ebd != null && ebd.getSnapshotId() != null) {
                LOGGER.debug(String.format("Snapshot %s is used to generate AMI %s", ebd.getSnapshotId(), image.getImageId()));
                Collection<String> amis = snapshotToAMIs.get(ebd.getSnapshotId());
                if (amis == null) {
                    amis = new ArrayList<String>();
                    snapshotToAMIs.put(ebd.getSnapshotId(), amis);
                }
                amis.add(image.getImageId());
            }
        }
    }
}
Also used : EbsBlockDevice(com.amazonaws.services.ec2.model.EbsBlockDevice) BlockDeviceMapping(com.amazonaws.services.ec2.model.BlockDeviceMapping) Image(com.amazonaws.services.ec2.model.Image)

Example 5 with Image

use of com.google.api.services.vision.v1.model.Image in project java-docs-samples by GoogleCloudPlatform.

the class FaceDetectApp method annotateWithFace.

/**
 * Annotates an image {@code img} with a polygon defined by {@code face}.
 */
private static void annotateWithFace(BufferedImage img, FaceAnnotation face) {
    Graphics2D gfx = img.createGraphics();
    Polygon poly = new Polygon();
    for (Vertex vertex : face.getFdBoundingPoly().getVertices()) {
        poly.addPoint(vertex.getX(), vertex.getY());
    }
    gfx.setStroke(new BasicStroke(5));
    gfx.setColor(new Color(0x00ff00));
    gfx.draw(poly);
}
Also used : BasicStroke(java.awt.BasicStroke) Vertex(com.google.api.services.vision.v1.model.Vertex) Color(java.awt.Color) Polygon(java.awt.Polygon) Graphics2D(java.awt.Graphics2D)

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