use of com.google.cloud.vision.v1p3beta1.AnnotateImageRequest in project google-cloud-java by GoogleCloudPlatform.
the class ImageAnnotatorClientTest method batchAnnotateImagesTest.
@Test
@SuppressWarnings("all")
public void batchAnnotateImagesTest() {
BatchAnnotateImagesResponse expectedResponse = BatchAnnotateImagesResponse.newBuilder().build();
mockImageAnnotator.addResponse(expectedResponse);
List<AnnotateImageRequest> requests = new ArrayList<>();
BatchAnnotateImagesResponse actualResponse = client.batchAnnotateImages(requests);
Assert.assertEquals(expectedResponse, actualResponse);
List<GeneratedMessageV3> actualRequests = mockImageAnnotator.getRequests();
Assert.assertEquals(1, actualRequests.size());
BatchAnnotateImagesRequest actualRequest = (BatchAnnotateImagesRequest) actualRequests.get(0);
Assert.assertEquals(requests, actualRequest.getRequestsList());
}
use of com.google.cloud.vision.v1p3beta1.AnnotateImageRequest in project spring-cloud-gcp by spring-cloud.
the class VisionController method uploadImage.
/**
* This method downloads an image from a URL and sends its contents to the Vision API for label detection.
*
* @param imageUrl the URL of the image
* @return a string with the list of labels and percentage of certainty
* @throws Exception if the Vision API call produces an error
*/
@GetMapping("/vision")
public String uploadImage(String imageUrl) throws Exception {
// Copies the content of the image to memory.
byte[] imageBytes = StreamUtils.copyToByteArray(this.resourceLoader.getResource(imageUrl).getInputStream());
BatchAnnotateImagesResponse responses;
Image image = Image.newBuilder().setContent(ByteString.copyFrom(imageBytes)).build();
// Sets the type of request to label detection, to detect broad sets of categories in an image.
Feature feature = Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder().setImage(image).addFeatures(feature).build();
responses = this.imageAnnotatorClient.batchAnnotateImages(Collections.singletonList(request));
StringBuilder responseBuilder = new StringBuilder("<table border=\"1\">");
responseBuilder.append("<tr><th>description</th><th>score</th></tr>");
// We're only expecting one response.
if (responses.getResponsesCount() == 1) {
AnnotateImageResponse response = responses.getResponses(0);
if (response.hasError()) {
throw new Exception(response.getError().getMessage());
}
for (EntityAnnotation annotation : response.getLabelAnnotationsList()) {
responseBuilder.append("<tr><td>").append(annotation.getDescription()).append("</td><td>").append(annotation.getScore()).append("</td></tr>");
}
}
responseBuilder.append("</table>");
responseBuilder.append("<p><img src='" + imageUrl + "'/></p>");
return responseBuilder.toString();
}
use of com.google.cloud.vision.v1p3beta1.AnnotateImageRequest in project beam by apache.
the class CloudVisionTest method shouldConvertStringToRequest.
@Test
public void shouldConvertStringToRequest() {
CloudVision.AnnotateImagesFromGcsUri annotateImagesFromGcsUri = CloudVision.annotateImagesFromGcsUri(null, features, 1, 1);
AnnotateImageRequest request = annotateImagesFromGcsUri.mapToRequest(TEST_URI, null);
assertEquals(1, request.getFeaturesCount());
assertEquals(TEST_URI, request.getImage().getSource().getGcsImageUri());
}
use of com.google.cloud.vision.v1p3beta1.AnnotateImageRequest in project beam by apache.
the class CloudVisionTest method shouldConvertKVOfBytesToRequest.
@Test
public void shouldConvertKVOfBytesToRequest() {
CloudVision.AnnotateImagesFromBytesWithContext annotateImagesFromBytesWithContext = CloudVision.annotateImagesFromBytesWithContext(features, 1, 1);
AnnotateImageRequest request = annotateImagesFromBytesWithContext.mapToRequest(KV.of(TEST_BYTES, null), null);
assertEquals(1, request.getFeaturesCount());
assertEquals(TEST_BYTES, request.getImage().getContent());
}
use of com.google.cloud.vision.v1p3beta1.AnnotateImageRequest in project beam by apache.
the class CloudVisionTest method shouldConvertByteStringToRequest.
@Test
public void shouldConvertByteStringToRequest() {
CloudVision.AnnotateImagesFromBytes annotateImagesFromBytes = CloudVision.annotateImagesFromBytes(null, features, 1, 1);
AnnotateImageRequest request = annotateImagesFromBytes.mapToRequest(TEST_BYTES, null);
assertEquals(1, request.getFeaturesCount());
assertEquals(TEST_BYTES, request.getImage().getContent());
}
Aggregations