• Stars
    star
    294
  • Rank 141,303 (Top 3 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 8 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Receipt scanner extracts information from your PDF or image receipts - built in NodeJS

receipt-scanner

Receipt scanner extracts information from your PDF or image receipts.

Travis NPM version Codecov David

Example

import scanner from 'receipt-scanner'

scanner(streamOrFilePath)
  .parse(function (err, results) {
    if (err) return console.error(err)
    else console.log(results)
  })

CLI

receipt-scanner path/to/image.png

Results

{
    "path/to/image.png": {
        "amount": "1,390.00",
        "date": "2016-06-19"
    }
}

Getting started

$ brew install opencv3 poppler tesseract --with-all-languages

$ brew link --force opencv3

$ npm install git+https://[email protected]/danschultzer/receipt-scanner.git -g

Now run:

receipt-scanner path/to/image.png

Command Line Interface

$ receipt-scanner --help

Usage: receipt-scanner [options] <path...>

Options:

  -h, --help             output usage information
  -f, --format <format>  format to return, json (default) or text
  -p, --progress         add a progress bar
  -s, --summary          show summary details
  -v, --verbose          show verbose information

Optional dependencies

These dependencies are only necessary if you're going to use imagemagick or graphicsmagick image preprocessor.

Preprocessor Install command
Graphicsmagick $ brew install graphicsmagick
Imagemagick $ brew install imagemagick

API

Custom image preprocessor

You can use, and chain, specific image preprocessors by using the imagePreprocessor method like so:

var gm = require('gm')

function customPreprocessor (fileOrStream, outfile, cb) {
  gm(fileOrStream)
   .resize(400, 200)
   .in('-level', '25%,75%')
   .write(outfile, function (error) {
     cb(error, outfile)
   })
}

import scanner from 'receipt-scanner'

scanner(streamOrFilePath)
  .imagePreprocessor(customPreprocessor)
  .parse(function (err, results) {
    if (err) return console.error(err)
    else console.log(results)
  })

The default preprocessor used is opencv. It's also possible to add configuration settings by pushing an array to imagePreprocessor like so:

scanner(stream_or_file_path)
  .imagePreprocessor(['opencv', { verbose: true }])

Custom text parser

You can add a custom text parser by using the textParser method like so:

function customTextParser (text) {
  var regexp = new RegExp('Description: (.*)', 'ig')
  var output = []
  while (var matches = regexp.exec(text)) {
    output.push(matches[1])
  }
  return { matches: output, match: output[0] }
}

import scanner from 'receipt-scanner'

scanner(streamOrFilePath)
  .textParser(customTextParser)
  .parse(function (err, results) {
    if (err) return console.error(err)
    else console.log(results)
  })

The value will be added to the response object for the customTextParser key.

Customizing amount or date parser

You can customize either parser by setting config like so:

import scanner from 'receipt-scanner'

scanner(streamOrFilePath)
  .textParser(['date', { parser: 'first' }])
  .parse(function (err, results) {
    if (err) return console.error(err)
    else console.log(results)
  })

Date parser will by default find the earliest date, but as shown in the example you can also find the first. The amount parser will find the total first, and if nothing is found, then find the biggest amount.

Date parser config options

parser: What parser to run, earliest or first

Amount parser config options

parsers: What parsers to run in order. Default is ['total', 'largest'].

Ticker

A ticker callback can be added with the ticker method.

import scanner from 'receipt-scanner'

scanner(streamOrFilePath)
  .ticker(function (percent) {
    // Update ticker with current percent amount
  })
  .parse(function (err, results) {
    if (err) return console.error(err)
    else console.log(results)
  })

Parse text

If you've already extracted the text, and just want to parse it for the relevant information you can use parseText.

import scanner from 'receipt-scanner'

var results = scanner().parseText(text)

It'll return the same results as when you use parse(callback).

How is text parsed?

Receipt scanner takes an ambiguous approach to date and amounts. Amounts formatting is guessed from the number of amounts found with comma or with dots for decimal separation.

What's the binaries for?

poppler: For pdftotext module and pdfimages binary (PDF processing)

imagemagick: For gm module (image preprocessing)

graphicsmagick: For gm module (image preprocessing)

opencv3: For node-opencv (image preprocessing)

vips: For sharp module (image preprocessing)

tesseract --all-languages: For node-tesseract module (OCR)

Tests

$ npm test

You can use npm test watch to keep tests running, and npm run cover for coverage.

Benchmark

Run npm run benchmark to get success rate using the receipt-scanner-testdata repository.

To generate random data set to benchmark run npm run generate-benchmark-sample.

LICENSE

(The MIT License)

Copyright (c) 2016 Dan Schultzer, Benjamin Schultzer & the Contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

ex_oauth2_provider

Making OAuth 2 provider and authentication with http bearer as simple as possible for Elixir and Phoenix apps
Elixir
190
star
2

premailex

Preflight for your HTML emails - inline styling and plain text.
Elixir
155
star
3

phoenix_oauth2_provider

Get an OAuth 2 provider running in your phoenix with controllers, views and models in just two minutes
Elixir
80
star
4

monit-graph

PHP graphing and logging tool for Monit, to graph and analyse process and/or server performance.
PHP
80
star
5

ansible-phoenix-build

Sample app with full build and deploy setup of a Phoenix app using ansible
Elixir
61
star
6

test_server

No fuzz ExUnit test server to mock third party services
Elixir
60
star
7

blazing-bookkeeper

Who has got time to read receipts? Let Blazing Bookkeeper blaze through all your receipts in no time.
JavaScript
27
star
8

coherence_assent

UNMAINTAINED - Add multi provider login to your Coherence Phoenix website
Elixir
26
star
9

elixir-terraform-aws-ecs-example

Sample for terraform IaC for elixir projects in ECS
HCL
25
star
10

idempotency_plug

Plug that makes POST and PATCH requests idempotent
Elixir
16
star
11

phoenix_ex_oauth2_provider_demo

Elixir
9
star
12

keep-the-chain

Keep the chain consistency tool. Tool to kill procrastination, focus on your daily goal, and keep being proactive. Built in Javascript (jQuery based), using local storage. Everything kept in your browser!
JavaScript
8
star
13

blazing-bookkeeper-server

JavaScript
2
star
14

peakium-documentation-viewer

Display your documentation easily and beautifully
CSS
1
star
15

fexr

Express.js API for foreign exchange rates from various national banks
JavaScript
1
star