Giter Site home page Giter Site logo

cfanatic / ocr-receipt Goto Github PK

View Code? Open in Web Editor NEW
42.0 42.0 8.0 2.02 MB

Perform optical character recognition on receipts

License: MIT License

CMake 5.99% Dockerfile 3.95% Shell 2.29% C++ 82.91% Python 4.85%
easyocr ocr-recognition opencv receipts tesseract

ocr-receipt's Introduction

ocr-receipt

This repository features an application which performs optical character recognition on receipts.

The input is an image recorded with a cell phone, and the output is a list of article/price pairs as in:

struct article
{
    std::string name;
    float price;
};
std::vector<article> articles;

Character recognition is based on a two-step process: Tesseract is used to detect bounding boxes on receipts as shown below first, and then EasyOCR is used to extract the text within these bounding boxes.

Tesseract performs better in terms of coherent line-by-line detections, whereas EasyOCR is able to extract uncommon words with higher accuracy.

Detection Results

$ ./ocr-receipt -c ../misc/config.json -i ../misc/input/receipt_2.jpg
Shop: unknown
Box[0]: x=1034, y=53, w=238, h=89, conf: 84, text: EUR
Box[1]: x=89, y=107, w=1253, h=105, conf: 71, text: 814373 Konfitüre Extra 1,29 A
Box[2]: x=120, y=173, w=1227, h=104, conf: 86, text: 44718 Feine Kleinkuchen 1,79 A
Box[3]: x=116, y=239, w=1237, h=106, conf: 76, text: 60819 Erdbeeren 500g 0,99 A
Box[4]: x=77, y=304, w=1280, h=111, conf: 92, text: 814989 Spargel grün 400g 2,99 A
Article=Konfitüre Extra, Price=1.29
Article=Feine Kleinkuchen, Price=1.79
Article=Erdbeeren 500g, Price=0.99
Article=Spargel Grün 400g, Price=2.99
$ ./ocr-receipt -c ../misc/config.json -i ../misc/input/receipt_2.jpg --json
{
    "Shop": "unknown",
    "Articles": [
        {
            "Name": "Konfitüre Extra",
            "Price": "1.29"
        },
        {
            "Name": "Feine Kleinkuchen",
            "Price": "1.79"
        },
        {
            "Name": "Erdbeeren 500g",
            "Price": "0.99"
        },
        {
            "Name": "Spargel Grün 400g",
            "Price": "2.99"
        }
    ]
}

Requirements

The project itself is automatically built inside a Docker container based on following dependencies:

  • Ubuntu 22.04
  • Tesseract 5.1.0
  • EasyOCR 1.5.0
  • Boost 1.74.0
  • OpenCV 4.5.4

Setup

Build the image:

docker build -t ocr:latest .

Run a detached container:

docker run -t -d -e "TERM=xterm-256color" -v "$PWD":/src/ocr \
-e TESSDATA_PREFIX="/usr/local/share/tessdata" --name ocr-receipt ocr:latest bash

Build this project:

docker exec -it ocr-receipt ../misc/build.sh

The build script executes unit tests in the end, so hopefully you are seeing:

Test project /src/ocr/build
      Start  1: configuration_test.shops
 1/13 Test  #1: configuration_test.shops .................   Passed    0.10 sec
      Start  2: configuration_test.filters
 2/13 Test  #2: configuration_test.filters ...............   Passed    0.16 sec
 ...
      Start 12: receipt_test.receipt_3
12/13 Test #12: receipt_test.receipt_3 ...................   Passed    2.36 sec
      Start 13: receipt_test.receipt_4
13/13 Test #13: receipt_test.receipt_4 ...................   Passed    2.41 sec

100% tests passed, 0 tests failed out of 13

Total Test time (real) =  23.33 sec

Usage

Perform character recognition on one of the input images from the misc/input/ folder:

docker exec -it ocr-receipt ./ocr-receipt -c ../misc/config.json -i ../misc/input/receipt_2.jpg

You should be getting the results from above.

