Search in sources :

Example 16 with Result

use of com.google.cloud.vision.v1.ProductSearchResults.Result in project spring-cloud-gcp by GoogleCloudPlatform.

the class DocumentOcrTemplate method extractOcrResultFuture.

private ListenableFuture<DocumentOcrResultSet> extractOcrResultFuture(OperationFuture<AsyncBatchAnnotateFilesResponse, OperationMetadata> grpcFuture) {
    SettableListenableFuture<DocumentOcrResultSet> result = new SettableListenableFuture<>();
    ApiFutures.addCallback(grpcFuture, new ApiFutureCallback<AsyncBatchAnnotateFilesResponse>() {

        @Override
        public void onFailure(Throwable throwable) {
            result.setException(throwable);
        }

        @Override
        public void onSuccess(AsyncBatchAnnotateFilesResponse asyncBatchAnnotateFilesResponse) {
            String outputLocationUri = asyncBatchAnnotateFilesResponse.getResponsesList().get(0).getOutputConfig().getGcsDestination().getUri();
            GoogleStorageLocation outputFolderLocation = new GoogleStorageLocation(outputLocationUri);
            result.set(readOcrOutputFileSet(outputFolderLocation));
        }
    }, this.executor);
    return result;
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) AsyncBatchAnnotateFilesResponse(com.google.cloud.vision.v1.AsyncBatchAnnotateFilesResponse) GoogleStorageLocation(com.google.cloud.spring.storage.GoogleStorageLocation)

Example 17 with Result

use of com.google.cloud.vision.v1.ProductSearchResults.Result in project spring-cloud-gcp by GoogleCloudPlatform.

the class CloudVisionTemplate method extractTextFromFile.

/**
 * Extract the text out of a file and return the result as a String.
 *
 * @param fileResource the file one wishes to analyze
 * @param mimeType the mime type of the fileResource. Currently, only "application/pdf",
 *     "image/tiff" and "image/gif" are supported.
 * @return the text extracted from the pdf as a string per page
 * @throws CloudVisionException if the image could not be read or if text extraction failed
 */
public List<String> extractTextFromFile(Resource fileResource, String mimeType) {
    AnnotateFileResponse response = analyzeFile(fileResource, mimeType, Type.DOCUMENT_TEXT_DETECTION);
    List<AnnotateImageResponse> annotateImageResponses = response.getResponsesList();
    if (annotateImageResponses.isEmpty()) {
        throw new CloudVisionException(EMPTY_RESPONSE_ERROR_MESSAGE);
    }
    List<String> result = annotateImageResponses.stream().map(annotateImageResponse -> annotateImageResponse.getFullTextAnnotation().getText()).collect(Collectors.toList());
    if (result.isEmpty() && response.getError().getCode() != Code.OK.getNumber()) {
        throw new CloudVisionException(response.getError().getMessage());
    }
    return result;
}
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) AnnotateFileResponse(com.google.cloud.vision.v1.AnnotateFileResponse) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ByteString(com.google.protobuf.ByteString)

Example 18 with Result

use of com.google.cloud.vision.v1.ProductSearchResults.Result 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)

Example 19 with Result

use of com.google.cloud.vision.v1.ProductSearchResults.Result in project java-vision by googleapis.

the class ProductSearch method getSimilarProductsFile.

// [START vision_product_search_get_similar_products]
/**
 * Search similar products to image in local file.
 *
 * @param projectId - Id of the project.
 * @param computeRegion - Region name.
 * @param productSetId - Id of the product set.
 * @param productCategory - Category of the product.
 * @param filePath - Local file path of the image to be searched
 * @param filter - Condition to be applied on the labels. Example for filter: (color = red OR
 *     color = blue) AND style = kids It will search on all products with the following labels:
 *     color:red AND style:kids color:blue AND style:kids
 * @throws IOException - on I/O errors.
 */
