A lightweight Django-based OCR package
Project description
django-ocr
A lightweight Django-based OCR package that extracts text from images and allows dynamic mapping of extracted text into structured dictionaries.
Features
- Extracts text from images using Tesseract OCR
- Supports dynamic key-value mapping for structured text output
- Simple API for easy integration into Django projects
- Fully customizable mapping to extract expected information
Installation
You can install django-ocr using pip:
pip install django-ocr
Dependencies
Make sure you have Tesseract-OCR installed on your system. If not, install it using:
- Ubuntu/Debian:
sudo apt install tesseract-ocr
- MacOS:
brew install tesseract
- Windows: Download and install from Tesseract's official site.
Usage
1. Extract Text from an Image
from django_ocr import process_image
text = process_image("sample_image.png")
print(text) # Output: Extracted text from the image
2. Extract and Map Text to Structured Dictionary
If you have an image containing structured information, such as:
Name: John
Last Name: Doe
You can map it dynamically:
from django_ocr import process_image_with_mapping
image_path = "sample_image.png"
mappings = {
"first_name": ["Name"],
"last_name": ["Last Name"]
}
result = process_image_with_mapping(image_path, mappings)
print(result)
# Output: {'first_name': 'John', 'last_name': 'Doe'}
API Reference
process_image(image_path: str) -> str
Extracts text from an image and returns it as a string.
process_image_with_mapping(image_path: str, mappings: dict) -> dict
Extracts text from an image and maps values to a structured dictionary based on provided mappings.
image_path: Path to the image filemappings: A dictionary where keys represent desired output fields, and values are lists of keywords to search for
Customization
You can define your own mappings dynamically based on the expected text format.
Example for extracting an ID and an email:
mappings = {
"user_id": ["ID", "User ID"],
"email": ["Email"]
}
Contributing
Feel free to open issues or submit pull requests to improve this package!
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_ocr-0.0.0.tar.gz.
File metadata
- Download URL: django_ocr-0.0.0.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f5bc289f10df2ca64b5381d9cdf0fda7d26e1bb656c7b19b3919bca8f52fcd3
|
|
| MD5 |
95fa16a1e64acc8b64b21763b28bb740
|
|
| BLAKE2b-256 |
2c766aaf9c1243467104109eda0624d1679def1fa143d277d042b053dc80c73d
|
File details
Details for the file django_ocr-0.0.0-py3-none-any.whl.
File metadata
- Download URL: django_ocr-0.0.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
445423520de1385d35c0b66629b72e8b0e392c763002c2ab39e683410b60820f
|
|
| MD5 |
3be1d5d033a11b1d01429ea0d5515cc5
|
|
| BLAKE2b-256 |
426df2438b4fdce7d505b929414db6aaca58d3af23e6291c636e60f3667d02b5
|