use of com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse in project spring-cloud-gcp by GoogleCloudPlatform.
the class CloudVisionTemplate method analyzeImage.
/**
* Analyze an image and extract the features of the image specified by {@code featureTypes}.
*
* <p>A feature describes the kind of Cloud Vision analysis one wishes to perform on an image,
* such as text detection, image labelling, facial detection, etc. A full list of feature types
* can be found in {@link Feature.Type}.
*
* @param imageResource the image one wishes to analyze. The Cloud Vision APIs support image
* formats described here: https://cloud.google.com/vision/docs/supported-files
* @param imageContext the image context used to customize the Vision API request
* @param featureTypes the types of image analysis to perform on the image
* @return the results of image analyses
* @throws CloudVisionException if the image could not be read or if a malformed response is
* received from the Cloud Vision APIs
*/
public AnnotateImageResponse analyzeImage(Resource imageResource, ImageContext imageContext, Feature.Type... featureTypes) {
ByteString imgBytes;
try {
imgBytes = ByteString.readFrom(imageResource.getInputStream());
} catch (IOException ex) {
throw new CloudVisionException(READ_BYTES_ERROR_MESSAGE, ex);
}
Image image = Image.newBuilder().setContent(imgBytes).build();
List<Feature> featureList = Arrays.stream(featureTypes).map(featureType -> Feature.newBuilder().setType(featureType).build()).collect(Collectors.toList());
BatchAnnotateImagesRequest request = BatchAnnotateImagesRequest.newBuilder().addRequests(AnnotateImageRequest.newBuilder().addAllFeatures(featureList).setImageContext(imageContext).setImage(image)).build();
BatchAnnotateImagesResponse batchResponse = this.imageAnnotatorClient.batchAnnotateImages(request);
List<AnnotateImageResponse> annotateImageResponses = batchResponse.getResponsesList();
if (!annotateImageResponses.isEmpty()) {
return annotateImageResponses.get(0);
} else {
throw new CloudVisionException(EMPTY_RESPONSE_ERROR_MESSAGE);
}
}
use of com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse in project TweetwallFX by TweetWallFX.
the class GoogleVisionCache method load.
private Map<String, ImageContentAnalysis> load(final Stream<String> imageUris) throws IOException {
if (null == getClient()) {
return Collections.emptyMap();
}
final List<AnnotateImageRequest> requests = imageUris.filter(Objects::nonNull).distinct().map(this::createImageRequest).peek(air -> LOG.info("Prepared {}", air)).collect(Collectors.toList());
if (requests.isEmpty()) {
return Collections.emptyMap();
}
LOG.info("Executing analysis for {} AnnotateImageRequests", requests.size());
final BatchAnnotateImagesResponse batchResponse = getClient().batchAnnotateImages(requests);
final Iterator<AnnotateImageResponse> itResponse = batchResponse.getResponsesList().iterator();
final Iterator<AnnotateImageRequest> itRequest = requests.iterator();
final Map<String, ImageContentAnalysis> result = new LinkedHashMap<>(requests.size());
while (itRequest.hasNext() && itResponse.hasNext()) {
final AnnotateImageRequest request = itRequest.next();
final AnnotateImageResponse response = itResponse.next();
final String uri = request.getImage().getSource().getImageUri();
final ImageContentAnalysis ica = new ImageContentAnalysis(response);
LOG.info("Image('{}') was evaluated as {}", uri, ica);
result.put(uri, ica);
cache.put(uri, ica);
}
if (itRequest.hasNext()) {
throw new IllegalStateException("There are still annotate Responses available!");
} else if (itRequest.hasNext()) {
throw new IllegalStateException("There are still annotate Requests available!");
} else {
return Collections.unmodifiableMap(result);
}
}
Aggregations