Search in sources :

Example 21 with ImageAnnotatorClient

use of com.google.cloud.vision.v1.ImageAnnotatorClient in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectLandmarks.

/**
 * Detects landmarks in the specified local image.
 *
 * @param filePath The path to the file to perform landmark detection on.
 * @param out A {@link PrintStream} to write detected landmarks to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLandmarks(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.LANDMARK_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.getLandmarkAnnotationsList()) {
                LocationInfo info = annotation.getLocationsList().listIterator().next();
                out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng());
            }
        }
    }
}
Also used : ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) FileInputStream(java.io.FileInputStream) LocationInfo(com.google.cloud.vision.v1.LocationInfo) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 22 with ImageAnnotatorClient

use of com.google.cloud.vision.v1.ImageAnnotatorClient in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectWebEntities.

// [END vision_detect_web_uri]
/**
 * Find web entities given a local image.
 * @param filePath The path of the image to detect.
 * @param out A {@link PrintStream} to write the results to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectWebEntities(String filePath, PrintStream out) throws Exception, IOException {
    // Instantiates a client
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        // Read in the local image
        ByteString contents = ByteString.readFrom(new FileInputStream(filePath));
        // Build the image
        Image image = Image.newBuilder().setContent(contents).build();
        // Create the request with the image and the specified feature: web detection
        AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)).setImage(image).build();
        // Perform the request
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request));
        // Display the results
        response.getResponsesList().stream().forEach(r -> r.getWebDetection().getWebEntitiesList().stream().forEach(entity -> {
            out.format("Description: %s\n", entity.getDescription());
            out.format("Score: %f\n", entity.getScore());
        }));
    }
}
Also used : WebDetectionParams(com.google.cloud.vision.v1.WebDetectionParams) Arrays(java.util.Arrays) WebPage(com.google.cloud.vision.v1.WebDetection.WebPage) Paragraph(com.google.cloud.vision.v1.Paragraph) ArrayList(java.util.ArrayList) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) WebLabel(com.google.cloud.vision.v1.WebDetection.WebLabel) CropHint(com.google.cloud.vision.v1.CropHint) PrintStream(java.io.PrintStream) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ImageSource(com.google.cloud.vision.v1.ImageSource) WebDetection(com.google.cloud.vision.v1.WebDetection) Type(com.google.cloud.vision.v1.Feature.Type) LocationInfo(com.google.cloud.vision.v1.LocationInfo) FaceAnnotation(com.google.cloud.vision.v1.FaceAnnotation) Block(com.google.cloud.vision.v1.Block) CropHintsAnnotation(com.google.cloud.vision.v1.CropHintsAnnotation) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Feature(com.google.cloud.vision.v1.Feature) Symbol(com.google.cloud.vision.v1.Symbol) ColorInfo(com.google.cloud.vision.v1.ColorInfo) ByteString(com.google.protobuf.ByteString) List(java.util.List) TextAnnotation(com.google.cloud.vision.v1.TextAnnotation) Image(com.google.cloud.vision.v1.Image) WebEntity(com.google.cloud.vision.v1.WebDetection.WebEntity) ImageContext(com.google.cloud.vision.v1.ImageContext) SafeSearchAnnotation(com.google.cloud.vision.v1.SafeSearchAnnotation) Page(com.google.cloud.vision.v1.Page) DominantColorsAnnotation(com.google.cloud.vision.v1.DominantColorsAnnotation) Word(com.google.cloud.vision.v1.Word) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) FileInputStream(java.io.FileInputStream) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 23 with ImageAnnotatorClient

use of com.google.cloud.vision.v1.ImageAnnotatorClient in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectLandmarksUrl.

/**
 * Detects landmarks in the specified URI.
 *
 * @param uri The path to the file to perform landmark detection on.
 * @param out A {@link PrintStream} to write detected landmarks to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLandmarksUrl(String uri, PrintStream out) throws Exception, IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ImageSource imgSource = ImageSource.newBuilder().setImageUri(uri).build();
    Image img = Image.newBuilder().setSource(imgSource).build();
    Feature feat = Feature.newBuilder().setType(Type.LANDMARK_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.getLandmarkAnnotationsList()) {
                LocationInfo info = annotation.getLocationsList().listIterator().next();
                out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng());
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) LocationInfo(com.google.cloud.vision.v1.LocationInfo)

Example 24 with ImageAnnotatorClient

use of com.google.cloud.vision.v1.ImageAnnotatorClient in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectLandmarksGcs.

/**
 * Detects landmarks in the specified remote image on Google Cloud Storage.
 *
 * @param gcsPath The path to the remote file on Google Cloud Storage to perform landmark
 *                detection on.
 * @param out A {@link PrintStream} to write detected landmarks to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLandmarksGcs(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.LANDMARK_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.getLandmarkAnnotationsList()) {
                LocationInfo info = annotation.getLocationsList().listIterator().next();
                out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng());
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) LocationInfo(com.google.cloud.vision.v1.LocationInfo)

Example 25 with ImageAnnotatorClient

use of com.google.cloud.vision.v1.ImageAnnotatorClient 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());
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) DominantColorsAnnotation(com.google.cloud.vision.v1.DominantColorsAnnotation) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) ColorInfo(com.google.cloud.vision.v1.ColorInfo) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Aggregations

AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)27 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)27 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)27 Feature (com.google.cloud.vision.v1.Feature)27 Image (com.google.cloud.vision.v1.Image)27 ArrayList (java.util.ArrayList)27 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)26 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)25 ByteString (com.google.protobuf.ByteString)17 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)15 ImageSource (com.google.cloud.vision.v1.ImageSource)15 FileInputStream (java.io.FileInputStream)14 WebPage (com.google.cloud.vision.v1.WebDetection.WebPage)8 LocationInfo (com.google.cloud.vision.v1.LocationInfo)7 Block (com.google.cloud.vision.v1.Block)6 ColorInfo (com.google.cloud.vision.v1.ColorInfo)6 CropHint (com.google.cloud.vision.v1.CropHint)6 CropHintsAnnotation (com.google.cloud.vision.v1.CropHintsAnnotation)6 DominantColorsAnnotation (com.google.cloud.vision.v1.DominantColorsAnnotation)6 FaceAnnotation (com.google.cloud.vision.v1.FaceAnnotation)6