Binarize document images

Overview

Binarization

Binarization for document images

Examples

Introduction

This tool performs document image binarization (i.e. transform colour/grayscale to black-and-white pixels) for OCR using multiple trained models.

The method used is based on Calvo-Zaragoza/Gallego, 2018. A selectional auto-encoder approach for document image binarization.

Installation

Clone the repository, enter it and run

pip install .

Models

Pre-trained models can be downloaded from here:

https://qurator-data.de/sbb_binarization/

Usage

sbb_binarize \
  --patches \
  -m <directory with models> \
  <input image> \
  <output image>

Note In virtually all cases, the --patches flag will improve results.

To use the OCR-D interface:

ocrd-sbb-binarize --overwrite -I INPUT_FILE_GRP -O OCR-D-IMG-BIN -P model "/var/lib/sbb_binarization"
Comments
  • Handle input errors in exceptions

    Handle input errors in exceptions

    Hello I trying to use image input "sbb_binarize --patches -m ./models/model_bin_sbb_ens.h5 179681.png img.png"

    I get the following error " File "/home/lin/anaconda3/bin/sbb_binarize", line 8, in sys.exit(main()) File "/home/lin/anaconda3/lib/python3.5/site-packages/click/core.py", line 829, in call return self.main(*args, **kwargs) File "/home/lin/anaconda3/lib/python3.5/site-packages/click/core.py", line 782, in main rv = self.invoke(ctx) File "/home/lin/anaconda3/lib/python3.5/site-packages/click/core.py", line 1066, in invoke return ctx.invoke(self.callback, **ctx.params) File "/home/lin/anaconda3/lib/python3.5/site-packages/click/core.py", line 610, in invoke return callback(*args, **kwargs) File "/home/lin/anaconda3/lib/python3.5/site-packages/sbb_binarize/cli.py", line 16, in main SbbBinarizer(model_dir).run(image_path=input_image, use_patches=patches, save=output_image) File "/home/lin/anaconda3/lib/python3.5/site-packages/sbb_binarize/sbb_binarize.py", line 265, in run img_last[:, :][img_last[:, :] > 0] = 255 TypeError: 'int' object is not subscriptable"

    documentation 
    opened by hiyashi-CianDuo 21
  • how to use sbb_binarization within a script?

    how to use sbb_binarization within a script?

    Hi. After searching for numerous hours without success, I am wondering if someone might offer insight on how to run this from within a python script.

    (Using Windows 10 os, Visual Studio Code) For example, I can run the following successfully from the terminal: sbb_binarize --patches -m 'C:/Users/Scott/Desktop/Python2/sbb_binarization/models' 'C:/Users/Scott/Desktop/Python2/Kpics/Pages_cropped/061r.png' 'C:/Users/Scott/Desktop/Python2/Kpics/new_test8.png' However, if I try the following script (using CodeRunner extension):

    import subprocess
    def sbb_def():
        args = ['sbb_binarize', '--patches', '-m', 'C:/Users/Scott/Desktop/Python2/sbb_binarization/models', 'C:/Users/Scott/Desktop/Python2/Kpics/Pages_cropped/061r.png', 'C:/Users/Scott/Desktop/Python2/Kpics/new_test8.png']
        subprocess.Popen(args)
    sbb_def()
    

    I get the following:

    [Running] C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3 & python "c:\Users\Scott\Desktop\Python2\my_sbb_binarization_example.py"
    Traceback (most recent call last):
      File "c:\Users\Scott\Desktop\Python2\my_sbb_binarization_example.py", line 8, in <module>
        sbb_def()
      File "c:\Users\Scott\Desktop\Python2\my_sbb_binarization_example.py", line 6, in sbb_def
        subprocess.Popen(args)
      File "C:\ProgramData\Anaconda3\lib\subprocess.py", line 854, in __init__
        self._execute_child(args, executable, preexec_fn, close_fds,
      File "C:\ProgramData\Anaconda3\lib\subprocess.py", line 1307, in _execute_child
        hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
    FileNotFoundError: [WinError 2] The system cannot find the file specified
    
    [Done] exited with code=1 in 0.687 seconds
    

    I don't suggest that this is a bug or anything. I'm rather sure the "issue" is mine. I'm very green at python/coding in general. Any help would be greatly appreciated.

    opened by SB2020-eye 11
  • Cannot install sbb_binarization (on Windows) - TensorFlow not found (even, if available)

    Cannot install sbb_binarization (on Windows) - TensorFlow not found (even, if available)

    Hi, I am trying out to setup your nice tool in Windows environment. I am using Python 3.8. After doing "pip install sbb_binarization" I get the following error:

    Collecting ocrd>=2.18.0
      Using cached ocrd-2.20.1-py3-none-any.whl (51 kB)
    ERROR: Could not find a version that satisfies the requirement tensorflow<1.16,>=1.15 (from sbb_binarization) (from versions: 2.2.0rc1, 2.2.0rc2, 2.2.0rc3, 2.2.0rc4, 2.2.0, 2.2.1, 2.3.0rc0, 2.3.0rc1, 2.3.0rc2, 2.3.0, 2.3.1, 2.4.0rc0, 2.4.0rc1)
    ERROR: No matching distribution found for tensorflow<1.16,>=1.15 (from sbb_binarization)
    

    If i call "pip list", I can see, that TensorFlow is installed:

    ...
    setuptools             41.2.0
    six                    1.14.0
    stomp.py               6.0.0
    tensorboard            2.4.0
    tensorboard-plugin-wit 1.7.0
    tensorflow             2.3.1
    tensorflow-estimator   2.3.0
    termcolor              1.1.0
    urllib3                1.26.2
    ...
    Do you have any idea, what to do?
    
    wontfix 
    opened by stefanCCS 11
  • Model won't load on Python 3.9

    Model won't load on Python 3.9

    Hey,

    After using this model for a while and having quite remarkable results as compared to standard binarization techniques, I would like to move to a newer version of python: 3.9.

    Unfortunately, the model won't load then as I get a ValueError: bad marshal data (unknown type code). To fix this I need the raw SBB model and load the weights there and save again in the newer python version.

    Is anyone aware of what the exact model is or where I can find it?

    Thanks! LudovA

    opened by LudovA 7
  • output is inverted in certain input formats

    output is inverted in certain input formats

    I sometimes get output which looks like this:

    OCR-D-SEG-PAGE-BIN_Ratsbuecher_O_10_0237 IMG-BIN

    The input image for this was a PNG (which someone seems to have converted somehow from an original JPEG):

    Ratsbuecher_O_10_0237

    (That's from this GT BTW.)

    bug 
    opened by bertsky 6
  • can't install

    can't install

    Hi. Running on Windows 10 OS. Using Visual Studio Code.

    Running (myenvironmentname) PS C:\users\scott\desktop\python2\sbb_binarization> pip install . I keep getting the following:

    Processing c:\users\scott\desktop\python2\sbb_binarization
        ERROR: Command errored out with exit status 1:
         command: 'C:\ProgramData\Anaconda3\envs\myenvironmentname\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Scott\\AppData\\Local\\Temp\\pip-req-build-l7egxsl1\\setup.py'"'"'; __file__='"'"'C:\\Users\\Scott\\AppData\\Local\\Temp\\pip-req-build-l7egxsl1\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\Scott\AppData\Local\Temp\pip-pip-egg-info-notlemz5'
             cwd: C:\Users\Scott\AppData\Local\Temp\pip-req-build-l7egxsl1\
        Complete output (5 lines):
        Traceback (most recent call last):
          File "<string>", line 1, in <module>
          File "C:\Users\Scott\AppData\Local\Temp\pip-req-build-l7egxsl1\setup.py", line 6, in <module>
            with open('./ocrd-tool.json', 'r') as f:
        FileNotFoundError: [Errno 2] No such file or directory: './ocrd-tool.json'
        ----------------------------------------
    WARNING: Discarding file:///C:/users/scott/desktop/python2/sbb_binarization. Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
    ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
    

    I'm very new at all this. And my (beginner) language is Python. I don't understand the json stuff. Any help getting this installed would be greatly appreciated. :)

    opened by SB2020-eye 6
  • [transformer_model_integration]

    [transformer_model_integration] "Normal" CLI does not produce useful output

    On the transformer_model_integration branch, the normal CLI does not produce useful output

    • I'm using the image from https://qurator-data.de/examples/actevedef_718448162.first-page.zip
    • This fails with a (transparent?) empty output TIFF: sbb_binarize --patches --model-dir ~/devel/qurator-data/sbb_binarization/2022-08-16/ OCR-D-IMG_00000024.tif OCR-D-IMG_00000024-bin.tif
    • This - the OCR-D CLI - works(!): ocrd-sbb-binarize -I OCR-D-IMG -O OCR-D-IMG-BIN -P model /home/mike/devel/qurator-data/sbb_binarization/2022-08-16
    bug 
    opened by mikegerber 4
  • strange border artifacts in patch mode

    strange border artifacts in patch mode

    I sometimes get output which looks like this:

    | input | output | | --- | --- | | Evakuierung_von_Polen_Ansieldung_WD_Lask_0168 IMG-NORM | Evakuierung_von_Polen_Ansieldung_WD_Lask_0168 IMG-BINSBB |

    Could this be a problem with the patch size or patching in general? Should I try to crop first?

    opened by bertsky 3
  • v0.0.7 is not on PyPi

    v0.0.7 is not on PyPi

    This just came up in our team meeting: Version 0.0.7 is not on PyPI.

    (Commit history also looks like a bug fix is not in the most recent GitHub release yet but I cannot say if that bug fix warrants a new release or not.)

    opened by mikegerber 3
  • Why is --patches not the default?

    Why is --patches not the default?

    The README says:

    Note In virtually all cases, applying the --patches flag will improve the quality of results.

    Why is it not the default? Why no --no-patches option instead?

    documentation 
    opened by mikegerber 2
  • Cannot load models in qurator-data git-annex

    Cannot load models in qurator-data git-annex

    $ ocrd-sbb-binarize --overwrite -I OCR-D-IMG -O OCR-D-IMG-BIN -P model /var/lib/sbb_binarization
    18:35:13.783 INFO processor.SbbBinarize - INPUT FILE 0 / PHYS_0024
    18:35:13.787 INFO processor.SbbBinarize - Binarizing on 'page' level in page 'PHYS_0024'
    /var/lib/sbb_binarization/.gitkeep
    Traceback (most recent call last):
      File "/usr/local/bin/ocrd-sbb-binarize", line 8, in <module>
        sys.exit(cli())
      File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 829, in __call__
        return self.main(*args, **kwargs)
      File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 782, in main
        rv = self.invoke(ctx)
      File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 1066, in invoke
        return ctx.invoke(self.callback, **ctx.params)
      File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 610, in invoke
        return callback(*args, **kwargs)
      File "/usr/local/lib/python3.6/dist-packages/sbb_binarize/ocrd_cli.py", line 115, in cli
        return ocrd_cli_wrap_processor(SbbBinarizeProcessor, *args, **kwargs)
      File "/usr/local/lib/python3.6/dist-packages/ocrd/decorators/__init__.py", line 81, in ocrd_cli_wrap_processor
        run_processor(processorClass, ocrd_tool, mets, workspace=workspace, **kwargs)
      File "/usr/local/lib/python3.6/dist-packages/ocrd/processor/helpers.py", line 69, in run_processor
        processor.process()
      File "/usr/local/lib/python3.6/dist-packages/sbb_binarize/ocrd_cli.py", line 66, in process
        bin_image = cv2pil(binarizer.run(image=pil2cv(page_image), use_patches=True))
      File "/usr/local/lib/python3.6/dist-packages/sbb_binarize/sbb_binarize.py", line 199, in run
        res = self.predict(model_in, image, use_patches)
      File "/usr/local/lib/python3.6/dist-packages/sbb_binarize/sbb_binarize.py", line 47, in predict
        model, model_height, model_width, n_classes = self.load_model(model_name)
      File "/usr/local/lib/python3.6/dist-packages/sbb_binarize/sbb_binarize.py", line 40, in load_model
        model = load_model(join(self.model_dir, model_name), compile=False)
      File "/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py", line 492, in load_wrapper
        return load_function(*args, **kwargs)
      File "/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py", line 583, in load_model
        with H5Dict(filepath, mode='r') as h5dict:
      File "/usr/local/lib/python3.6/dist-packages/keras/utils/io_utils.py", line 191, in __init__
        self.data = h5py.File(path, mode=mode)
      File "/usr/local/lib/python3.6/dist-packages/h5py/_hl/files.py", line 408, in __init__
        swmr=swmr)
      File "/usr/local/lib/python3.6/dist-packages/h5py/_hl/files.py", line 173, in make_fid
        fid = h5f.open(name, flags, fapl=fapl)
      File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
      File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
      File "h5py/h5f.pyx", line 88, in h5py.h5f.open
    OSError: Unable to open file (file signature not found)
    

    The directory /var/lib/sbb_binarization is a copy of sbb_binarization/ in our private qurator-data git-annex, which happens to include a file .gitkeep - which the current code tries to load as a HDF5 file.

    opened by mikegerber 2
  • packaging inconsistency

    packaging inconsistency

    If I run sbb_binarize --version, I get:

    Traceback (most recent call last):
      File "/bin/sbb_binarize", line 8, in <module>
        sys.exit(main())
      File "/lib/python3.6/site-packages/click/core.py", line 1128, in __call__
        return self.main(*args, **kwargs)
      File "/lib/python3.6/site-packages/click/core.py", line 1052, in main
        with self.make_context(prog_name, args, **extra) as ctx:
      File "/lib/python3.6/site-packages/click/core.py", line 914, in make_context
        self.parse_args(ctx, args)
      File "/lib/python3.6/site-packages/click/core.py", line 1370, in parse_args
        value, args = param.handle_parse_result(ctx, opts, args)
      File "/lib/python3.6/site-packages/click/core.py", line 2347, in handle_parse_result
        value = self.process_value(ctx, value)
      File "/lib/python3.6/site-packages/click/core.py", line 2309, in process_value
        value = self.callback(ctx, self, value)
      File "/lib/python3.6/site-packages/click/decorators.py", line 383, in callback
        ) from None
    RuntimeError: 'sbb_binarize' is not installed. Try passing 'package_name' instead.
    

    Looks like the name=sbb_binarization kwarg is not consistent with the top-level module sbb_binarize IINM.

    Maybe you want to restructure your package using qurator as namespace package on that occasion?

    opened by bertsky 1
  • Batch-prediction across multiple GPUs and more efficient patch-prediction

    Batch-prediction across multiple GPUs and more efficient patch-prediction

    In order to batch-binarize thousands of images, I've rewritten the prediction script to allow us to predict around 1500-2000 images per hour on a decent machine with two GPUs.

    The proposed changes include:

    • An efficient way to compute the image patches instead of a very inefficient loop
    • Complete removal of the prediction on the down-scaled image as the results are pretty much always worse
    • Batch-prediction code that can binarize an entire directory into a given output directory while preserving the folder structure and skipping images that have already been binarized, to allow stopping and continuing the conversion
    • Multiprocessing batch-prediction across multiple GPUs using the mpire library
    • A fix for the memory-leak that caused mass-binarization to very quickly crash because we were running out of memory on the GPU. With this fix, we are already running the conversion for 16 hours without any crash.
    • Simplified loading of the model removing obsolete session-handling code

    Please note: I know that the code looks completely different now (hopefully more readable) and is probably not 1:1 compatible with the remaining code in your repository, but I tried to put all the relevant changes into this PR and make the code as self-contained as possible to allow you to update the solution as you see fit.

    Thanks for sharing the code-base with us. I hope that this PR is of some help to you.

    opened by apacha 3
  • Saving to TIFF does not work

    Saving to TIFF does not work

    E.g.

    % sbb_binarize --patches --model-dir /home/mike/devel/qurator-data/sbb_binarization/2022-08-16/ OCR-D-IMG_00000024.tif OCR-D-IMG_00000024-bin.tif
    

    produces a transparent(?) TIFF with no content. No warning, no error.

    See also #46.

    bug 
    opened by mikegerber 3
  • Pinning library versions and adding Python version to README

    Pinning library versions and adding Python version to README

    This PR updates the requirements.txt (to pin specifically tested versions that actually work) as well as adding a tested Python version information to the README. Later versions will not work because of loading serialized h5 models causes marshalling errors.

    Also adding .idea directory to list of ignored directories.

    Fixes #39

    opened by apacha 4
  • Document supported Python versions

    Document supported Python versions

    sbb_binarization currently needs TensorFlow 2.4, which is not available* for Python 3.10, the default on my Linux installation. Which versions are supported?

    • as in: available on PyPI:
    ERROR: Could not find a version that satisfies the requirement tensorflow==2.4.* (from sbb-binarization) (from versions: 2.8.0rc0, 2.8.0rc1, 2.8.0, 2.8.1, 2.8.2, 2.9.0rc0, 2.9.0rc1, 2.9.0rc2, 2.9.0, 2.9.1)
    ERROR: No matching distribution found for tensorflow==2.4.*
    
    documentation 
    opened by mikegerber 6