Add the --json switch if you want to serialize the results for further processing.

ocr-receipt's People

Contributors

cfanatic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ocr-receipt's Issues

Not worked, whats wrong

Here is the output,

docker exec -it ocr-receipt ../misc/build.sh
-- Configuring done
-- Generating done
-- Build files have been written to: /src/ocr/build
ninja: no work to do.
Test project /src/ocr/build
Start 1: configuration_test.shops
1/13 Test #1: configuration_test.shops ................. Passed 0.13 sec
Start 2: configuration_test.filters
2/13 Test #2: configuration_test.filters ............... Passed 0.13 sec
Start 3: configuration_test.easyocr
3/13 Test #3: configuration_test.easyocr ............... Passed 0.13 sec
Start 4: configuration_test.tesseract_paddings
4/13 Test #4: configuration_test.tesseract_paddings .... Passed 0.13 sec
Start 5: configuration_test.tesseract_threshold
5/13 Test #5: configuration_test.tesseract_threshold ... Passed 0.13 sec
Start 6: engine_easyocr_test.receipt_1
6/13 Test #6: engine_easyocr_test.receipt_1 ............***Exception: SegFault 52.22 sec
Start 7: engine_easyocr_test.receipt_2
7/13 Test #7: engine_easyocr_test.receipt_2 ............***Exception: SegFault 51.11 sec
Start 8: engine_tesseract_test.receipt_1
8/13 Test #8: engine_tesseract_test.receipt_1 ..........***Failed 16.81 sec
Start 9: engine_tesseract_test.receipt_2
9/13 Test #9: engine_tesseract_test.receipt_2 ..........***Failed 0.62 sec
Start 10: receipt_test.receipt_1
10/13 Test #10: receipt_test.receipt_1 ...................***Exception: SegFault 1.13 sec
Start 11: receipt_test.receipt_2
11/13 Test #11: receipt_test.receipt_2 ...................***Exception: SegFault 1.14 sec
Start 12: receipt_test.receipt_3
12/13 Test #12: receipt_test.receipt_3 ...................***Exception: SegFault 1.14 sec
Start 13: receipt_test.receipt_4
13/13 Test #13: receipt_test.receipt_4 ...................***Exception: SegFault 1.14 sec

38% tests passed, 8 tests failed out of 13

Total Test time (real) = 133.08 sec

The following tests FAILED:
6 - engine_easyocr_test.receipt_1 (SEGFAULT)
7 - engine_easyocr_test.receipt_2 (SEGFAULT)
8 - engine_tesseract_test.receipt_1 (Failed)
9 - engine_tesseract_test.receipt_2 (Failed)
10 - receipt_test.receipt_1 (SEGFAULT)
11 - receipt_test.receipt_2 (SEGFAULT)
12 - receipt_test.receipt_3 (SEGFAULT)
13 - receipt_test.receipt_4 (SEGFAULT)
Errors while running CTest
Output from these tests are in: /src/ocr/build/Testing/Temporary/LastTest.log
Use "--rerun-failed --output-on-failure" to re-run the failed cases verbosely.

Example Images Appear to Be Corrupt

Example Images under misc/input appear to be corrupt they cant be opened with any image manipulation software and can't be identified by "ImageMagicks" . This appears to be the case for all receipt_*.jpg files

Example System Error

user@host $ identify misc/input/receipt_2.jpg 
identify: Not a JPEG file: starts with 0x76 0x65 `misc/input/receipt_2.jpg' @ error/jpeg.c/JPEGErrorHandler/348.

running error

after installing it, I tried the example and I got this:

Error in pixReadStream: Unknown format: no pix returned
Error in pixRead: pix not read
Error in pixGetSpp: pix not defined
Error in pixGetDimensions: pix not defined
Error in pixGetColormap: pix not defined
Error in pixCopy: pixs not defined
Error in pixGetDepth: pix not defined
Error in pixGetWpl: pix not defined
Error in pixGetYRes: pix not defined
Please call SetImage before attempting recognition.
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string: construction from null is not valid

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.