Search in sources :

Example 76 with BatchAnnotateImagesResponse

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);
    }
}
Also used : AnnotateFileRequest(com.google.cloud.vision.v1.AnnotateFileRequest) Arrays(java.util.Arrays) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) Type(com.google.cloud.vision.v1.Feature.Type) BatchAnnotateFilesRequest(com.google.cloud.vision.v1.BatchAnnotateFilesRequest) IOException(java.io.IOException) InputConfig(com.google.cloud.vision.v1.InputConfig) Collectors(java.util.stream.Collectors) Feature(com.google.cloud.vision.v1.Feature) ByteString(com.google.protobuf.ByteString) List(java.util.List) AnnotateFileResponse(com.google.cloud.vision.v1.AnnotateFileResponse) BatchAnnotateFilesResponse(com.google.cloud.vision.v1.BatchAnnotateFilesResponse) Image(com.google.cloud.vision.v1.Image) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ImageContext(com.google.cloud.vision.v1.ImageContext) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) BatchAnnotateImagesRequest(com.google.cloud.vision.v1.BatchAnnotateImagesRequest) Code(com.google.rpc.Code) Resource(org.springframework.core.io.Resource) Assert(org.springframework.util.Assert) BatchAnnotateImagesRequest(com.google.cloud.vision.v1.BatchAnnotateImagesRequest) ByteString(com.google.protobuf.ByteString) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) IOException(java.io.IOException) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 77 with BatchAnnotateImagesResponse

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);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) Map(java.util.Map) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ImageSource(com.google.cloud.vision.v1.ImageSource) Iterator(java.util.Iterator) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) FeatureType(org.tweetwallfx.google.vision.CloudVisionSettings.FeatureType) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Cache(org.ehcache.Cache) Collectors(java.util.stream.Collectors) Feature(com.google.cloud.vision.v1.Feature) GoogleSettings(org.tweetwallfx.google.GoogleSettings) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) ImageAnnotatorSettings(com.google.cloud.vision.v1.ImageAnnotatorSettings) Image(com.google.cloud.vision.v1.Image) Configuration(org.tweetwallfx.config.Configuration) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) CacheManagerProvider(org.tweetwallfx.cache.CacheManagerProvider) LinkedHashMap(java.util.LinkedHashMap) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) Objects(java.util.Objects) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Aggregations

BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)72 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)69 Image (com.google.cloud.vision.v1.Image)68 Feature (com.google.cloud.vision.v1.Feature)66 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)64 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)63 ArrayList (java.util.ArrayList)63 ByteString (com.google.protobuf.ByteString)44 ImageSource (com.google.cloud.vision.v1.ImageSource)37 FileInputStream (java.io.FileInputStream)32 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)28 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)26 IOException (java.io.IOException)13 ImageContext (com.google.cloud.vision.v1.ImageContext)12 WebDetection (com.google.cloud.vision.v1.WebDetection)11 LocationInfo (com.google.cloud.vision.v1.LocationInfo)10 SafeSearchAnnotation (com.google.cloud.vision.v1.SafeSearchAnnotation)10 WebPage (com.google.cloud.vision.v1.WebDetection.WebPage)9 Block (com.google.cloud.vision.v1.Block)8 ColorInfo (com.google.cloud.vision.v1.ColorInfo)8