Code Monkey home page Code Monkey logo

htrpipeline's Introduction

Detect and Read Handwritten Words

This is a handwritten text recognition (HTR) pipeline that operates on scanned pages and applies the following operations:

  • Detect words
  • Read words

example

Installation

  • Download the zipped model weights
  • Unzip
  • Copy the files (reader.onnx, reader.json, detector.onnx) into the folder htr_pipeline/models
  • Go to the root level of the repository (where setup.py is located)
  • Execute pip install .

Usage

Run demo

  • Additionally install matplotlib for plotting: pip install matplotlib
  • Go to scripts/
  • Run python demo.py
  • The output should look like the plot shown above

Run web demo (gradio)

  • Additionally install gradio: pip install gradio
  • Go to the root directory of the repository
  • Run python scripts/gradio_demo.py
  • Open the URL shown in the output

example

Use Python package

Import the function read_page to detect and read text.

import cv2
from htr_pipeline import read_page, DetectorConfig, LineClusteringConfig

# read image
img = cv2.imread('data/sample_1.png', cv2.IMREAD_GRAYSCALE)

# detect and read text
read_lines = read_page(img, 
                       DetectorConfig(scale=0.4, margin=5), 
                       line_clustering_config=LineClusteringConfig(min_words_per_line=2))

# output text
for read_line in read_lines:
    print(' '.join(read_word.text for read_word in read_line))

Selection of parameters

Configuration is done by passing instances of these dataclasses to the read_page function:

  • DetectorConfig: configure the word detector
  • LineClusteringConfig: configure the line clustering algorithm
  • ReaderConfig: configure the text reader

The most important parameter for the detector is the scale. The detector works best for text of height 50px. Setting a scale != 1 automatically resizes the image before applying te detector. Example: Text height h is 100px in the original image. Set the scale to 0.5 so that detection happens at the ideal text size.

scale

The second most important parameter for the detector is the margin. It allows adding a few pixels (blue) around the detected words (red) which might improve reading quality.

scale

For the line clustering algorithm the minimum number of words can be set with the parameter min_words_per_line. Lines which contain fewer words will be ignored. Example: it is known that all lines contain 2 or more words. Then set the parameter to 2 to ignore false positive detections that form lines with only a single word.

Future work

  • Better documentation of all the features (e.g., how to use a dictionary) - for now please have a look into the demo scripts to learn about the features of this package
  • Add special characters like ".", "?", ...
  • Optionally, read the whole line instead of single words
  • Improve inference speed

htrpipeline's People

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.