Attention
Attention is a production-ready, high-accuracy Python library for automatically detecting and correcting document and image orientation using PaddleOCR.
Designed specifically for document intelligence, identity card verification (PAN cards, Aadhar, Passports, Driving Licenses), and general OCR pipelines, Attention handles complex rotations (0°, 90°, 180°, 270°, and fine fractional skew) with mathematical precision.
🌟 Key Features
- 📐 Dynamic Short-Edge Midpoint Axis Selection: Calculates true text reading directions regardless of initial word rotation.
- 🎯 Hybrid Deep-Learning & Geometric Deskew: Combines PaddleOCR's
PP-LCNet_x1_0_doc_oricoarse orientation classifier with sub-degree vector geometric deskew. - 🛡️ 180° Ambiguity Disambiguation: Evaluates text line crops to resolve upside-down text without false flips.
- 🔠 Noise-Resilient Text Filtering: Prioritizes non-ambiguous words ($\ge 5$ alphabetic letters) and automatically filters out phone UI system buttons (
"Delete","Share","Edit", etc.). - ⚡ High-Performance Downscaling Pre-Processing: Downscales large photos during OCR detection (
MAX_DETECTION_DIM = 960) for 3x–5x faster CPU execution. - 🦥 Lazy Initialization: Models load lazily on first use to ensure fast import times.
- 🖼️ In-Memory & Batch Ready: Fully supports in-memory NumPy arrays and OpenCV image buffers without writing temporary files to disk.
📦 Installation
Basic Installation (CPU)
pip install attention
Installation with GPU Support
pip install attention[gpu]
Local Development Installation
git clone https://github.com/girijageddavalasa/attention.git
cd Attention
pip install -e .
🚀 Quick Start
1. Simple Image Uprighting
import cv2
from attention import upright_image
# Load a rotated image
image = cv2.imread("rotated_document.jpg")
# Upright the image
uprighted = upright_image(image)
# Save the corrected image
cv2.imwrite("upright_document.jpg", uprighted)
2. Get Rotation Angle & Detailed Debug Info
from attention import upright_image
uprighted, angle, debug = upright_image(
image,
return_angle=True,
return_debug=True
)
print(f"Rotation Applied: {angle:.2f}°")
print(f"Detected Text: '{debug['detected_text']}' (Conf: {debug['confidence']:.4f})")
print(f"Ambiguity Debug: {debug['ambiguity_debug']}")
⚙️ Configuration Options
Customize pipeline parameters using the Config class:
from attention import Config
# Downscale max dimension for 3x-5x faster OCR detection (default: 960)
Config.MAX_DETECTION_DIM = 960
# Enable/disable bilateral pre-filtering (default: True)
Config.USE_BILATERAL_FILTER = True
# Minimum OCR confidence threshold
Config.OCR_MIN_CONF = 0.0
🧠 How It Works
- Bilateral Filtering & Downscaling: Smooths background noise and resizes high-resolution photos for high-speed text detection.
- Text Line Detection & Candidate Selection: Runs PaddleOCR and filters candidate text lines, bypassing phone UI labels and ambiguous letter sequences (
IONSXZ). - Short-Edge Midpoint Vector Axis: Calculates vector start/end points along the shortest edge pairs of the bounding box to find true text baseline direction.
- Coarse & Fine Rotation Matrix: Combines document orientation classifier angle ($0^\circ, 90^\circ, 180^\circ, 270^\circ$) with fine-grained tilt ($\theta$).
- Noise-Resilient 180° Verification: Evaluates cropped word ROI orientations to prevent floating-point noise flips.
- Full-Canvas Affine Rotation: Rotates the original high-resolution image with expanded canvas boundaries to prevent corner cropping.
🛠️ Testing & API Server
Attention includes a built-in FastAPI test server with a live browser dashboard:
python test_api.py
Open http://localhost:8001 in your browser to test single images or process entire image folders with live real-time side-by-side gallery streaming.
📄 License
Distributed under the MIT License. See LICENSE for details.
👤 Author
Girija Geddavalasa - thatsgirijag@gmail.com
GitHub: github.com/girijageddavalasa/attention
Metadata
Release files for attention-upright 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| attention_upright-0.1.0.tar.gz | 17.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| attention_upright-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 31.8 kB
Release files / attention_upright-0.1.0.tar.gz
| Download URL | attention_upright-0.1.0.tar.gz |
|---|---|
| Size | 17.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a67f253e6d7b5384817b0604e6549bc43211da29f69a689c20887eec1e46fb3b
|
|
BLAKE2b-256 checksum How to use checksums |
64f1cf192016876109701b1d949ba2212a49ff7da659bc23b65244fd29d06d26
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.10
|
Release files / attention_upright-0.1.0-py3-none-any.whl
| Download URL | attention_upright-0.1.0-py3-none-any.whl |
|---|---|
| Size | 14.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
72341456fa5978c09a7c25c38222c9f1e9cd8e6ca03f87ad985500e44a3059bc
|
|
BLAKE2b-256 checksum How to use checksums |
aef08a3313e65cf65d3a155c9626e97a484d2747205f0fa74d6d587e6139b870
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.10
|
Security for attention-upright 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.