Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
[![License: MIT](https://img.shields.io/github/license/mindee/mindee-api-python)](https://opensource.org/licenses/MIT)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/mindee/mindee-api-python/test.yml)](https://github.com/mindee/mindee-api-python)
[![PyPI Version](https://img.shields.io/pypi/v/mindee)](https://pypi.org/project/mindee/)
[![Downloads](https://img.shields.io/pypi/dm/mindee)](https://pypi.org/project/mindee/)
[![License: MIT](https://img.shields.io/github/license/mindee/mindee-api-python)](https://opensource.org/licenses/MIT) [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/mindee/mindee-api-python/test.yml)](https://github.com/mindee/mindee-api-python) [![PyPI Version](https://img.shields.io/pypi/v/mindee)](https://pypi.org/project/mindee/) [![Downloads](https://img.shields.io/pypi/dm/mindee)](https://pypi.org/project/mindee/)

# Mindee API Helper Library for Python
Quickly and easily connect to Mindee's API services using Python.
Expand Down Expand Up @@ -31,10 +28,10 @@ mindee_client = Client(api_key="my-api-key")
input_doc = mindee_client.doc_from_path("/path/to/the/file.ext")

# Parse the document as an invoice by passing the appropriate type
api_response = input_doc.parse(documents.TypeInvoiceV4)
result = input_doc.parse(documents.TypeInvoiceV4)

# Print a brief summary of the parsed data
print(api_response.document)
print(result.document)
```

#### Region-Specific Documents
Expand All @@ -48,10 +45,10 @@ mindee_client = Client(api_key="my-api-key")
input_doc = mindee_client.doc_from_path("/path/to/the/file.ext")

# Parse the document as a USA bank check by passing the appropriate type
api_response = input_doc.parse(documents.us.TypeBankCheckV1)
result = input_doc.parse(documents.us.TypeBankCheckV1)

# Print a brief summary of the parsed data
print(api_response.document)
print(result.document)
```

#### Custom Document (API Builder)
Expand All @@ -67,24 +64,29 @@ mindee_client = Client(api_key="my-api-key").add_endpoint(

# Load a file from disk and parse it.
# The endpoint name must be specified since it can't be determined from the class.
api_response = mindee_client.doc_from_path(
result = mindee_client.doc_from_path(
"/path/to/the/file.ext"
).parse(documents.TypeCustomV1, endpoint_name="wnine")

# Print a brief summary of the parsed data
print(api_response.document)
print(result.document)

# Iterate over all the fields in the document
for field_name, field_values in api_response.document.fields.items():
for field_name, field_values in result.document.fields.items():
print(field_name, "=", field_values)
```

## Further Reading
There's more to it than that for those that need more features, or want to
customize the experience.
Complete details on the working of the library are available in the following guides:

All the juicy details are described in the
**[Official Guide](https://developers.mindee.com/docs/python-sdk)**.
* [Getting started](https://developers.mindee.com/docs/getting-started)
* [Command Line Interface (CLI)](https://developers.mindee.com/docs/python-cli)
* [Custom APIs (API Builder)](https://developers.mindee.com/docs/python-api-builder)
* [Invoice API](https://developers.mindee.com/docs/python-invoice-ocr)
* [Passport API](https://developers.mindee.com/docs/python-passport-ocr)
* [Receipt API](https://developers.mindee.com/docs/python-receipt-ocr)

You can view the source code on [GitHub](https://github.com/mindee/mindee-api-nodejs).

You can also take a look at the
**[Reference Documentation](https://mindee.github.io/mindee-api-python/)**.
Expand All @@ -93,3 +95,6 @@ You can also take a look at the
Copyright © Mindee

Available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

## Questions?
[Join our Slack](https://join.slack.com/t/mindee-community/shared_invite/zt-1jv6nawjq-FDgFcF2T5CmMmRpl9LLptw)
134 changes: 134 additions & 0 deletions docs/guide/python-api-builder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
The Python OCR SDK supports [custom-built API](https://developers.mindee.com/docs/build-your-first-document-parsing-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](https://developers.mindee.com/docs/overview).

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](https://developers.mindee.com/docs/overview).

For the following examples, we are using our own [W9s custom API](https://developers.mindee.com/docs/w9-forms-ocr),
created with the [API Builder](https://developers.mindee.com/docs/overview).

```python
from mindee import Client, documents

# Init a new client and add your custom endpoint (document)
mindee_client = Client(api_key="my-api-key").add_endpoint(
account_name="john",
endpoint_name="wsnine",
# version="1.2", # optional, see configuring client section below
)

# Load a file from disk and parse it.
# The endpoint name must be specified since it can't be determined from the class.
result = mindee_client.doc_from_path(
"/path/to/the/w9.jpg"
).parse(documents.TypeCustomV1, endpoint_name="wnine")

# Print a brief summary of the parsed data
print(result.document)
```

## Adding the Endpoint
Below are the arguments for adding a custom endpoint using the `add_endpoint` method.

**`endpoint_name`**: The endpoint name is the API name from [Settings](https://developers.mindee.com/docs/build-your-first-document-parsing-api#settings-api-keys-and-documentation) page

**`account_name`**: Your organization's or user's name in the API Builder.

**`version`**: If set, locks the version of the model to use, you'll be required to update your code every time a new model is trained.
This is probably not needed for development but essential for production use.
If not set, uses the latest version of the model.

## Parsing Documents
The client calls the `parse` method when parsing your custom document, which will return an object containing the prediction results of sent file.
The `endpoint_name` must be specified when calling the `parse` method for a custom endpoint.

```python
result = mindee_client.doc_from_path("/path/to/receipt.jpg").parse(
documents.TypeCustomV1, endpoint_name="wnine"
)

print(result.document)
```

> 📘 **Info**
>
> If your custom document has the same name as an [off-the-shelf APIs](https://developers.mindee.com/docs/what-is-off-the-shelf-api) document,
> you **must** specify your account name when calling the `parse` method:

```python
from mindee import Client, documents

mindee_client = Client(api_key="johndoe-receipt-api-key").add_endpoint(
endpoint_name="receipt",
account_name="JohnDoe",
)

result = mindee_client.doc_from_path("/path/to/receipt.jpg").parse(
documents.TypeCustomV1,
endpoint_name="wnine",
account_name="JohnDoe",
)
```

## Document Fields
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 can be accessed via the `fields` attribute.

Classification fields can be accessed via the `classifications` attribute.

> 📘 **Info**
>
> Both document level and page level objects work in the same way.

### Fields Attribute
The `fields` attribute is a dictionary with the following structure:

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

Individual field values can be accessed by using the field's API name, in the examples below we'll use the `address` field.

```python
# raw data, list of each word object
print(result.document.fields["address"].values)

# list of all values
print(result.document.fields["address"].contents_list)

# default string representation
print(str(result.document.fields["address"]))

# custom string representation
print(result.document.fields["address"].contents_string(separator="_"))
```

To iterate over all the fields:
```python
for name, info in result.document.fields.items():
print(name)
print(info.values)
```

### Classifications Attribute
The `classifications` attribute is a dictionary with the following structure:

* key: the API name of the field, as a `str`
* value: a `ClassificationField` object which has a `value` attribute, containing a string representation of the detected classification.

```python
# raw data, list of each word object
print(result.document.classifications["doc_type"].values)
```

To iterate over all the classifications:
```python
for name, info in result.document.classifications.items():
print(name)
print(info.values)
```

## Questions?
[Join our Slack](https://join.slack.com/t/mindee-community/shared_invite/zt-1jv6nawjq-FDgFcF2T5CmMmRpl9LLptw)
48 changes: 48 additions & 0 deletions docs/guide/python-cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## Command Line Usage
The CLI tool is provided mainly for quick tests and debugging.

### General help

```shell
python3 -m mindee --help
```

### Example command help

```shell
python3 -m mindee invoice --help
```

### Example parse command for Off-the-Shelf document

```shell
python3 -m mindee invoice --invoice-key xxxxxxx /path/to/invoice.pdf
```

### Works with environment variables

```shell
export MINDEE_API_KEY=xxxxxx
python3 -m mindee invoice /path/to/invoice.pdf
```

### Example parse command for a custom document

```shell
python3 -m mindee custom -u pikachu -k xxxxxxx pokemon_card /path/to/card.jpg
```

### You can get the full parsed output as well

```shell
python3 -m mindee invoice -o parsed /path/to/invoice.pdf
```

### In the Git repo, there's a helper script for it

```shell
./mindee-cli.sh -h
```

## Questions?
[Join our Slack](https://join.slack.com/t/mindee-community/shared_invite/zt-1jv6nawjq-FDgFcF2T5CmMmRpl9LLptw)
Loading