Search in sources :

Example 16 with BatchAnnotateImagesResponse

use of com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse in project spring-cloud-gcp by GoogleCloudPlatform.

the class CloudVisionTemplateTests method testExtractTextError.

@Test
public void testExtractTextError() {
    AnnotateImageResponse response = AnnotateImageResponse.newBuilder().setError(Status.newBuilder().setCode(Code.INTERNAL.value()).setMessage("Error Message from Vision API.")).build();
    BatchAnnotateImagesResponse responseBatch = BatchAnnotateImagesResponse.newBuilder().addResponses(response).build();
    when(this.imageAnnotatorClient.batchAnnotateImages(any(BatchAnnotateImagesRequest.class))).thenReturn(responseBatch);
    this.expectedException.expect(CloudVisionException.class);
    this.expectedException.expectMessage("Error Message from Vision API.");
    this.cloudVisionTemplate.extractTextFromImage(FAKE_IMAGE);
}
Also used : BatchAnnotateImagesRequest(com.google.cloud.vision.v1.BatchAnnotateImagesRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) Test(org.junit.Test)

Example 17 with BatchAnnotateImagesResponse

use of com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse in project java-vision by googleapis.

the class QuickstartSample method main.

public static void main(String... args) throws Exception {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) {
        // The path to the image file to annotate
        String fileName = "./resources/wakeupcat.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.format("Error: %s%n", res.getError().getMessage());
                return;
            }
            for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
                annotation.getAllFields().forEach((k, v) -> System.out.format("%s : %s%n", k, v.toString()));
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.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) 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 18 with BatchAnnotateImagesResponse

use of com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse in project java-vision by googleapis.

the class DetectCropHints method detectCropHints.

// Suggests a region to crop to for a local file.
public static void detectCropHints(String filePath) throws 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(Feature.Type.CROP_HINTS).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).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()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                System.out.format("Error: %s%n", res.getError().getMessage());
                return;
            }
            // For full list of available annotations, see http://g.co/cloud/vision/docs
            CropHintsAnnotation annotation = res.getCropHintsAnnotation();
            for (CropHint hint : annotation.getCropHintsList()) {
                System.out.println(hint.getBoundingPoly());
            }
        }
    }
}
Also used : ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) FileInputStream(java.io.FileInputStream) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) CropHintsAnnotation(com.google.cloud.vision.v1.CropHintsAnnotation) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) CropHint(com.google.cloud.vision.v1.CropHint) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 19 with BatchAnnotateImagesResponse

use of com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse in project java-vision by googleapis.

the class ITSystemTest method detectWebEntitiesGcsTest.

@Test
public void detectWebEntitiesGcsTest() throws IOException {
    ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(SAMPLE_BUCKET + "landmark/pofa.jpg").build();
    Image img = Image.newBuilder().setSource(imgSource).build();
    Feature feat = Feature.newBuilder().setType(Type.WEB_DETECTION).setMaxResults(MAX_RESULTS).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(ImmutableList.of(request));
    List<AnnotateImageResponse> responses = response.getResponsesList();
    List<String> actual = new ArrayList<>();
    for (AnnotateImageResponse imgResponse : responses) {
        for (WebDetection.WebEntity entity : imgResponse.getWebDetection().getWebEntitiesList()) {
            actual.add(entity.getDescription());
        }
    }
    assertThat(actual).contains("The Palace Of Fine Arts");
}
Also used : WebDetection(com.google.cloud.vision.v1.WebDetection) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) ByteString(com.google.protobuf.ByteString) ReferenceImage(com.google.cloud.vision.v1.ReferenceImage) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) Test(org.junit.Test)

Example 20 with BatchAnnotateImagesResponse

use of com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse in project java-vision by googleapis.

the class ITSystemTest method addResponsesToList.

private List<String> addResponsesToList(AnnotateImageRequest request) {
    List<String> actual = new ArrayList<>();
    BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(ImmutableList.of(request));
    List<AnnotateImageResponse> responses = response.getResponsesList();
    for (AnnotateImageResponse res : responses) {
        if (res.getError().getCode() == 14) {
            throw new StatusRuntimeException(Status.UNAVAILABLE);
        }
        for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) {
            actual.add(annotation.getDescription());
        }
    }
    return actual;
}
Also used : AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Aggregations

BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)72 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)69 Image (com.google.cloud.vision.v1.Image)68 Feature (com.google.cloud.vision.v1.Feature)66 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)64 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)63 ArrayList (java.util.ArrayList)63 ByteString (com.google.protobuf.ByteString)44 ImageSource (com.google.cloud.vision.v1.ImageSource)37 FileInputStream (java.io.FileInputStream)32 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)28 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)26 IOException (java.io.IOException)13 ImageContext (com.google.cloud.vision.v1.ImageContext)12 WebDetection (com.google.cloud.vision.v1.WebDetection)11 LocationInfo (com.google.cloud.vision.v1.LocationInfo)10 SafeSearchAnnotation (com.google.cloud.vision.v1.SafeSearchAnnotation)10 WebPage (com.google.cloud.vision.v1.WebDetection.WebPage)9 Block (com.google.cloud.vision.v1.Block)8 ColorInfo (com.google.cloud.vision.v1.ColorInfo)8