# Barcode Reader OCR

Mindee’s Barcode Reader OCR API uses deep learning to automatically, accurately, and instantaneously parse your Barcode details. In under a second, the API extracts a set of data from your PDFs or photos of any document, including:

* Barcodes 1d
* Barcodes 2d
* Orientation

The Barcode Reader OCR API supports all kind of documents.

## Set up the API

{% hint style="info" %}
**Create an API key**

To begin using the Mindee V1 OCR API, your first step is to [create your V1 API key](https://docs.mindee.com/v1/get-started/create-api-key).
{% endhint %}

1. You'll need a document including one or multiple barcodes. You can use the sample document provided below.

<figure><img src="https://126655343-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2al1MDqAP9Dg9iDRjkWg%2Fuploads%2Fgit-blob-01eb0c1c961148d6e2d3e272d7ea0d960d983b4b%2F45c94c5-barcode-receipt3.jpg?alt=media" alt="" width="563"><figcaption></figcaption></figure>

2. Access your API by clicking on the **Barcode Reader** card in the **Utilities**

<figure><img src="https://126655343-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2al1MDqAP9Dg9iDRjkWg%2Fuploads%2Fgit-blob-a07e4602a2b70f351d1d1507cc687e7d224deeb3%2Fc00714c-barcodes-doc.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

3. From the left navigation, go to [**documentation**](doc:platform-tour#api---documentation) **> API Reference**, you'll find sample code in popular languages and command line.

{% tabs %}
{% tab title="Python" %}

```python
from mindee import Client, PredictResponse, product

# Init a new client
mindee_client = Client(api_key="my-api-key")

# Load a file from disk
input_doc = mindee_client.source_from_path("/path/to/the/file.ext")

# Load a file from disk and parse it.
result: PredictResponse = mindee_client.parse(
    product.BarcodeReaderV1,
    input_doc,
)

# Print a summary of the API result
print(result.document)

# Print the document-level summary
# print(result.document.inference.prediction)
```

{% endtab %}

{% tab title="Node.js" %}

```javascript
const mindee = require("mindee");
// for TS or modules:
// import * as mindee from "mindee";

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

// Load a file from disk
const inputSource = mindeeClient.docFromPath("/path/to/the/file.ext");

// Parse the file
const apiResponse = mindeeClient.parse(
  mindee.product.BarcodeReaderV1,
  inputSource
);

// Handle the response Promise
apiResponse.then((resp) => {
  // print a string summary
  console.log(resp.document.toString());
});
```

{% endtab %}

{% tab title="PHP" %}

```php
use Mindee\Client;
use Mindee\Product\BarcodeReader\BarcodeReaderV1;

// Init a new client
$mindeeClient = new Client("my-api-key");

// Load a file from disk
$inputSource = $mindeeClient->sourceFromPath("/path/to/the/file.ext");

// Parse the file
$apiResponse = $mindeeClient->parse(BarcodeReaderV1::class, $inputSource);

echo $apiResponse->document;
```

{% endtab %}

{% tab title=".NET" %}

```csharp
using Mindee;
using Mindee.Input;
using Mindee.Http;
using Mindee.Parsing;

string apiKey = "my-api-key-here";
string filePath = "/path/to/the/file.ext";

// Construct a new client
MindeeClient mindeeClient = new MindeeClient(apiKey);

// Load an input source as a path string
// Other input types can be used, as mentioned in the docs
var inputSource = new LocalInputSource(filePath);

// Set the endpoint configuration
CustomEndpoint myEndpoint = new CustomEndpoint(
    endpointName: "barcode_reader",
    accountName: "mindee",
    version: "1.0"
);

var response = await mindeeClient
    .ParseAsync(inputSource, myEndpoint);

// Print a summary of the predictions
System.Console.WriteLine(response.Document.ToString());

// Print the document-level predictions
// System.Console.WriteLine(response.Document.Inference.Prediction.ToString());
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
require 'mindee'

# Init a new client
mindee_client = Mindee::Client.new(api_key: 'my-api-key')

# Load a file from disk
input_source = mindee_client.source_from_path('/path/to/the/file.ext')

# Parse the file
result = mindee_client.parse(
  input_source,
  Mindee::Product::BarcodeReader::BarcodeReaderV1
)

# Print a full summary of the parsed data in RST format
puts result.document

# Print the document-level parsed data
# puts result.document.inference.prediction
```

{% endtab %}

{% tab title="Java" %}

```java
import com.mindee.MindeeClient;
import com.mindee.input.LocalInputSource;
import com.mindee.parsing.common.PredictResponse;
import com.mindee.product.custom.CustomV1;
import com.mindee.http.Endpoint;
import java.io.File;
import java.io.IOException;

public class SimpleMindeeClient {

  public static void main(String[] args) throws IOException {
    String apiKey = "my-api-key-here";
    String filePath = "/path/to/the/file.ext";

    // Init a new client
    MindeeClient mindeeClient = new MindeeClient(apiKey);

    // Load a file from disk
    LocalInputSource inputSource = new LocalInputSource(new File(filePath));

    // Configure the endpoint
    Endpoint endpoint = new Endpoint(
        "barcode_reader",
        "mindee",
        "1.0"
    );
    
    // Parse the file
    PredictResponse<CustomV1> response =  mindeeClient.parse(
        inputSource,
        endpoint
    );

    // Print a summary of the response
    System.out.println(response.toString());

    // Print a summary of the predictions
//  System.out.println(response.getDocument().toString());

    // Print the document-level predictions
//    System.out.println(response.getDocument().getInference().getPrediction().toString());

    // Print the page-level predictions
//    response.getDocument().getInference().getPages().forEach(
//        page -> System.out.println(page.toString())
//    );
  }

}
```

{% endtab %}

{% tab title="Bash" %}

```bash
curl -X POST \\
  https://api.mindee.net/v1/products/mindee/barcode_reader/v1/predict \\
  -H 'Authorization: Token my-api-key-here' \\
  -H 'content-type: multipart/form-data' \\
  -F document=@/path/to/your/file.png
```

{% endtab %}
{% endtabs %}

* Replace **my-api-key-here** with your new API key, or use the **select an API key** feature and it will be filled automatically.
* Copy and paste the sample code of your desired choice in your application, code environment, terminal etc.
* Replace `/path/to/the/file.ext` with the path to your input document.

{% hint style="warning" %}
Remember to replace with your V1 API key.
{% endhint %}

## API Response

Here is the full JSON response you get when you call the API:

```json
{
  "api_request": {
    "error": {},
    "resources": [
      "document"
    ],
    "status": "success",
    "status_code": 201,
    "url": "https://api.mindee.net/v1/products/mindee/barcode_reader/v1/predict"
  },
  "document": {
    "id": "c68d69b3-3855-40b6-8dae-f53fd6198800",
    "name": "sample_receipt.jpg",
    "n_pages": 1,
    "is_rotation_applied": true,
    "inference": {
      "started_at": "2023,-05-06T16:37:28",
      "finished_at": "2023-05-06T16:37:29",
      "processing_time": 1.755,
      "pages": [
        {
          "id": 0,
          "orientation": {"value": 0},
          "prediction": { .. },
          "extras": {}
        }
      ],
      "prediction": { .. },
      "extras": {}
    }
  }
}
```

You can find the prediction within the `prediction` key found in two locations:

* **In `document > inference > prediction` for document-level predictions**: it contains the different fields extracted at the document level, meaning that for multi-pages PDFs, we reconstruct a single receipt object using all the pages.
* **In `document > inference > pages[ ] > prediction` for page-level predictions**: it gives the prediction for each page independently. With images, there is only one element on this array, but with PDFs, you can find the extracted data for each PDF page.

Each predicted field may contain one or several values:

* a `confidence` score
* a `polygon` highlighting the information location
* a `page_id` where the information was found (document level only)

```json
{
    "extras": {},
    "finished_at": "2023-08-18T08:48:25.761599",
    "is_rotation_applied": true,
    "pages": [
        {
            "extras": {},
            "id": 0,
            "orientation": {"value": 0},
            "prediction": {
                "codes_1d": [
                    {
                        "confidence": 1,
                        "polygon": [
                            [
                                0.342,
                                0.856
                            ],
                            [
                                0.671,
                                0.856
                            ],
                            [
                                0.671,
                                0.975
                            ],
                            [
                                0.342,
                                0.975
                            ]
                        ],
                        "value": "Mindee"
                    }
                ],
                "codes_2d": [
                    {
                        "confidence": 1,
                        "polygon": [
                            [
                                0.671,
                                0.06
                            ],
                            [
                                0.87,
                                0.06
                            ],
                            [
                                0.87,
                                0.189
                            ],
                            [
                                0.671,
                                0.189
                            ]
                        ],
                        "value": "https://developers.mindee.com/docs/barcode-reader-ocr"
                    },
                    {
                        "confidence": 1,
                        "polygon": [
                            [
                                0.745,
                                0.846
                            ],
                            [
                                0.925,
                                0.846
                            ],
                            [
                                0.925,
                                0.966
                            ],
                            [
                                0.745,
                                0.966
                            ]
                        ],
                        "value": "I love paperwork! - Said no one ever"
                    }
                ],
                "orientation": {
                    "confidence": 0.99,
                    "degrees": 0
                }
            }
        }
    ],
    "prediction": {
        "codes_1d": [
            {
                "confidence": 1,
                "page_id": 0,
                "polygon": [
                    [0.342, 0.856],
                    [0.671, 0.856],
                    [0.671, 0.975],
                    [0.342, 0.975]
                ],
                "value": "Mindee"
            }
        ],
        "codes_2d": [
            {
                "confidence": 1,
                "page_id": 0,
                "polygon": [
                    [0.671, 0.06],
                    [0.87, 0.06],
                    [0.87, 0.189],
                    [0.671, 0.189]
                ],
                "value": "https://developers.mindee.com/docs/barcode-reader-ocr"
            },
            {
                "confidence": 1,
                "page_id": 0,
                "polygon": [
                    [0.745, 0.846],
                    [0.925, 0.846],
                    [0.925, 0.966],
                    [0.745, 0.966]
                ],
                "value": "I love paperwork! - Said no one ever"
            }
        ]
    },
    "processing_time": 0.631,
    "product": {
        "features": [
            "codes_1d",
            "codes_2d",
            "orientation"
        ],
        "name": "mindee/barcode_reader",
        "type": "standard",
        "version": "1.0"
    },
    "started_at": "2023-08-18T08:48:25.130224"
}
```

## Extracted barcode data

Using the above barcode example the following are the basic fields that can be extracted.

* [Barcodes 1D](#barcodes_1d)
* [Barcodes 2D](#barcodes_2d)
* [Orientation](#orientation)

### Barcodes 1D

* **barcodes\_1d**: This field outputs the usual 1d barcodes like the Code-128 format. The API response includes, beside a confidence score, the position of the barcode on the document as well as the transcribed value, that is hidden behind the barcode.

```json
{
  "codes_1d": [
                    {
                        "confidence": 1,
                        "polygon": [
                            [
                                0.342,
                                0.856
                            ],
                            [
                                0.671,
                                0.856
                            ],
                            [
                                0.671,
                                0.975
                            ],
                            [
                                0.342,
                                0.975
                            ]
                        ],
                        "value": "Mindee"
                    }
                ]
}
```

### Barcodes 2D

* **barcodes\_2d**: This field outputs the usual 2d barcodes like the QR-Code format. The API response includes, beside a confidence score, the position of the barcode on the document as well as the transcribed value, that is hidden behind the barcode. As seen in the example, it is no issue to output multiple barcodes of the same kind on the same document. This goes of course for the 1d barcodes as well.

```json
{
  "codes_2d": [
                    {
                        "confidence": 1,
                        "polygon": [
                            [
                                0.671,
                                0.06
                            ],
                            [
                                0.87,
                                0.06
                            ],
                            [
                                0.87,
                                0.189
                            ],
                            [
                                0.671,
                                0.189
                            ]
                        ],
                        "value": "https://developers.mindee.com/docs/barcode-reader-ocr"
                    },
                    {
                        "confidence": 1,
                        "polygon": [
                            [
                                0.745,
                                0.846
                            ],
                            [
                                0.925,
                                0.846
                            ],
                            [
                                0.925,
                                0.966
                            ],
                            [
                                0.745,
                                0.966
                            ]
                        ],
                        "value": "I love paperwork! - Said no one ever"
                    }
                ]
}
```

### Orientation

* **orientation**: Classification indicating the possible rotation of the image. Possible values are:
  * 0
  * 90
  * 270\
    Images with a rotation of 180 degrees are not supported.

```json
{
  "orientation": {
                    "confidence": 0.99,
                    "degrees": 0
                }
}
```
