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