Search in sources :

Example 1 with LocalizedObjectAnnotation

use of com.google.cloud.vision.v1p3beta1.LocalizedObjectAnnotation in project java-vision by googleapis.

the class ITSystemTest method detectLocalizedObjectsGcsTest.

@Test
public void detectLocalizedObjectsGcsTest() throws IOException {
    List<AnnotateImageResponse> responses = getResponsesList("object_localization/puppies.jpg", Type.OBJECT_LOCALIZATION, true);
    List<String> actual = new ArrayList<>();
    for (AnnotateImageResponse res : responses) {
        for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) {
            actual.add(entity.getName());
        }
    }
    assertThat(actual).contains("Dog");
}
Also used : AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) LocalizedObjectAnnotation(com.google.cloud.vision.v1.LocalizedObjectAnnotation) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 2 with LocalizedObjectAnnotation

use of com.google.cloud.vision.v1p3beta1.LocalizedObjectAnnotation in project java-vision by googleapis.

the class Detect method detectLocalizedObjectsGcs.

// [END vision_localize_objects]
// [START vision_localize_objects_gcs]
/**
 * Detects localized objects in a remote image on Google Cloud Storage.
 *
 * @param gcsPath The path to the remote file on Google Cloud Storage to detect localized objects
 *     on.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLocalizedObjectsGcs(String gcsPath) throws IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
    Image img = Image.newBuilder().setSource(imgSource).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(Feature.newBuilder().setType(Type.OBJECT_LOCALIZATION)).setImage(img).build();
    requests.add(request);
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        // Perform the request
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        client.close();
        // Display the results
        for (AnnotateImageResponse res : responses) {
            for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) {
                System.out.format("Object name: %s%n", entity.getName());
                System.out.format("Confidence: %s%n", entity.getScore());
                System.out.format("Normalized Vertices:%n");
                entity.getBoundingPoly().getNormalizedVerticesList().forEach(vertex -> System.out.format("- (%s, %s)%n", vertex.getX(), vertex.getY()));
            }
        }
    }
}
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) LocalizedObjectAnnotation(com.google.cloud.vision.v1.LocalizedObjectAnnotation) ImageSource(com.google.cloud.vision.v1.ImageSource) Image(com.google.cloud.vision.v1.Image) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 3 with LocalizedObjectAnnotation

use of com.google.cloud.vision.v1p3beta1.LocalizedObjectAnnotation in project java-vision by googleapis.

the class Detect method detectLocalizedObjects.

// [END vision_text_detection_pdf_gcs]
// [START vision_localize_objects]
/**
 * Detects localized objects in the specified local image.
 *
 * @param filePath The path to the file to perform localized object detection on.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLocalizedObjects(String filePath) throws IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
    Image img = Image.newBuilder().setContent(imgBytes).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(Feature.newBuilder().setType(Type.OBJECT_LOCALIZATION)).setImage(img).build();
    requests.add(request);
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        // Perform the request
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        // Display the results
        for (AnnotateImageResponse res : responses) {
            for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) {
                System.out.format("Object name: %s%n", entity.getName());
                System.out.format("Confidence: %s%n", entity.getScore());
                System.out.format("Normalized Vertices:%n");
                entity.getBoundingPoly().getNormalizedVerticesList().forEach(vertex -> System.out.format("- (%s, %s)%n", vertex.getX(), vertex.getY()));
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) LocalizedObjectAnnotation(com.google.cloud.vision.v1.LocalizedObjectAnnotation) Image(com.google.cloud.vision.v1.Image) FileInputStream(java.io.FileInputStream) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 4 with LocalizedObjectAnnotation

use of com.google.cloud.vision.v1p3beta1.LocalizedObjectAnnotation in project java-vision by googleapis.

the class DetectBeta method detectLocalizedObjectsGcs.

// [END vision_localize_objects_beta]
// [START vision_localize_objects_gcs_beta]
/**
 * Detects localized objects in a remote image on Google Cloud Storage.
 *
 * @param gcsPath The path to the remote file on Google Cloud Storage to detect localized objects
 *     on.
 * @param out A {@link PrintStream} to write detected objects to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLocalizedObjectsGcs(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();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(Feature.newBuilder().setType(Type.OBJECT_LOCALIZATION)).setImage(img).build();
    requests.add(request);
    // Perform the request
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        client.close();
        // Display the results
        for (AnnotateImageResponse res : responses) {
            for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) {
                out.format("Object name: %s\n", entity.getName());
                out.format("Confidence: %s\n", entity.getScore());
                out.format("Normalized Vertices:\n");
                entity.getBoundingPoly().getNormalizedVerticesList().forEach(vertex -> out.format("- (%s, %s)\n", vertex.getX(), vertex.getY()));
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1p3beta1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1p3beta1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1p3beta1.AnnotateImageResponse) ArrayList(java.util.ArrayList) LocalizedObjectAnnotation(com.google.cloud.vision.v1p3beta1.LocalizedObjectAnnotation) ImageSource(com.google.cloud.vision.v1p3beta1.ImageSource) Image(com.google.cloud.vision.v1p3beta1.Image) BatchAnnotateImagesResponse(com.google.cloud.vision.v1p3beta1.BatchAnnotateImagesResponse)

Example 5 with LocalizedObjectAnnotation

use of com.google.cloud.vision.v1p3beta1.LocalizedObjectAnnotation in project java-vision by googleapis.

the class ITSystemTest method detectLocalizedObjectsTest.

@Test
public void detectLocalizedObjectsTest() throws IOException {
    List<AnnotateImageResponse> responses = getResponsesList("puppies.jpg", Type.OBJECT_LOCALIZATION, false);
    List<String> actual = new ArrayList<>();
    for (AnnotateImageResponse res : responses) {
        for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) {
            actual.add(entity.getName());
        }
    }
    assertThat(actual).contains("Dog");
}
Also used : AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) LocalizedObjectAnnotation(com.google.cloud.vision.v1.LocalizedObjectAnnotation) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)6 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)4 LocalizedObjectAnnotation (com.google.cloud.vision.v1.LocalizedObjectAnnotation)4 ByteString (com.google.protobuf.ByteString)4 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)2 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)2 Image (com.google.cloud.vision.v1.Image)2 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)2 AnnotateImageRequest (com.google.cloud.vision.v1p3beta1.AnnotateImageRequest)2 AnnotateImageResponse (com.google.cloud.vision.v1p3beta1.AnnotateImageResponse)2 BatchAnnotateImagesResponse (com.google.cloud.vision.v1p3beta1.BatchAnnotateImagesResponse)2 Image (com.google.cloud.vision.v1p3beta1.Image)2 ImageAnnotatorClient (com.google.cloud.vision.v1p3beta1.ImageAnnotatorClient)2 LocalizedObjectAnnotation (com.google.cloud.vision.v1p3beta1.LocalizedObjectAnnotation)2 FileInputStream (java.io.FileInputStream)2 Test (org.junit.Test)2 ImageSource (com.google.cloud.vision.v1.ImageSource)1 ImageSource (com.google.cloud.vision.v1p3beta1.ImageSource)1