AWS Textract - GetDocumentAnalysisRequest only returns correct results for first page of document

Viewed 630

I have written code to extract tables and name value pairs from pdf using Amazon Textract. I followed this example: https://docs.aws.amazon.com/textract/latest/dg/async-analyzing-with-sqs.html which was in sdk for java version 1.1. I have refactored it for version 2.

This is an async process that only applies to multi page documents. When i get back the results it is pretty accurate for first page. But the consecutive pages are mostly empty rows. The documents i parse are scanned so the quality is not great. However if i take a jpg of individual pages and use the one page operation, i.e. AnalyzeDocumentRequest, each page comes out good. Also Amazon Textract tryit service renders the pages correctly.

So the error must be in my code but can't see where. As you see it all happens in here :

    GetDocumentAnalysisRequest documentAnalysisRequest = GetDocumentAnalysisRequest.builder().jobId(jobId)
                    .maxResults(maxResults).nextToken(paginationToken).build();

            response = textractClient.getDocumentAnalysis(documentAnalysisRequest);

and i can't really do any intervention.

The most likely place I could make a mistake would be in the util file that gathers the page and table blocks i.e. here:

  PageModel pageModel = tableUtil.getTableResults(blocks);

But that works perfectly for the first page, and i could also see in the response object above, that the number of blocks returned are much less.

Here is the full code:


private DocumentModel getDocumentAnalysisResults(String jobId) throws Exception {

        int maxResults = 1000;
        String paginationToken = null;
        GetDocumentAnalysisResponse response = null;
        Boolean finished = false;

        int pageCount = 0;
        DocumentModel documentModel = new DocumentModel();
        // loops until pagination token is null
        while (finished == false) {
            GetDocumentAnalysisRequest documentAnalysisRequest = GetDocumentAnalysisRequest.builder().jobId(jobId)
                    .maxResults(maxResults).nextToken(paginationToken).build();

            response = textractClient.getDocumentAnalysis(documentAnalysisRequest);

            // Show blocks, confidence and detection times
            List<Block> blocks = response.blocks();
            PageModel pageModel = tableUtil.getTableResults(blocks);
            pageModel.setPageNumber(pageCount++);
            Map<String,String> keyValues = formUtil.getFormResults(blocks);
            pageModel.setKeyValues(keyValues);
            documentModel.getPages().add(pageModel);
            paginationToken = response.nextToken();
            if (paginationToken == null)
                finished = true;
        }
        return documentModel;

    }

Has anyone else encountered this issue?

Many thanks

1 Answers
Related