Extract receipt text

Now that we have the upload and archiving part, it's time to use OCR and read the expense amount and date from receipt photos. But what's a good OCR service for our serverless application?

We can connect a serverless application to almost any third-party service. However, if we are not careful, we can hit a rate limit or crash the third-party service we use. Also, we need to make sure our app has all the permissions to talk to the external application.

As you can guess, there's an AWS service for that. As part of its rich AI and ML offering, AWS offers Amazon Textract, a service that automatically extracts text and data from scanned documents. Textract goes beyond simple optical character recognition (OCR) to also identify the contents of fields in forms and information stored in tables.

Task

Your task in this exercise is to send a photo customer uploads to Amazon Textract, get the data, and store the expense details to our DynamoDB table.

To do that you'll need the following steps:

  1. Use AWS SDK to upload a photo to Amazon Textract.
  2. Get the data, and extract the date and amount info.
  3. Save the amount and date to the DynamoDB table.

Once you complete this exercise, take a minute and discuss the limitations of your solution with your team. Here are a few questions you can try to answer:

  • How does Amazon Textract affects function duration?
  • What would be more optimal solution?

Hint

Here are a few hints to help you with this task:

  1. Use the textract.analyzeDocument method of AWS SDK to analyze the document.
  2. You can enable or disable form and table recognition using FeatureTypes parameter of the textract.analyzeDocument method.
  3. Make sure your function has permission to analyze the document using Amazon Textract. There's no managed SAM policy template for Textract, but you can add a required permission manually.
Need more help? Here are some code samples.

Here's a piece of Node.js code that invokes Amazon Textract to analyze the document:

async function getReceiptData(fileBuffer, textract) {
  const params = {
    Document: {
      Bytes: fileBuffer // An uploaded photo as a buffer
    },
    FeatureTypes: ['FORMS', 'TABLES'] // Get both forms and tables
  };

  const response = await textract.analyzeDocument(params).promise();
  // Get the data from the Textract result
  const receiptData = extractData(response);
  return receiptData;
}

results matching ""

    No results matching ""