Receipt OCR Node.js

The Node.js OCR SDK supports the receipt API for extracting data from receipts.

Using this sample receipt below, we are going to illustrate how to extract the data that we want using the OCR SDK.
sample receiptsample receipt

Quick Start

import { Client,  ReceiptResponse } from "mindee";

// Init a new client
const mindeeClient = new Client({ apiKey: "my-api-key" });

// Load a file from disk and parse it
const doc = mindeeClient.docFromPath("./ffc127d-sample_receipt.jpg");
const respPromise = doc.parse(ReceiptResponse);

// Print a summary of the parsed data
respPromise.then((resp) => {
  if (resp.document === undefined) return;

  console.log(resp.document.toString());
});

Output:

-----Receipt data-----
Total amount including taxes: 10.2
Total amount excluding taxes: 8.5
Date: 2016-02-26
Category: food
Time: 15:20
Merchant name: CLACHAN
Taxes: 1.7 20.0%
Total taxes: 1.7
Locale: en-GB; en; GB; GBP;
---------------------

Field Objects

Each Field object contains at a minimum the following attributes:

  • value (string or number depending on the field type):
    Corresponds to the field value. Can be null if no value was extracted.
  • confidence (Float):
    The confidence score of the field prediction.
  • bbox (Array< Array< Float > >):
    Contains exactly 4 relative vertices coordinates (points) of a right rectangle containing the field in the document.
  • polygon (Array< Array< Float > >):
    Contains the relative vertices coordinates (points) of a polygon containing the field in the image.
  • reconstructed (Boolean):
    True if the field was reconstructed or computed using other fields.

Extracted Fields

Attributes that will be extracted from the document and available in the Receipt object:

Orientation

  • orientation (Orientation): The orientation field is only available at the page level as it describes whether the page image should be rotated to be upright.

If the page requires rotation for correct display, the orientation field gives a prediction among these 3 possible outputs:

  • 0 degrees: the page is already upright
  • 90 degrees: the page must be rotated clockwise to be upright
  • 270 degrees: the page must be rotated counterclockwise to be upright
// Show the orientation of each page
resp.pages.forEach((page) => {
  console.log(page.orientation.value);
});

Category

  • category (Field): Receipt category as seen on the receipt.
    The following categories are supported: toll, food, parking, transport, accommodation, gasoline, miscellaneous.
// To get the category
const category = resp.document.category.value

Date

Date fields:

  • contain the date_object attribute, which is a standard Ruby date object
  • contain the raw attribute, which is the textual representation found on the document.
  • have a value attribute which is the ISO 8601 representation of the date, regardless of the raw contents.

The following date fields are available:

  • date: Date the receipt was issued
// To get the receipt date of issuance
const receiptDate = resp.document.date.value

Locale

locale (Locale): Locale information.

  • locale.language (String): Language code in ISO 639-1 format as seen on the document.
    The following language codes are supported: ca, de, en, es, fr, it, nl and pt.
// To get the language code
const language = resp.document.locale.language
  • locale.currency (String): Currency code in ISO 4217 format as seen on the document.
    The following country codes are supported: CAD, CHF, GBP, EUR, USD.
// To get the currency code
const currency = resp.document.locale.currency
  • locale.country (String): Country code in ISO 3166-1 alpha-2 format as seen on the document.
    The following country codes are supported: CA, CH, DE, ES, FR, GB, IT, NL, PT and US.
// To get the country code
const country = resp.document.locale.country

Supplier Information

  • supplier (Field): Supplier name as written in the receipt.
// To get the supplier name
const supplierName = resp.document.supplier.value

Taxes

taxes (Array< TaxField >): Contains tax fields as seen on the receipt.

  • value (Float): The tax amount.
  • code (String): The tax code (HST, GST... for Canadian; City Tax, State tax for US, etc..).
  • rate (Float): The tax rate.
resp.document.taxes.forEach((tax) => {
  console.log(
    tax.value,
    tax.rate,
    tax.code,
  );
});

Time

  • time: Time of purchase as seen on the receipt
    • value (string): Time of purchase with 24 hours formatting (hh:mm).
    • raw (string): In any format as seen on the receipt.
// To get the time
const time = resp.document.time.value

Total Amounts

  • totalIncl (Field): Total amount including taxes
// To get the total amount including taxes value
const totalIncl = resp.document.totalIncl.value
  • totalExcl (Field): Total amount paid excluding taxes
// To get the total amount excluding taxes value
const totalExcl = receipt_data.document.totalExcl.value
  • totalTax (Field): Total tax value from tax lines
// To get the total tax amount value
const totalTax = receipt_data.document.totalTax.value

 

Questions?
Slack Logo IconSlack Logo Icon  Join our Slack


Did this page help you?