Search in sources :

Example 26 with GcsSource

use of com.google.cloud.documentai.v1beta2.GcsSource in project java-vision by googleapis.

the class DetectBatchAnnotateFilesGcs method detectBatchAnnotateFilesGcs.

// Performs document feature detection on a remote PDF/TIFF/GIF file on Google Cloud Storage.
public static void detectBatchAnnotateFilesGcs(String gcsPath) {
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        // Annotate the first two pages and the last one (max 5 pages)
        // First page starts at 1, and not 0. Last page is -1.
        List<Integer> pages = Arrays.asList(1, 2, -1);
        GcsSource gcsSource = GcsSource.newBuilder().setUri(gcsPath).build();
        Feature feat = Feature.newBuilder().setType(Type.DOCUMENT_TEXT_DETECTION).build();
        // Other supported mime types : 'image/tiff' or 'image/gif'
        InputConfig inputConfig = InputConfig.newBuilder().setMimeType("application/pdf").setGcsSource(gcsSource).build();
        AnnotateFileRequest request = AnnotateFileRequest.newBuilder().addFeatures(feat).setInputConfig(inputConfig).addAllPages(pages).build();
        List<AnnotateFileRequest> requests = new ArrayList<>();
        requests.add(request);
        BatchAnnotateFilesRequest batchAnnotateFilesRequest = BatchAnnotateFilesRequest.newBuilder().addAllRequests(requests).build();
        ApiFuture<BatchAnnotateFilesResponse> future = client.batchAnnotateFilesCallable().futureCall(batchAnnotateFilesRequest);
        BatchAnnotateFilesResponse response = future.get();
        // Getting the first response
        AnnotateFileResponse annotateFileResponse = response.getResponses(0);
        // For full list of available annotations, see http://g.co/cloud/vision/docs
        TextAnnotation textAnnotation = annotateFileResponse.getResponses(0).getFullTextAnnotation();
        for (Page page : textAnnotation.getPagesList()) {
            String pageText = "";
            for (Block block : page.getBlocksList()) {
                String blockText = "";
                for (Paragraph para : block.getParagraphsList()) {
                    String paraText = "";
                    for (Word word : para.getWordsList()) {
                        String wordText = "";
                        for (Symbol symbol : word.getSymbolsList()) {
                            wordText = wordText + symbol.getText();
                            System.out.format("Symbol text: %s (Confidence: %f)\n", symbol.getText(), symbol.getConfidence());
                        }
                        System.out.format("Word text: %s (Confidence: %f)\n\n", wordText, word.getConfidence());
                        paraText = String.format("%s %s", paraText, wordText);
                    }
                    // Output Example using Paragraph:
                    System.out.println("\nParagraph: \n" + paraText);
                    System.out.format("Paragraph Confidence: %f\n", para.getConfidence());
                    blockText = blockText + paraText;
                }
                pageText = pageText + blockText;
            }
        }
        System.out.println("\nComplete annotation:");
        System.out.println(textAnnotation.getText());
    } catch (Exception e) {
        System.out.println("Error during detectPdfText: \n" + e.toString());
    }
}
Also used : GcsSource(com.google.cloud.vision.v1p4beta1.GcsSource) Word(com.google.cloud.vision.v1p4beta1.Word) BatchAnnotateFilesRequest(com.google.cloud.vision.v1p4beta1.BatchAnnotateFilesRequest) Symbol(com.google.cloud.vision.v1p4beta1.Symbol) ImageAnnotatorClient(com.google.cloud.vision.v1p4beta1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) Page(com.google.cloud.vision.v1p4beta1.Page) Feature(com.google.cloud.vision.v1p4beta1.Feature) Paragraph(com.google.cloud.vision.v1p4beta1.Paragraph) BatchAnnotateFilesResponse(com.google.cloud.vision.v1p4beta1.BatchAnnotateFilesResponse) AnnotateFileResponse(com.google.cloud.vision.v1p4beta1.AnnotateFileResponse) AnnotateFileRequest(com.google.cloud.vision.v1p4beta1.AnnotateFileRequest) Block(com.google.cloud.vision.v1p4beta1.Block) InputConfig(com.google.cloud.vision.v1p4beta1.InputConfig) TextAnnotation(com.google.cloud.vision.v1p4beta1.TextAnnotation)

Aggregations

Document (com.google.cloud.documentai.v1beta2.Document)7 DocumentUnderstandingServiceClient (com.google.cloud.documentai.v1beta2.DocumentUnderstandingServiceClient)7 GcsSource (com.google.cloud.documentai.v1beta2.GcsSource)7 InputConfig (com.google.cloud.documentai.v1beta2.InputConfig)7 ProcessDocumentRequest (com.google.cloud.documentai.v1beta2.ProcessDocumentRequest)7 GcsSource (com.google.cloud.translate.v3.GcsSource)5 LocationName (com.google.cloud.translate.v3.LocationName)5 TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)5 BatchTranslateMetadata (com.google.cloud.translate.v3.BatchTranslateMetadata)4 BatchTranslateResponse (com.google.cloud.translate.v3.BatchTranslateResponse)4 BatchTranslateTextRequest (com.google.cloud.translate.v3.BatchTranslateTextRequest)4 GcsDestination (com.google.cloud.translate.v3.GcsDestination)4 InputConfig (com.google.cloud.translate.v3.InputConfig)4 OutputConfig (com.google.cloud.translate.v3.OutputConfig)4 GcsSource (com.google.cloud.automl.v1.GcsSource)3 GcsSource (com.google.cloud.automl.v1beta1.GcsSource)3 GlossaryName (com.google.cloud.translate.v3.GlossaryName)3 GcsSource (com.google.cloud.translate.v3beta1.GcsSource)3 LocationName (com.google.cloud.translate.v3beta1.LocationName)3 TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)3