Search in sources :

Example 1 with Result

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

the class DocumentOcrResultSet method getAllPages.

/**
 * Returns an {@link Iterator} over all the OCR pages of the document.
 *
 * @return iterator of {@link TextAnnotation} describing OCR content of each page in the
 * document.
 */
public Iterator<TextAnnotation> getAllPages() {
    return new Iterator<TextAnnotation>() {

        private final Iterator<OcrPageRange> pageRangeIterator = ocrPageRanges.values().iterator();

        private int offset = 0;

        private List<TextAnnotation> currentPageRange = Collections.EMPTY_LIST;

        @Override
        public boolean hasNext() {
            return pageRangeIterator.hasNext() || offset < currentPageRange.size();
        }

        @Override
        public TextAnnotation next() {
            if (!hasNext()) {
                throw new NoSuchElementException("No more pages left in DocumentOcrResultSet.");
            }
            if (offset >= currentPageRange.size()) {
                OcrPageRange pageRange = pageRangeIterator.next();
                offset = 0;
                try {
                    currentPageRange = pageRange.getPages();
                } catch (InvalidProtocolBufferException e) {
                    throw new RuntimeException("Failed to parse OCR output from JSON output file " + pageRange.getBlob().getName(), e);
                }
            }
            TextAnnotation result = currentPageRange.get(offset);
            offset++;
            return result;
        }
    };
}
Also used : Iterator(java.util.Iterator) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) List(java.util.List) TextAnnotation(com.google.cloud.vision.v1.TextAnnotation) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with Result

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

the class CloudVisionTemplate method extractTextFromImage.

/**
 * Extract the text out of an image and return the result as a String.
 * @param imageResource the image one wishes to analyze
 * @param imageContext the image context to customize the text extraction request
 * @return the text extracted from the image aggregated to a String
 * @throws CloudVisionException if the image could not be read or if text extraction failed
 */
public String extractTextFromImage(Resource imageResource, ImageContext imageContext) {
    AnnotateImageResponse response = analyzeImage(imageResource, imageContext, Type.TEXT_DETECTION);
    String result = response.getFullTextAnnotation().getText();
    if (result.isEmpty() && response.getError().getCode() != Code.OK.getNumber()) {
        throw new CloudVisionException(response.getError().getMessage());
    }
    return result;
}
Also used : AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ByteString(com.google.protobuf.ByteString)

Example 3 with Result

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

the class DocumentOcrTemplate method runOcrForDocument.

/**
 * Runs OCR processing for a specified {@code document} and generates OCR output files
 * under the path specified by {@code outputFilePathPrefix}.
 *
 * <p>
 * For example, if you specify an {@code outputFilePathPrefix} of
 * "gs://bucket_name/ocr_results/myDoc_", all the output files of OCR processing will be
 * saved under prefix, such as:
 *
 * <ul>
 * <li>gs://bucket_name/ocr_results/myDoc_output-1-to-5.json
 * <li>gs://bucket_name/ocr_results/myDoc_output-6-to-10.json
 * <li>gs://bucket_name/ocr_results/myDoc_output-11-to-15.json
 * </ul>
 *
 * <p>
 * Note: OCR processing operations may take several minutes to complete, so it may not be
 * advisable to block on the completion of the operation. One may use the returned
 * {@link ListenableFuture} to register callbacks or track the status of the operation.
 *
 * @param document The {@link GoogleStorageLocation} of the document to run OCR processing
 * @param outputFilePathPrefix The {@link GoogleStorageLocation} of a file, folder, or a
 *     bucket describing the path for which all output files shall be saved under
 *
 * @return A {@link ListenableFuture} allowing you to register callbacks or wait for the
 * completion of the operation.
 */
public ListenableFuture<DocumentOcrResultSet> runOcrForDocument(GoogleStorageLocation document, GoogleStorageLocation outputFilePathPrefix) {
    Assert.isTrue(document.isFile(), "Provided document location is not a valid file location: " + document);
    GcsSource gcsSource = GcsSource.newBuilder().setUri(document.uriString()).build();
    String contentType = extractContentType(document);
    InputConfig inputConfig = InputConfig.newBuilder().setMimeType(contentType).setGcsSource(gcsSource).build();
    GcsDestination gcsDestination = GcsDestination.newBuilder().setUri(outputFilePathPrefix.uriString()).build();
    OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).setBatchSize(this.jsonOutputBatchSize).build();
    AsyncAnnotateFileRequest request = AsyncAnnotateFileRequest.newBuilder().addFeatures(DOCUMENT_OCR_FEATURE).setInputConfig(inputConfig).setOutputConfig(outputConfig).build();
    OperationFuture<AsyncBatchAnnotateFilesResponse, OperationMetadata> result = imageAnnotatorClient.asyncBatchAnnotateFilesAsync(Collections.singletonList(request));
    return extractOcrResultFuture(result);
}
Also used : GcsSource(com.google.cloud.vision.v1.GcsSource) OutputConfig(com.google.cloud.vision.v1.OutputConfig) AsyncBatchAnnotateFilesResponse(com.google.cloud.vision.v1.AsyncBatchAnnotateFilesResponse) InputConfig(com.google.cloud.vision.v1.InputConfig) AsyncAnnotateFileRequest(com.google.cloud.vision.v1.AsyncAnnotateFileRequest) GcsDestination(com.google.cloud.vision.v1.GcsDestination) OperationMetadata(com.google.cloud.vision.v1.OperationMetadata)

