Custom documents Node.js OCR APIs

The Node.js OCR SDK supports custom-built API from the API Builder. If your document isn't covered by one of Mindee's Off-the-Shelf APIs, you can create your own API using the API Builder.

For the following examples, we are using our own W9s custom API created with the API Builder.

Quick Start

📘

Info

To modify this to your own custom API, change the addEndpoint and parse calls with your own parameters.

import { Client,  CustomResponse } from "mindee";

// Init a new client and add your custom document
mindeeClient = new Client({ apiKey: "my-api-key" })
  .addEndpoint({
      accountName: "john",
      documentType: "wsnine",
      version: "1.1" // optional: if not set, use the latest version of the model
  });

// Load a file from disk and parse it
const doc = mindeeClient.docFromPath("/path/to/custom_file.jpg");
const respPromise = doc.parse(CustomResponse, { docType: "wsnine" });

// Print a summary of the parsed data
respPromise.then((resp) => {
    console.log(resp.document);
});

If the version argument is set, you'll be required to update it every time a new model is trained. This is probably not needed for development but essential for production use.

Parsing Documents

Use the parse method to call the API prediction on your custom document.
The response class and document type must be specified when calling this method.

The return will be a promise which resolves to an instance of CustomResponse.

// Load a file from disk and parse it
respPromise = mindeeClient.docFromPath('/path/to/custom_file.jpg')
  .parse(CustomResponse, { docType: "wsnine" });

// Print a brief summary of the parsed data
respPromise.then((resp) => {
    console.log(resp.document);
});

📘

Info

If your custom document has the same name as an off-the-shelf APIs document, you must specify your account name when calling the parse method:

mindeeClient = new Client({ apiKey: "my-api-key" })
  .configCustomDoc({
      accountName: "john",
      documentType: "receipt",
  });

respPromise = mindeeClient.docFromPath('/path/to/custom_file.jpg')
  .parse(CustomResponse, { docType: "receipt", username: "john" });

Field Objects

All the fields defined in the API builder when creating your custom document are available.

In custom documents, each field will hold an array of all the words in the document which are related to that field.
Each word is an object that has the text content, geometry information, and confidence score.

Value fields are accessed via the fields attribute.

Classification fields are accessed via the classifications attribute.

📘

Info

Both document level and page level objects work in the same way.

Fields property

A Map with the following structure:

  • key: the API name of the field, as a string
  • value: a ListField object which has a values attribute, containing a list of all values found for the field.

In the examples below we'll use the address field.

if (resp.document === undefined) return;

const address = resp.document.fields.get("address");

// Map's get method can return undefined.
if (address === undefined) return;

// raw data, list of each word object
console.log(address.values);

// list of all values
console.log(address.contentsList());

// default string representation
console.log(address.toString());
// or
console.log(`${address}`);
// or
console.log(address.contentsString());

// custom string representation
console.log(address.contentsString("_"));

Being a Map, it's very easy to iterate over all the fields:

if (resp.document === undefined) return;

resp.document.fields.forEach((field, name) => {
  console.log(name);
  console.log(field.values);
});

Classifications property

A Map with the following structure:

  • key: the API name of the field, as a string
  • value: a ClassificationField object which has a value attribute, containing a string representation of the detected classification.
if (resp.document === undefined) return;

const docType = resp.document.classifications.get("doc_type");

// Map's get method can return undefined.
if (docType === undefined) return;

// raw data, list of each word object
console.log(docType.value);

Being a Map, it's very easy to iterate over all the fields:

if (resp.document === undefined) return;

resp.document.classifications.forEach((classification, name) => {
  console.log(name);
  console.log(classification.value);
});

 

Questions?
Slack Logo IconSlack Logo Icon  Join our Slack


Did this page help you?