Custom OCR .NET
The .NET 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
CustomEndpoint myEndpoint = new CustomEndpoint(
endpointName: "wnine",
accountName: "john",
version: "1.1" // optional
);
var prediction = await _mindeeClient
.LoadDocument(new FileInfo(Path))
.ParseAsync(myEndpoint);
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 ParseAsync
method to call the API prediction on your custom document.
The response class and document type must be specified when calling this method.
You have two different ways to parse a custom document.
- Don't specify the return class, and use the default one (named
CustomPrediction
):
var prediction = await _mindeeClient
.LoadDocument(new FileInfo(Path))
.ParseAsync(myEndpoint);
- You can also use your own class which will represent the required fields. For example:
// The CustomEndpoint attribute is required when using your own model.
// It will be used to know which Mindee API called.
[CustomEndpoint(
endpointName: "wnine",
accountName: "john",
version: "1.1" // optional
)]
public sealed class WNine
{
[JsonPropertyName("name")]
public StringField Name { get; set; }
[JsonPropertyName("employer_id")]
public StringField EmployerId { get; set; }
...
}
var prediction = await _mindeeClient
.LoadDocument(new FileInfo(Path))
.ParseAsync<WNine>();
CustomPrediction object
All the fields which are defined in the API builder when creating your custom document, are available.
CustomPrediction
is a Dictionary
with the key as a string
for the name of the field, and a ListField
as a value.
Each ListField
contains a list of all values extracted for this field.
Value fields are accessed via the Values
property.
Info
Both document level and page level objects work in the same way.
Fields property
A Map with the following structure:
Confidence
: adouble
Values
: a list ofListFieldValue
which containing a list of all values found for the field.
In the examples below we'll use the employer_id
field.
CustomEndpoint myEndpoint = new CustomEndpoint(
endpointName: "wnine",
accountName: "john",
version: "1.1" // optional
);
var prediction = await _mindeeClient
.LoadDocument(new FileInfo(path))
.ParseAsync(myEndpoint);
ListField? employerId = prediction.Inference.Prediction.GetValueOrDefault("employer_id");
Line items reconstructions
We offer the possiblity to use a post processing after you get prediction result from your custom API.
In the below example, image that your custom document have 4 columns defined a table.
So, you want to get all the line items of it.
In that case, you will have to define 4 fields and do the annotation verticaly for each one.
After training your model, test it using the mindee client as below to get your document parsed and line items reconstructed.
CustomEndpoint myEndpoint = new CustomEndpoint(
endpointName: "wnine",
accountName: "john",
version: "1.1" // optional
);
var documentParsed = await _mindeeClient
.LoadDocument(new FileInfo(path))
.ParseAsync(myEndpoint);
var fieldNamesToLineItems = new List<string>() { "beneficiary_birth_date", "beneficiary_number", "beneficiary_name", "beneficiary_rank" };
var lineHeigthTolerance = 0.011d; // it helps to handle line height variation of your lines. The value will depends of your table of course !
var lineItems = LineItemsGenerator.Generate(
fieldNamesToLineItems,
documentParsed.Inference.DocumentPrediction.Fields,
new Anchor("beneficiary_name", lineHeigthTolerance)); // The anchor must be the column where there is always a value in your table.
Questions?
Join our Slack
Updated about 1 month ago