Search in sources :

Example 6 with EntityAnnotation

use of com.google.cloud.vision.v1.EntityAnnotation 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 7 with EntityAnnotation

use of com.google.cloud.vision.v1.EntityAnnotation in project java-vision by googleapis.

the class ITSystemTest method detectTextTest.

@Test
public void detectTextTest() throws IOException {
    List<AnnotateImageResponse> responses = getResponsesList("text.jpg", Type.TEXT_DETECTION, false);
    List<String> actual = new ArrayList<>();
    for (AnnotateImageResponse res : responses) {
        for (EntityAnnotation annotation : res.getTextAnnotationsList()) {
            actual.add(annotation.getDescription());
        }
    }
    assertThat(actual).contains("37%");
}
Also used : AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Test(org.junit.Test)

Example 8 with EntityAnnotation

use of com.google.cloud.vision.v1.EntityAnnotation 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)

Example 9 with EntityAnnotation

use of com.google.cloud.vision.v1.EntityAnnotation in project java-vision by googleapis.

the class ITSystemTest method detectLandmarksTest.

@Test
public void detectLandmarksTest() throws IOException {
    List<AnnotateImageResponse> responses = getResponsesList("landmark.jpg", Type.LANDMARK_DETECTION, false);
    List<String> actual = new ArrayList<>();
    for (AnnotateImageResponse res : responses) {
        for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) {
            actual.add(annotation.getDescription());
        }
    }
    assertThat(actual).contains("Palace of Fine Arts");
}
Also used : AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Test(org.junit.Test)

Example 10 with EntityAnnotation

use of com.google.cloud.vision.v1.EntityAnnotation in project java-vision by googleapis.

the class ITSystemTest method detectLandmarksGcsTest.

@Test
public void detectLandmarksGcsTest() throws IOException {
    List<AnnotateImageResponse> responses = getResponsesList("landmark/pofa.jpg", Type.LANDMARK_DETECTION, true);
    List<String> actual = new ArrayList<>();
    for (AnnotateImageResponse res : responses) {
        for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) {
            actual.add(annotation.getDescription());
        }
    }
    assertThat(actual).contains("Palace of Fine Arts");
}
Also used : AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Test(org.junit.Test)

Aggregations

EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)36 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)34 ArrayList (java.util.ArrayList)30 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)25 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)24 Feature (com.google.cloud.vision.v1.Feature)24 Image (com.google.cloud.vision.v1.Image)24 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)23 ByteString (com.google.protobuf.ByteString)19 ImageSource (com.google.cloud.vision.v1.ImageSource)11 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)9 FileInputStream (java.io.FileInputStream)8 Test (org.junit.Test)7 LocationInfo (com.google.cloud.vision.v1.LocationInfo)6 ModelAndView (org.springframework.web.servlet.ModelAndView)5 Path (java.nio.file.Path)4 GetMapping (org.springframework.web.bind.annotation.GetMapping)4 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3