Releases(v0.0.11)
Owner
QURATOR-SPK
Curation Technologies
QURATOR-SPK
Multi-choice answer sheet correction system using computer vision with opencv & python.

Multi choice answer correction 🔴 5 answer sheet samples with a specific solution for detecting answers and sheet correction. 🔴 By running the soluti

Reza Firouzi 7 Mar 07, 2022
The code for CVPR2022 paper "Likert Scoring with Grade Decoupling for Long-term Action Assessment".

Likert Scoring with Grade Decoupling for Long-term Action Assessment This is the code for CVPR2022 paper "Likert Scoring with Grade Decoupling for Lon

10 Oct 21, 2022
This is the implementation of the paper "Gated Recurrent Convolution Neural Network for OCR"

Gated Recurrent Convolution Neural Network for OCR This project is an implementation of the GRCNN for OCR. For details, please refer to the paper: htt

90 Dec 22, 2022
Characterizing possible failure modes in physics-informed neural networks.

Characterizing possible failure modes in physics-informed neural networks This repository contains the PyTorch source code for the experiments in the

Aditi Krishnapriyan 55 Jan 02, 2023
Official implementation of "An Image is Worth 16x16 Words, What is a Video Worth?" (2021 paper)

An Image is Worth 16x16 Words, What is a Video Worth? paper Official PyTorch Implementation Gilad Sharir, Asaf Noy, Lihi Zelnik-Manor DAMO Academy, Al

