Search in sources :

Example 31 with EntityAnnotation

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

the class DetectLabelsGcs method detectLabelsGcs.

// Detects labels in the specified remote image on Google Cloud Storage.
public static void detectLabelsGcs(String gcsPath) throws 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(Feature.Type.LABEL_DETECTION).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
            for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
                annotation.getAllFields().forEach((k, v) -> System.out.format("%s : %s%n", k, v.toString()));
            }
        }
    }
}
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) 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)

Example 32 with EntityAnnotation

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

the class DetectLogos method detectLogos.

// Detects logos in the specified local image.
public static void detectLogos(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.LOGO_DETECTION).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
            for (EntityAnnotation annotation : res.getLogoAnnotationsList()) {
                System.out.println(annotation.getDescription());
            }
        }
    }
}
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) Image(com.google.cloud.vision.v1.Image) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Feature(com.google.cloud.vision.v1.Feature) FileInputStream(java.io.FileInputStream) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 33 with EntityAnnotation

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

the class DetectLogosGcs method detectLogosGcs.

// Detects logos in the specified remote image on Google Cloud Storage.
public static void detectLogosGcs(String gcsPath) throws 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(Feature.Type.LOGO_DETECTION).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
            for (EntityAnnotation annotation : res.getLogoAnnotationsList()) {
                System.out.println(annotation.getDescription());
            }
        }
    }
}
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) 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)

Example 34 with EntityAnnotation

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

the class ITSystemTest method detectLabelsGcsTest.

@Test
public void detectLabelsGcsTest() throws IOException {
    List<AnnotateImageResponse> responses = getResponsesList("label/wakeupcat.jpg", Type.LABEL_DETECTION, true);
    List<String> actual = new ArrayList<>();
    for (AnnotateImageResponse res : responses) {
        for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
            actual.add(annotation.getDescription());
        }
    }
    assertThat(actual).contains("Whiskers");
}
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 35 with EntityAnnotation

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

the class ITSystemTest method detectLabelsTest.

@Test
public void detectLabelsTest() throws IOException {
    List<AnnotateImageResponse> responses = getResponsesList("wakeupcat.jpg", Type.LABEL_DETECTION, false);
    List<String> actual = new ArrayList<>();
    for (AnnotateImageResponse res : responses) {
        for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
            actual.add(annotation.getDescription());
        }
    }
    assertThat(actual).contains("Whiskers");
}
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