public static void getSimilarProductsFile(String projectId, String computeRegion, String productSetId, String productCategory, String filePath, String filter) throws IOException {
    try (ImageAnnotatorClient queryImageClient = ImageAnnotatorClient.create()) {
        // Get the full path of the product set.
        String productSetPath = ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId);
        // Read the image as a stream of bytes.
        File imgPath = new File(filePath);
        byte[] content = Files.readAllBytes(imgPath.toPath());
        // Create annotate image request along with product search feature.
        Feature featuresElement = Feature.newBuilder().setType(Type.PRODUCT_SEARCH).build();
        // The input image can be a HTTPS link or Raw image bytes.
        // Example:
        // To use HTTP link replace with below code
        // ImageSource source = ImageSource.newBuilder().setImageUri(imageUri).build();
        // Image image = Image.newBuilder().setSource(source).build();
        Image image = Image.newBuilder().setContent(ByteString.copyFrom(content)).build();
        ImageContext imageContext = ImageContext.newBuilder().setProductSearchParams(ProductSearchParams.newBuilder().setProductSet(productSetPath).addProductCategories(productCategory).setFilter(filter)).build();
        AnnotateImageRequest annotateImageRequest = AnnotateImageRequest.newBuilder().addFeatures(featuresElement).setImage(image).setImageContext(imageContext).build();
        List<AnnotateImageRequest> requests = Arrays.asList(annotateImageRequest);
        // Search products similar to the image.
        BatchAnnotateImagesResponse response = queryImageClient.batchAnnotateImages(requests);
        List<Result> similarProducts = response.getResponses(0).getProductSearchResults().getResultsList();
        System.out.println("Similar Products: ");
        for (Result product : similarProducts) {
            System.out.println(String.format("\nProduct name: %s", product.getProduct().getName()));
            System.out.println(String.format("Product display name: %s", product.getProduct().getDisplayName()));
            System.out.println(String.format("Product description: %s", product.getProduct().getDescription()));
            System.out.println(String.format("Score(Confidence): %s", product.getScore()));
            System.out.println(String.format("Image name: %s", product.getImage()));
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ByteString(com.google.protobuf.ByteString) Image(com.google.cloud.vision.v1.Image) File(java.io.File) Feature(com.google.cloud.vision.v1.Feature) ImageContext(com.google.cloud.vision.v1.ImageContext) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) Result(com.google.cloud.vision.v1.ProductSearchResults.Result)

Example 20 with Result

use of com.google.cloud.vision.v1.ProductSearchResults.Result in project java-vision by googleapis.

the class Detect method detectDocumentsGcs.

// [END vision_fulltext_detection_gcs]
// [START vision_text_detection_pdf_gcs]
/**
 * Performs document text OCR with PDF/TIFF as source files on Google Cloud Storage.
 *
 * @param gcsSourcePath The path to the remote file on Google Cloud Storage to detect document
 *     text on.
 * @param gcsDestinationPath The path to the remote file on Google Cloud Storage to store the
 *     results on.
 * @throws Exception on errors while closing the client.
 */
public static void detectDocumentsGcs(String gcsSourcePath, String gcsDestinationPath) throws Exception {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        List<AsyncAnnotateFileRequest> requests = new ArrayList<>();
        // Set the GCS source path for the remote file.
        GcsSource gcsSource = GcsSource.newBuilder().setUri(gcsSourcePath).build();
        // Create the configuration with the specified MIME (Multipurpose Internet Mail Extensions)
        // types
        InputConfig inputConfig = InputConfig.newBuilder().setMimeType(// Supported MimeTypes: "application/pdf", "image/tiff"
        "application/pdf").setGcsSource(gcsSource).build();
        // Set the GCS destination path for where to save the results.
        GcsDestination gcsDestination = GcsDestination.newBuilder().setUri(gcsDestinationPath).build();
        // Create the configuration for the System.output with the batch size.
        // The batch size sets how many pages should be grouped into each json System.output file.
        OutputConfig outputConfig = OutputConfig.newBuilder().setBatchSize(2).setGcsDestination(gcsDestination).build();
        // Select the Feature required by the vision API
        Feature feature = Feature.newBuilder().setType(Feature.Type.DOCUMENT_TEXT_DETECTION).build();
        // Build the OCR request
        AsyncAnnotateFileRequest request = AsyncAnnotateFileRequest.newBuilder().addFeatures(feature).setInputConfig(inputConfig).setOutputConfig(outputConfig).build();
        requests.add(request);
        // Perform the OCR request
        OperationFuture<AsyncBatchAnnotateFilesResponse, OperationMetadata> response = client.asyncBatchAnnotateFilesAsync(requests);
        System.out.println("Waiting for the operation to finish.");
        // Wait for the request to finish. (The result is not used, since the API saves the result to
        // the specified location on GCS.)
        List<AsyncAnnotateFileResponse> result = response.get(180, TimeUnit.SECONDS).getResponsesList();
        // Once the request has completed and the System.output has been
        // written to GCS, we can list all the System.output files.
        Storage storage = StorageOptions.getDefaultInstance().getService();
        // Get the destination location from the gcsDestinationPath
        Pattern pattern = Pattern.compile("gs://([^/]+)/(.+)");
        Matcher matcher = pattern.matcher(gcsDestinationPath);
        if (matcher.find()) {
            String bucketName = matcher.group(1);
            String prefix = matcher.group(2);
            // Get the list of objects with the given prefix from the GCS bucket
            Bucket bucket = storage.get(bucketName);
            com.google.api.gax.paging.Page<Blob> pageList = bucket.list(BlobListOption.prefix(prefix));
            Blob firstOutputFile = null;
            // List objects with the given prefix.
            System.out.println("Output files:");
            for (Blob blob : pageList.iterateAll()) {
                System.out.println(blob.getName());
                // the first two pages of the input file.
                if (firstOutputFile == null) {
                    firstOutputFile = blob;
                }
            }
            // Get the contents of the file and convert the JSON contents to an AnnotateFileResponse
            // object. If the Blob is small read all its content in one request
            // (Note: the file is a .json file)
            // Storage guide: https://cloud.google.com/storage/docs/downloading-objects
            String jsonContents = new String(firstOutputFile.getContent());
            Builder builder = AnnotateFileResponse.newBuilder();
            JsonFormat.parser().merge(jsonContents, builder);
            // Build the AnnotateFileResponse object
            AnnotateFileResponse annotateFileResponse = builder.build();
            // Parse through the object to get the actual response for the first page of the input file.
            AnnotateImageResponse annotateImageResponse = annotateFileResponse.getResponses(0);
            // Here we print the full text from the first page.
            // The response contains more information:
            // annotation/pages/blocks/paragraphs/words/symbols
            // including confidence score and bounding boxes
            System.out.format("%nText: %s%n", annotateImageResponse.getFullTextAnnotation().getText());
        } else {
            System.out.println("No MATCH");
        }
    }
}
Also used : GcsSource(com.google.cloud.vision.v1.GcsSource) Matcher(java.util.regex.Matcher) AsyncAnnotateFileResponse(com.google.cloud.vision.v1.AsyncAnnotateFileResponse) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) Builder(com.google.cloud.vision.v1.AnnotateFileResponse.Builder) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Feature(com.google.cloud.vision.v1.Feature) AsyncBatchAnnotateFilesResponse(com.google.cloud.vision.v1.AsyncBatchAnnotateFilesResponse) AnnotateFileResponse(com.google.cloud.vision.v1.AnnotateFileResponse) AsyncAnnotateFileResponse(com.google.cloud.vision.v1.AsyncAnnotateFileResponse) InputConfig(com.google.cloud.vision.v1.InputConfig) OperationMetadata(com.google.cloud.vision.v1.OperationMetadata) Pattern(java.util.regex.Pattern) Blob(com.google.cloud.storage.Blob) OutputConfig(com.google.cloud.vision.v1.OutputConfig) Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) AsyncAnnotateFileRequest(com.google.cloud.vision.v1.AsyncAnnotateFileRequest) GcsDestination(com.google.cloud.vision.v1.GcsDestination)

Aggregations

AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)8 ByteString (com.google.protobuf.ByteString)6 List (java.util.List)6 AsyncBatchAnnotateFilesResponse (com.google.cloud.vision.v1.AsyncBatchAnnotateFilesResponse)5 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)5 Feature (com.google.cloud.vision.v1.Feature)5 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)5 Collectors (java.util.stream.Collectors)5 ModelAndView (org.springframework.web.servlet.ModelAndView)5 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)4 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)4 Image (com.google.cloud.vision.v1.Image)4 InputConfig (com.google.cloud.vision.v1.InputConfig)4 TextAnnotation (com.google.cloud.vision.v1.TextAnnotation)4 AsyncAnnotateFileRequest (com.google.cloud.vision.v1.AsyncAnnotateFileRequest)3 GcsDestination (com.google.cloud.vision.v1.GcsDestination)3 GcsSource (com.google.cloud.vision.v1.GcsSource)3 ImageContext (com.google.cloud.vision.v1.ImageContext)3 OperationMetadata (com.google.cloud.vision.v1.OperationMetadata)3 ArrayList (java.util.ArrayList)3