213 Nov 12, 2022
Character Segmentation using TensorFlow

Character Segmentation Segment characters and spaces in one text line,from this paper Chinese English mixed Character Segmentation as Semantic Segment

26 Aug 25, 2022
ocroseg - This is a deep learning model for page layout analysis / segmentation.

ocroseg This is a deep learning model for page layout analysis / segmentation. There are many different ways in which you can train and run it, but by

NVIDIA Research Projects 71 Dec 06, 2022
Autonomous Driving project for Euro Truck Simulator 2

hope-autonomous-driving Autonomous Driving project for Euro Truck Simulator 2 Video: How is it working ? In this video, the program processes the imag

Umut Görkem Kocabaş 36 Nov 06, 2022
nofacedb/faceprocessor is a face recognition engine for NoFaceDB program complex.

faceprocessor nofacedb/faceprocessor is a face recognition engine for NoFaceDB program complex. Tech faceprocessor uses a number of open source projec

NoFaceDB 3 Sep 06, 2021
Virtual Zoom Gesture using OpenCV

Virtual_Zoom_Gesture I have created a virtual zoom gesture where we can Zoom in and Zoom out any image and even we can move that image anywhere on the

Mudit Sinha 2 Dec 26, 2021
Hand gesture detection project with aweome UI implementation.

