Search in sources :

Example 1 with GoogleStorageLocation

use of com.google.cloud.spring.storage.GoogleStorageLocation in project spring-cloud-gcp by GoogleCloudPlatform.

the class DocumentOcrTemplateTests method testValidateGcsFileInputs.

@Test
void testValidateGcsFileInputs() {
    GoogleStorageLocation folder = GoogleStorageLocation.forFolder("bucket", "path/to/folder/");
    assertThatThrownBy(() -> this.documentOcrTemplate.runOcrForDocument(folder, folder)).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("Provided document location is not a valid file location");
    assertThatThrownBy(() -> this.documentOcrTemplate.readOcrOutputFile(folder)).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("Provided jsonOutputFile location is not a valid file location");
}
Also used : GoogleStorageLocation(com.google.cloud.spring.storage.GoogleStorageLocation) Test(org.junit.jupiter.api.Test)

Example 2 with GoogleStorageLocation

use of com.google.cloud.spring.storage.GoogleStorageLocation in project spring-cloud-gcp by GoogleCloudPlatform.

the class DocumentOcrTemplateIntegrationTests method testDocumentOcrTemplate.

@Test
void testDocumentOcrTemplate() throws ExecutionException, InterruptedException, InvalidProtocolBufferException, TimeoutException {
    GoogleStorageLocation document = GoogleStorageLocation.forFile("vision-integration-test-bucket", "test.pdf");
    GoogleStorageLocation outputLocationPrefix = GoogleStorageLocation.forFile("vision-integration-test-bucket", "it_output/test-");
    ListenableFuture<DocumentOcrResultSet> result = this.documentOcrTemplate.runOcrForDocument(document, outputLocationPrefix);
    DocumentOcrResultSet ocrPages = result.get(5, TimeUnit.MINUTES);
    String page1Text = ocrPages.getPage(1).getText();
    assertThat(page1Text).contains("Hello World. Is mayonnaise an instrument?");
    String page2Text = ocrPages.getPage(2).getText();
    assertThat(page2Text).contains("Page 2 stuff");
    ArrayList<String> pageContent = new ArrayList<>();
    Iterator<TextAnnotation> pageIterator = ocrPages.getAllPages();
    while (pageIterator.hasNext()) {
        pageContent.add(pageIterator.next().getText());
    }
    assertThat(pageContent).containsExactly("Hello World. Is mayonnaise an instrument?\n", "Page 2 stuff\n", "Page 3 stuff\n", "Page 4 stuff\n");
}
Also used : DocumentOcrResultSet(com.google.cloud.spring.vision.DocumentOcrResultSet) ArrayList(java.util.ArrayList) GoogleStorageLocation(com.google.cloud.spring.storage.GoogleStorageLocation) TextAnnotation(com.google.cloud.vision.v1.TextAnnotation) Test(org.junit.jupiter.api.Test)

Example 3 with GoogleStorageLocation

use of com.google.cloud.spring.storage.GoogleStorageLocation in project spring-cloud-gcp by GoogleCloudPlatform.

the class WebController method submitDocument.

@PostMapping("/submitDocument")
public ModelAndView submitDocument(@RequestParam("documentUrl") String documentUrl) throws IOException {
    // Uploads the document to the GCS bucket
    Resource documentResource = resourceLoader.getResource(documentUrl);
    BlobId outputBlobId = BlobId.of(ocrBucket, documentResource.getFilename());
    BlobInfo blobInfo = BlobInfo.newBuilder(outputBlobId).setContentType(getFileType(documentResource)).build();
    try (WriteChannel writer = storage.writer(blobInfo)) {
        ByteStreams.copy(documentResource.getInputStream(), Channels.newOutputStream(writer));
    }
    // Run OCR on the document
    GoogleStorageLocation documentLocation = GoogleStorageLocation.forFile(outputBlobId.getBucket(), outputBlobId.getName());
    GoogleStorageLocation outputLocation = GoogleStorageLocation.forFolder(outputBlobId.getBucket(), "ocr_results/" + documentLocation.getBlobName());
    ListenableFuture<DocumentOcrResultSet> result = documentOcrTemplate.runOcrForDocument(documentLocation, outputLocation);
    ocrStatusReporter.registerFuture(documentLocation.uriString(), result);
    return new ModelAndView("submit_done");
}
Also used : DocumentOcrResultSet(com.google.cloud.spring.vision.DocumentOcrResultSet) WriteChannel(com.google.cloud.WriteChannel) Resource(org.springframework.core.io.Resource) ModelAndView(org.springframework.web.servlet.ModelAndView) BlobInfo(com.google.cloud.storage.BlobInfo) GoogleStorageLocation(com.google.cloud.spring.storage.GoogleStorageLocation) BlobId(com.google.cloud.storage.BlobId) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 4 with GoogleStorageLocation

use of com.google.cloud.spring.storage.GoogleStorageLocation 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 5 with GoogleStorageLocation

use of com.google.cloud.spring.storage.GoogleStorageLocation in project spring-cloud-gcp by GoogleCloudPlatform.

the class DocumentOcrTemplateIntegrationTests method testParseOcrFile.

@Test
void testParseOcrFile() throws InvalidProtocolBufferException {
    GoogleStorageLocation ocrOutputFile = GoogleStorageLocation.forFile("vision-integration-test-bucket", "json_output_set/test_output-2-to-2.json");
    DocumentOcrResultSet pages = this.documentOcrTemplate.readOcrOutputFile(ocrOutputFile);
    String text = pages.getPage(2).getText();
    assertThat(text).contains("Hello World. Is mayonnaise an instrument?");
}
Also used : DocumentOcrResultSet(com.google.cloud.spring.vision.DocumentOcrResultSet) GoogleStorageLocation(com.google.cloud.spring.storage.GoogleStorageLocation) Test(org.junit.jupiter.api.Test)

Aggregations

GoogleStorageLocation (com.google.cloud.spring.storage.GoogleStorageLocation)6 DocumentOcrResultSet (com.google.cloud.spring.vision.DocumentOcrResultSet)4 Test (org.junit.jupiter.api.Test)4 WriteChannel (com.google.cloud.WriteChannel)1 BlobId (com.google.cloud.storage.BlobId)1 BlobInfo (com.google.cloud.storage.BlobInfo)1 AsyncBatchAnnotateFilesResponse (com.google.cloud.vision.v1.AsyncBatchAnnotateFilesResponse)1 TextAnnotation (com.google.cloud.vision.v1.TextAnnotation)1 ArrayList (java.util.ArrayList)1 Resource (org.springframework.core.io.Resource)1 SettableListenableFuture (org.springframework.util.concurrent.SettableListenableFuture)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1