Skip to content

NanoNets/ocr-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 

Repository files navigation

Python OCR

image

This python package is an OCR library which reads all text & tables from image & PDF files using an OCR engine & provides intelligent post-processing options to save OCR results in formats you want.

image

Installation -----

The package requires Python 3 to run.

You can use pip to install:

pip install ocr-nanonets-wrapper

Authentication -----

This software is perpetually free :)

You can get your free API key (with unlimited requests) by creating a free account on https://app.nanonets.com/#/keys.

from nanonets import NANONETSOCR
model = NANONETSOCR()
model.set_token('REPLACE_API_KEY')

Usage

You can refer the code shared below or directly use code from here.

# Initialise
from nanonets import NANONETSOCR
model = NANONETSOCR()

# Authenticate
# This software is perpetually free :)
# You can get your free API key (with unlimited requests) by creating a free account on https://app.nanonets.com/#/keys?utm_source=wrapper.
model.set_token('REPLACE_API_KEY')

# PDF / Image to Raw OCR Engine Output
import json
pred_json = model.convert_to_prediction('INPUT_FILE')
print(json.dumps(pred_json, indent=2))

# PDF / Image to String
string = model.convert_to_string('INPUT_FILE')
print(string)

# PDF / Image to TXT File
model.convert_to_txt('INPUT_FILE', output_file_name = 'OUTPUTNAME.txt')

# PDF / Image to Boxes 
# each element contains predicted word and bounding box information
# bounding box information denotes the spatial position of each word in the file
boxes = model.convert_to_boxes('test.png')
for box in boxes:
    print(box)

# PDF / Image to CSV
# This method extracts tables from your file and prints them in a .csv file.
# NOTE : This particular function is a trial offering 1000 pages of use. 
# To use this at scale, please create your own model at app.nanonets.com --> New Model --> Tables.
model.convert_to_csv('INPUT_FILE', output_file_name = 'OUTPUTNAME.csv')

# PDF / Image to Tables
# This method extracts tables from your file and returns a json object.
# NOTE : This particular function is a trial offering 1000 pages of use. 
# To use this at scale, please create your own model at app.nanonets.com --> New Model --> Tables.
import json
tables_json = model.convert_to_tables('INPUT_FILE')
print(json.dumps(tables_json, indent=2))

# PDF / Image to Searchable PDF
model.convert_to_searchable_pdf('INPUT_FILE', output_file_name = 'OUTPUTNAME.pdf')  

Testing

To make getting started easier for you, there is a bunch of sample code along with sample input files.

  • Clone or download the repo and open the /tests folder.
  • all_tests.ipynb is a python notebook containing testing for all methods in the package.
  • convert_to{METHOD}.py files are python files corresponding to each method in the package individually.

Note

convert_to_string() and convert_to_txt() methods have two optional parameters -

  1. formatting =
  • `lines and spaces` : default, all formatting enabled
  • `none` : space separated text with formatting removed
  • `lines` : space separated text with lines separated with newline character
  • `pages` : list of page wise space separated text
  1. line_threshold =
  • `low` : default
  • `high : You can add line_threshold='high'` as a parameter while calling the method which in few cases can improve reading flowcharts and diagrams.

Advanced Functions ------------If extracting flat fields, tables and line items from PDFs and images is your use case, I will strongly advice you to create your own model by signing up on app.nanonets.com and using our advanced API. This will improve functionalities, accuracy and response times significantly. Once you have created your account and model, you can use API documentation present here to extract flat fields, tables and line items from any PDF or image.

Nanonets

We help businesses automate Manual Data Entry Using AI and reduce turn around times & manual effort required. More than 1000 enterprises use Nanonets for Intelligent Document Processing. We have generated incredible ROIs for our clients.

We provide OCR and IDP solutions customised for various use cases - invoice automation, Receipt OCR, purchase order automation, accounts payable automation, ID Card OCR and many more.

License

MIT

This software is perpetually free :)