an awesome hand gesture detection project for you to be creative! Imagination is the limit to do with this project.

AR Ashraf 39 Sep 26, 2022
M-LSDを用いて四角形を検出し、射影変換を行うサンプルプログラム

M-LSD-warpPerspective-Example M-LSDを用いて四角形を検出し、射影変換を行うサンプルプログラムです。 Requirements OpenCV 3.4.2 or Later tensorflow 2.4.1 or Later Usage 実行方法は以下です。 pytho

KazuhitoTakahashi 9 Oct 14, 2022
OCR system for Arabic language that converts images of typed text to machine-encoded text.

Arabic OCR OCR system for Arabic language that converts images of typed text to machine-encoded text. The system currently supports only letters (29 l

Hussein Youssef 144 Jan 05, 2023
Here use convulation with sobel filter from scratch in opencv python .

Here use convulation with sobel filter from scratch in opencv python .

Tamzid hasan 2 Nov 11, 2021
Converts an image into funny, smaller amongus characters

SussyImage Converts an image into funny, smaller amongus characters Demo Mona Lisa | Lona Misa (Made up of AmongUs characters) API I've also added an

Dhravya Shah 14 Aug 18, 2022
Repositório para registro de estudo da biblioteca opencv (Python)

OpenCV (Python) Objetivo do Repositório: Registrar avanços no estudo da biblioteca opencv. O repositório estará aberto a qualquer pessoa e há tambem u

1 Jun 14, 2022
2 telegram-bots: for image recognition and for text generation

💻 📱 Telegram_Bots 🔎 & 📖 2 telegram-bots: for image recognition and for text generation. About Image recognition bot: User sends a photo and bot de

Marina Polukoshko 1 Jan 27, 2022
SCOUTER: Slot Attention-based Classifier for Explainable Image Recognition

SCOUTER: Slot Attention-based Classifier for Explainable Image Recognition PDF Abstract Explainable artificial intelligence has been gaining attention

87 Dec 26, 2022
Unofficial implementation of "TableNet: Deep Learning model for end-to-end Table detection and Tabular data extraction from Scanned Document Images"

TableNet Unofficial implementation of ICDAR 2019 paper : TableNet: Deep Learning model for end-to-end Table detection and Tabular data extraction from

Jainam Shah 243 Dec 30, 2022
Code for CVPR 2022 paper "SoftGroup for Instance Segmentation on 3D Point Clouds"

SoftGroup We provide code for reproducing results of the paper SoftGroup for 3D Instance Segmentation on Point Clouds (CVPR 2022) Author: Thang Vu, Ko

Thang Vu 231 Dec 27, 2022