Skip to main content

A color-based OCR library using EasyOCR

Project description

ColorOCR

ColorOCR is a Python library that uses EasyOCR to perform optical character recognition (OCR) on image regions based on specified colors.
It supports two primary modes:

  1. Filtered Mode: Perform OCR only on regions matching specified colors.
  2. Highlight Mode: Perform OCR on the entire image and then determine whether each text region matches the specified color.

Additionally, ColorOCR provides error feedback and visualization utilities to help you debug and present OCR results.


Features

  • Color-based Filtering:
    Extract text only from regions matching a specified target color (e.g. "red", "deep_red").

  • Highlight Mode:
    Perform OCR on the entire image and then return only the text regions that match the specified target color.

  • Default Presets:
    The library comes with default presets for common colors:

    • red
    • black
    • blue
    • green
    • yellow
    • orange
    • white
    • gray
      Users can add custom presets via the API.
  • Configurable Defaults:
    When creating a ColorOCR instance, you must specify:

    • The default color space (default_color_mode), e.g. "HSV" or "RGB".
    • The default threshold for color matching (default_ratio_threshold).
    • The default target color (default_target_color) is required.
      If not provided, a ValueError is raised, ensuring OCR always has a defined target color.
  • Error Feedback:
    If an error occurs (e.g. a preset is not found or a target color is not specified), the library outputs clear error messages in English via the error_feedback() method.

  • Visualization:
    The visualize_results() method draws bounding boxes and OCR text on the image, which is useful for debugging or presentation.


Installation

Using pip

Once published on PyPI, install via:

pip install ColorOCR

API Reference

Class: ColorOCR

Constructor

ColorOCR(languages=['en'], gpu=True, verbose=False,
         default_ratio_threshold=0.5, default_color_mode='HSV',
         default_target_color=<target_color>)
  • languages: List of language codes for EasyOCR (default: ['en']).
  • gpu: Boolean flag; set to True to use GPU if available.
  • verbose: Enable verbose output from EasyOCR.
  • default_ratio_threshold: Float, default threshold for color matching (default: 0.5).
  • default_color_mode: String, the color space to use ("HSV" or "RGB", default: "HSV").
  • default_target_color: (Required) String, the target color (e.g. "red", ).
    If not provided, a ValueError is raised.

Methods

  • add_color_preset(name, bounds, mode='HSV')
    Add or update a custom color preset.

    • name: Preset name (e.g. "my_red").
    • bounds: List of tuples of lower and upper bounds (e.g. [([0, 40, 40], [20, 255, 255])]).
    • mode: Color space, "HSV" or "RGB".
  • remove_color_preset(name)
    Remove a custom preset by name.

  • list_color_presets()
    Returns a dictionary of available presets (both default and user-defined).

  • get_preset_bounds(target_color, color_mode='HSV')
    Retrieves the preset bounds for the given target color and color space.
    If not found, an error message is output via error_feedback().

  • create_color_mask(image, color_mode='HSV', target_color, lower_bound=None, upper_bound=None)
    Creates a mask from the image based on the specified target color (or directly specified bounds).
    Note: If no target color is specified, an error is output and None is returned.

  • preprocess_for_ocr(image)
    Converts the input image to grayscale for OCR processing.

  • ocr_filtered(image, color_mode=None, target_color, lower_bound=None, upper_bound=None)
    Performs OCR on the filtered region (only where the target color is detected).
    Returns a tuple: (OCR results, mask, filtered image).
    Note: Target color must be specified (or provided via the default).

  • region_matches_color(region, color_mode=None, target_color, ratio_threshold=None)
    Checks if the given region matches the specified target color above the given threshold.
    Returns True or False.

  • region_matched_colors(region, color_mode=None, target_colors, ratio_threshold=None)
    For a given region, returns a list of target colors that match based on the threshold.

  • ocr_highlight(image, color_mode=None, target_color, ratio_threshold=None)
    Performs OCR on the entire image and then filters text regions based on whether they match the specified target color.
    Returns a list of dictionaries, each containing:

    • 'bbox': Bounding box as (x, y, width, height).
    • 'text': Recognized text.
    • 'confidence': OCR confidence.
    • 'matched_colors': List of target colors that match the region.
  • visualize_results(image, results, box_color=(0, 255, 0), text_color=(0, 255, 0), thickness=2, font_scale=0.8)
    Draws bounding boxes and OCR text on the image using the results from either ocr_filtered or ocr_highlight.
    Returns the image with drawn results.

Setter Methods

  • set_default_ratio_threshold(threshold)
    Updates the default ratio threshold for color matching.

  • set_default_color_mode(color_mode)
    Updates the default color space (e.g., "HSV" or "RGB").

  • set_default_target_color(target_color)
    Updates the default target color.
    Note: This cannot be set to None.

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

colorocr-0.1.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

ColorOCR-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file colorocr-0.1.0.tar.gz.

File metadata

  • Download URL: colorocr-0.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.12.0 requests/2.31.0 setuptools/68.0.0 requests-toolbelt/1.0.0 tqdm/4.67.1 CPython/3.9.0

File hashes

Hashes for colorocr-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0b40d6335968cf8889c69ca768797aebaeb43a89c7362663684b6efb57cc2033
MD5 ad9161ef1e813c2a7ffe94e786bac3a0
BLAKE2b-256 85dcd8337dc75658f39b648e339edfe66a47c529e6c8e02bf4b69ecf0ea53e95

See more details on using hashes here.

File details

Details for the file ColorOCR-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ColorOCR-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.12.0 requests/2.31.0 setuptools/68.0.0 requests-toolbelt/1.0.0 tqdm/4.67.1 CPython/3.9.0

File hashes

Hashes for ColorOCR-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 21b3ca748b3eece0371480cd4697828f036f0ece709ee8244dacf97366d5ab07
MD5 2c0b963629c302a4cab284cc88692b9e
BLAKE2b-256 0c4a5f3aa88fd210007bf89d2f816e0cfdcc6088b3fda14a0f15cc40ae26dfa9

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