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()));
}
}
}
}
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());
}
}
}
}
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());
}
}
}
}
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");
}
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");
}
Aggregations