Search in sources :

Example 1 with BatchAnnotateImagesResponse

use of com.google.cloud.vision.v1.BatchAnnotateImagesResponse in project google-cloud-java by GoogleCloudPlatform.

the class AnnotateImage method main.

public static void main(String... args) throws Exception {
    // Instantiates a client
    ImageAnnotatorClient vision = ImageAnnotatorClient.create();
    // The path to the image file to annotate
    // for example "./resources/wakeupcat.jpg";
    String fileName = "your/image/path.jpg";
    // Reads the image file into memory
    Path path = Paths.get(fileName);
    byte[] data = Files.readAllBytes(path);
    ByteString imgBytes = ByteString.copyFrom(data);
    // Builds the image annotation request
    List<AnnotateImageRequest> requests = new ArrayList<>();
    Image img = Image.newBuilder().setContent(imgBytes).build();
    Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    // Performs label detection on the image file
    BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();
    for (AnnotateImageResponse res : responses) {
        if (res.hasError()) {
            System.out.printf("Error: %s\n", res.getError().getMessage());
            return;
        }
        for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
            for (Map.Entry<FieldDescriptor, Object> entry : annotation.getAllFields().entrySet()) {
                System.out.printf("%s : %s\n", entry.getKey(), entry.getValue());
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.spi.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Map(java.util.Map) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 2 with BatchAnnotateImagesResponse

use of com.google.cloud.vision.v1.BatchAnnotateImagesResponse in project google-cloud-java by GoogleCloudPlatform.

the class ImageAnnotatorClientTest method batchAnnotateImagesTest.

@Test
@SuppressWarnings("all")
public void batchAnnotateImagesTest() {
    BatchAnnotateImagesResponse expectedResponse = BatchAnnotateImagesResponse.newBuilder().build();
    mockImageAnnotator.addResponse(expectedResponse);
    List<AnnotateImageRequest> requests = new ArrayList<>();
    BatchAnnotateImagesResponse actualResponse = client.batchAnnotateImages(requests);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockImageAnnotator.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    BatchAnnotateImagesRequest actualRequest = (BatchAnnotateImagesRequest) actualRequests.get(0);
    Assert.assertEquals(requests, actualRequest.getRequestsList());
}
Also used : BatchAnnotateImagesRequest(com.google.cloud.vision.v1.BatchAnnotateImagesRequest) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ArrayList(java.util.ArrayList) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) Test(org.junit.Test)

Aggregations

AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)2 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)2 ArrayList (java.util.ArrayList)2 ImageAnnotatorClient (com.google.cloud.vision.spi.v1.ImageAnnotatorClient)1 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)1 BatchAnnotateImagesRequest (com.google.cloud.vision.v1.BatchAnnotateImagesRequest)1 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)1 Feature (com.google.cloud.vision.v1.Feature)1 Image (com.google.cloud.vision.v1.Image)1 ByteString (com.google.protobuf.ByteString)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)1 Path (java.nio.file.Path)1 Map (java.util.Map)1 Test (org.junit.Test)1