Skip to main content

Fast DotCode 2D barcode decoder

Project description

DotCode Decoder

Sample decode

PyPI Version Python Versions License: MIT Downloads

Fast, accurate DotCode 2D barcode decoder for industrial and commercial applications.(10*47-grid)

DotCode Decoder is a pure Python/OpenCV library for reading DotCode symbols from photos of dot-peened, laser-etched, or printed parts — built for production-line conditions where lighting is inconsistent and marks vary in clarity.

Table of contents

Features

  • Sub-50ms average decode time on real-world industrial images
  • ~95%+ decode success rate out of the box
  • Iterative per-row regression for skew correction, with early exit once the estimate stabilizes
  • Adaptive thresholding (Otsu, fixed, and optional local/Sauvola) for uneven lighting
  • Reed-Solomon error correction over GF(113)
  • Configurable timeout and confidence thresholds for production tuning
  • Simple one-line API for single images, plus a DotCodeDecoder class for full control

Installation

pip install dotcode-decoder

Requires Python 3.7+. See Requirements for dependency details.

Quick start

from dotcode import decode, DecodeResult

# Single image
result = decode("image.jpg")
print(result.message)  # "25E13KXUKSXG1313"

# With full details
result: DecodeResult = decode("image.jpg")
print(f"Message: {result.message}")
print(f"Time: {result.elapsed_ms:.0f}ms")
print(f"Confidence: {result.confidence:.2f}")
print(f"Method: {result.method}")

API reference

decode()

The simplest way to decode a DotCode image.

from dotcode import decode

result = decode(
    image,                    # str (path) or np.ndarray
    target_length: int = 14,  # Fixed output length
    pad_char: str = '0',      # Padding character
    truncate: bool = True,    # Truncate if too long
    timeout_ms: int = 300,    # Max time per image
    confidence: float = 0.25, # Confidence threshold
    fast_mode: bool = True,   # Speed vs. accuracy
    use_sauvola: bool = False # Local thresholding
)

DotCodeDecoder

For full control over decoding parameters.

from dotcode import DotCodeDecoder

decoder = DotCodeDecoder(
    # Timing
    timeout_ms: int = 300,        # Max time per image (ms)

    # Quality
    confidence: float = 0.25,     # Minimum confidence (0.20-0.40)

    # Mode
    fast_mode: bool = True,       # True = faster, False = more accurate

    # Preprocessing
    use_sauvola: bool = False,    # Local thresholding for poor lighting

    # Output
    target_length: int = 14,      # Fixed output length
    pad_char: str = '0',          # Padding character
    truncate: bool = True,        # Truncate if too long
)

result = decoder.decode("image.jpg")

DecodeResult

The result object returned by both decode() and DotCodeDecoder.decode().

Field Type Description
success bool Whether decoding succeeded
message str Decoded text
confidence float Confidence score (0-1)
stage DecodeStage REGRESSION, ANGLE_SWEEP, or INVERT
method str Specific method used (e.g. "reg_iter_0.0")
elapsed_ms float Time taken, in milliseconds
file str Filename (batch mode)
error str Error message, if decoding failed
timed_out bool Whether the timeout was hit

Advanced configuration

Custom threshold priority

Override auto-threshold selection:

decoder = DotCodeDecoder(
    fixed_threshold=155,   # Custom threshold (105-175)
    use_otsu=False         # Disable Otsu auto-threshold
)

Custom angle sweep

Override the default angle sweep values:

decoder = DotCodeDecoder(
    custom_angles=[1.0, 0.5, -0.5, -1.0, 2.0, -2.0]
)

Fixed output length

Force exactly 14 characters:

decoder = DotCodeDecoder(
    target_length=14,      # Default
    pad_char='0',          # Pad with zeros
    truncate=True          # Truncate if longer
)

# Or via decode()
result = decode("image.jpg", target_length=14)

Performance

Metric Value
Accuracy 95.5% (378/396 images)
Average speed ~47ms per image
Timeout 300ms (configurable)

Performance by stage

Stage Images Description
Regression 103 (27%) Fast path — direct grid fit
Angle sweep 275 (73%) Slower path — angle correction
Invert fallback 0 (0%) Last resort for inverted images

How it works

  1. Dot detection — extracts dot centroids using adaptive thresholding (Otsu/fixed) with morphological erosion to separate touching dots.
  2. Iterative regression — straightens tilted/skewed grids using per-row polynomial fitting, with early exit once the angle estimate stabilizes.
  3. Grid snapping — fits detected dots to a 10×47 checkerboard grid via k-means clustering.
  4. Reed-Solomon error correction — corrects errors in the extracted bitstream over GF(113), handling several errors per code.
  5. Text decoding — converts corrected values to readable text using DotCode code sets (A, B, C) via a character-encoding state machine.

Algorithm details

Reed-Solomon error correction

  • Field: GF(113)
  • Primitive element: 3
  • Corrects several errors per code via the Berlekamp-Massey algorithm

Iterative regression

  • Configurable iteration count (default: a few, with early exit)
  • Early exit when angle change < 0.1°
  • Per-row quadratic fit for curvature correction

Threshold selection

  • Otsu's method (automatic)
  • Fixed thresholds spanning the typical brightness range
  • Optional local/Sauvola thresholding for uneven lighting

Requirements

  • Python 3.7+
  • OpenCV >= 4.5.0
  • NumPy >= 1.19.0
  • SciPy >= 1.7.0 (for KDTree)

Contributing

Contributions are welcome. To propose a change:

  1. Open an issue describing the bug or feature before starting significant work.
  2. Fork the repo and create a branch for your change.
  3. Add or update tests in test.py covering the change.
  4. Open a pull request with a clear description of what changed and why.

Please run the full test suite locally before submitting a PR.

License

MIT License — see LICENSE for details.

References

Acknowledgments

This project builds on research in 2D barcode decoding and industrial computer vision. Special thanks to:

  • The open-source community behind OpenCV, NumPy, and SciPy
  • AIM Global for the DotCode specification
  • Contributors who helped test and improve the decoder

Questions or support: open a GitHub issue or email anishkarki989@gmail.com.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dotcode_decoder-1.0.1.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dotcode_decoder-1.0.1-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file dotcode_decoder-1.0.1.tar.gz.

File metadata

  • Download URL: dotcode_decoder-1.0.1.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.0

File hashes

Hashes for dotcode_decoder-1.0.1.tar.gz
Algorithm Hash digest
SHA256 e9fe42300a0e0b7c656675fa46203f1df1fff6e569c44e51cb3f76fe8e1c2f38
MD5 b970dbbf80360befad4edfe4ff9dc23c
BLAKE2b-256 e39b605a074172be713bf01bde333ac20eff4cabd650353d4d4f2e1021ed6882

See more details on using hashes here.

File details

Details for the file dotcode_decoder-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for dotcode_decoder-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bb15394819706f8764bb90e34eb8217b6e3401428be27220252e6ae334bdfdce
MD5 d2565279b0c097d3ad9a5b3a6b3408e5
BLAKE2b-256 3f041246d6855cb5ad11a2bb45e4bb5e81337206becbe1740ca2764b2350cbbf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page