use of org.springframework.cloud.gcp.storage.GoogleStorageLocation in project spring-cloud-gcp by spring-cloud.
the class DocumentOcrTemplateIntegrationTests method testParseOcrFile.
@Test
public 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?");
}
use of org.springframework.cloud.gcp.storage.GoogleStorageLocation in project spring-cloud-gcp by spring-cloud.
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");
}
use of org.springframework.cloud.gcp.storage.GoogleStorageLocation in project spring-cloud-gcp by spring-cloud.
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;
}
use of org.springframework.cloud.gcp.storage.GoogleStorageLocation in project spring-cloud-gcp by spring-cloud.
the class DocumentOcrTemplateTests method testValidateGcsFileInputs.
@Test
public 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");
}
use of org.springframework.cloud.gcp.storage.GoogleStorageLocation in project spring-cloud-gcp by spring-cloud.
the class DocumentOcrTemplateIntegrationTests method testParseOcrResultSet.
@Test
public void testParseOcrResultSet() throws InvalidProtocolBufferException {
GoogleStorageLocation ocrOutputPrefix = GoogleStorageLocation.forFolder("vision-integration-test-bucket", "json_output_set/");
DocumentOcrResultSet result = this.documentOcrTemplate.readOcrOutputFileSet(ocrOutputPrefix);
String text = result.getPage(2).getText();
assertThat(text).contains("Hello World. Is mayonnaise an instrument?");
}
Aggregations