Skip to main content

Python wrapper for the LESIM-Co-Ltd/CoreOCR Swift library, enabling macOS Vision OCR from Python.

Project description

pyCoreOCR

Python wrapper for the CoreOCR Swift library, providing easy access to macOS Vision framework's text recognition capabilities from Python.

Features

  • Recognize text from image files (PNG, JPG, etc.) and PDF documents.
  • Supports specifying recognition languages or using automatic detection.
  • Configurable recognition level (accurate vs. fast).
  • Option to preserve text order for PDF files.

Requirements

  • macOS: This library relies heavily on Apple's Vision and PDFKit frameworks, which are only available on macOS (10.15 Catalina or later).
  • Xcode Command Line Tools or Swift Toolchain: Required to build the underlying Swift library during installation.
  • Python: >= 3.8 (as specified in pyproject.toml)

Installation

From PyPI (Recommended once published):

pip install pycoreocr

From Source (Current method):

  1. Clone the repository including the submodule:

    git clone --recurse-submodules https://github.com/LESIM-Co-Ltd/pyCoreOCR.git
    cd pyCoreOCR
    

    (If you cloned without --recurse-submodules, run git submodule update --init --recursive inside the pyCoreOCR directory.)

  2. Install the package. This will build the Swift library first:

    pip install .
    

    (For development, use pip install -e .)

Usage

import pyCoreOCR

image_path = "path/to/your/image.png"
pdf_path = "path/to/your/document.pdf"

try:
    # Recognize text from an image (specify English and Japanese)
    image_text = pyCoreOCR.recognize_text(image_path, languages=['en-US', 'ja-JP'])
    print("--- Image Text ---")
    print(image_text)

    # Recognize text from a PDF (using default settings: accurate, preserve order)
    pdf_text = pyCoreOCR.recognize_text(pdf_path)
    print("\n--- PDF Text (Accurate, Preserved Order) ---")
    # print(pdf_text) # Potentially very long output
    print(pdf_text[:500] + "...") # Print first 500 characters

    # Recognize text from PDF faster (but potentially less accurate/stable)
    # Note: level='fast' might cause issues with some PDFs.
    # pdf_text_fast = pyCoreOCR.recognize_text(pdf_path, level='fast', preserve_order=False)
    # print("\n--- PDF Text (Fast, Order Not Preserved) ---")
    # print(pdf_text_fast[:500] + "...")

except FileNotFoundError:
    print("Error: Input file not found.")
except Exception as e:
    print(f"An error occurred: {e}")

Parameters for recognize_text:

  • file_path (str): Path to the image or PDF file.
  • languages (Optional[List[str]]): List of language codes (e.g., ["en-US", "ja-JP"]) for recognition. Defaults to None (auto-detection).
  • level (str): Recognition level, either 'accurate' (default) or 'fast'. 'fast' might be unstable for some PDFs.
  • preserve_order (bool): For PDF files, whether to process pages sequentially to preserve text order (default: True).

Development

  1. Clone the repository with submodules:
    git clone --recurse-submodules https://github.com/LESIM-Co-Ltd/pyCoreOCR.git
    cd pyCoreOCR
    
  2. Install in editable mode:
    pip install -e .
    

This allows you to modify the Python code and have the changes reflected without reinstalling. If you modify the Swift code in the CoreOCR submodule, you need to run pip install -e . again to rebuild the Swift library.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

(Contributions are welcome! Please refer to contribution guidelines - if any)

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

pycoreocr-0.3.0.tar.gz (717.5 kB view details)

Uploaded Source

Built Distribution

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

pycoreocr-0.3.0-py3-none-any.whl (37.3 kB view details)

Uploaded Python 3

File details

Details for the file pycoreocr-0.3.0.tar.gz.

File metadata

  • Download URL: pycoreocr-0.3.0.tar.gz
  • Upload date:
  • Size: 717.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for pycoreocr-0.3.0.tar.gz
Algorithm Hash digest
SHA256 cbff58ec703efc529b11f64ce2d8107ed2d8af0ac2f3e3aa14b20c349e7c9c58
MD5 26984454061019a4c9e8b347ad86cb1d
BLAKE2b-256 e85ba7e6d361970c0b6178339e92d73564f817aafbeb2c0ddeca57d3bb77387c

See more details on using hashes here.

File details

Details for the file pycoreocr-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pycoreocr-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 37.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for pycoreocr-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43fdea760228a4097669ccd712021093a6493b466596dea7033ae1cd97d1bc69
MD5 5515a6b060484670fe6221acf6ffce5f
BLAKE2b-256 235e68ededd029209419b551108a421687800300b43c2e06e41c3d3dfd22f77c

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