Example 4 with Result

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

the class VisionApiSampleApplicationTests method testClassifyImageLabels.

@Test
public void testClassifyImageLabels() throws Exception {
    this.mockMvc.perform(get(LABEL_IMAGE_URL)).andDo((response) -> {
        ModelAndView result = response.getModelAndView();
        List<EntityAnnotation> annotations = (List<EntityAnnotation>) result.getModelMap().get("annotations");
        List<String> annotationNames = annotations.stream().map(annotation -> annotation.getDescription().toLowerCase().trim()).collect(Collectors.toList());
        assertThat(annotationNames).contains("dog");
    });
}
Also used : Assume.assumeThat(org.junit.Assume.assumeThat) BeforeClass(org.junit.BeforeClass) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.junit.Test) Collectors(java.util.stream.Collectors) MockMvc(org.springframework.test.web.servlet.MockMvc) ModelAndView(org.springframework.web.servlet.ModelAndView) List(java.util.List) AutoConfigureMockMvc(org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) MockMvcRequestBuilders.get(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get) Matchers.is(org.hamcrest.Matchers.is) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) SpringRunner(org.springframework.test.context.junit4.SpringRunner) ModelAndView(org.springframework.web.servlet.ModelAndView) List(java.util.List) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with Result

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

the class DocumentOcrTemplate method runOcrForDocument.

/**
 * Runs OCR processing for a specified {@code document} and generates OCR output files under the
 * path specified by {@code outputFilePathPrefix}.
 *
 * <p>For example, if you specify an {@code outputFilePathPrefix} of
 * "gs://bucket_name/ocr_results/myDoc_", all the output files of OCR processing will be saved
 * under prefix, such as:
 *
 * <ul>
 *   <li>gs://bucket_name/ocr_results/myDoc_output-1-to-5.json
 *   <li>gs://bucket_name/ocr_results/myDoc_output-6-to-10.json
 *   <li>gs://bucket_name/ocr_results/myDoc_output-11-to-15.json
 * </ul>
 *
 * <p>Note: OCR processing operations may take several minutes to complete, so it may not be
 * advisable to block on the completion of the operation. One may use the returned {@link
 * ListenableFuture} to register callbacks or track the status of the operation.
 *
 * @param document The {@link GoogleStorageLocation} of the document to run OCR processing
 * @param outputFilePathPrefix The {@link GoogleStorageLocation} of a file, folder, or a bucket
 *     describing the path for which all output files shall be saved under
 * @return A {@link ListenableFuture} allowing you to register callbacks or wait for the
 *     completion of the operation.
 */
public ListenableFuture<DocumentOcrResultSet> runOcrForDocument(GoogleStorageLocation document, GoogleStorageLocation outputFilePathPrefix) {
    Assert.isTrue(document.isFile(), "Provided document location is not a valid file location: " + document);
    GcsSource gcsSource = GcsSource.newBuilder().setUri(document.uriString()).build();
    String contentType = extractContentType(document);
    InputConfig inputConfig = InputConfig.newBuilder().setMimeType(contentType).setGcsSource(gcsSource).build();
    GcsDestination gcsDestination = GcsDestination.newBuilder().setUri(outputFilePathPrefix.uriString()).build();
    OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).setBatchSize(this.jsonOutputBatchSize).build();
    AsyncAnnotateFileRequest request = AsyncAnnotateFileRequest.newBuilder().addFeatures(DOCUMENT_OCR_FEATURE).setInputConfig(inputConfig).setOutputConfig(outputConfig).build();
    OperationFuture<AsyncBatchAnnotateFilesResponse, OperationMetadata> result = imageAnnotatorClient.asyncBatchAnnotateFilesAsync(Collections.singletonList(request));
    return extractOcrResultFuture(result);
}
Also used : GcsSource(com.google.cloud.vision.v1.GcsSource) OutputConfig(com.google.cloud.vision.v1.OutputConfig) AsyncBatchAnnotateFilesResponse(com.google.cloud.vision.v1.AsyncBatchAnnotateFilesResponse) InputConfig(com.google.cloud.vision.v1.InputConfig) AsyncAnnotateFileRequest(com.google.cloud.vision.v1.AsyncAnnotateFileRequest) GcsDestination(com.google.cloud.vision.v1.GcsDestination) OperationMetadata(com.google.cloud.vision.v1.OperationMetadata)

Aggregations

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