tessl install tessl/pypi-paddleocr@3.3.0Industry-leading OCR and document AI engine that converts documents and images into structured, AI-friendly data formats with comprehensive solutions from text extraction to intelligent document understanding.
Document preprocessing pipeline for correcting orientation and unwarping distorted documents. Automatically detects and corrects document rotation, and removes perspective distortion or curvature from photos of documents.
class DocPreprocessor:
"""
Document preprocessing for orientation and distortion correction.
Combines orientation classification and document unwarping
to prepare scanned or photographed documents for OCR.
"""
def __init__(
self,
doc_orientation_classify_model_name: str = None,
doc_orientation_classify_model_dir: str = None,
doc_unwarping_model_name: str = None,
doc_unwarping_model_dir: str = None,
use_doc_orientation_classify: bool = None,
use_doc_unwarping: bool = None,
paddlex_config: str = None,
device: str = None,
use_hpi: bool = None,
**kwargs
):
"""
Initialize DocPreprocessor pipeline.
Args:
doc_orientation_classify_model_name (str, optional): Orientation model
Default: 'PP-LCNet_x1_0_doc_ori'
doc_unwarping_model_name (str, optional): Unwarping model
Default: 'UVDoc'
use_doc_orientation_classify (bool, optional): Enable orientation correction
use_doc_unwarping (bool, optional): Enable document unwarping
device (str, optional): Device for inference ('cpu', 'gpu')
use_hpi (bool, optional): Use high-performance inference
paddlex_config (str or dict, optional): Configuration file or dict
"""def predict(
self,
input,
*,
use_doc_orientation_classify: bool = None,
use_doc_unwarping: bool = None
) -> list:
"""
Preprocess document images.
Args:
input: Image/PDF path, numpy array, PIL Image, directory, or list
use_doc_orientation_classify (bool, optional): Override orientation correction
use_doc_unwarping (bool, optional): Override unwarping
Returns:
list: Preprocessed images with correction information
"""
def predict_iter(
self,
input,
*,
use_doc_orientation_classify: bool = None,
use_doc_unwarping: bool = None
):
"""
Iterate over preprocessing results for memory efficiency.
Args:
input: Image/PDF path, numpy array, PIL Image, directory, or list
use_doc_orientation_classify (bool, optional): Override orientation correction
use_doc_unwarping (bool, optional): Override unwarping
Yields:
dict: Preprocessed result for each image
"""
def close(self) -> None:
"""Close the pipeline and free resources."""
def export_paddlex_config_to_yaml(self, yaml_path: str) -> None:
"""Export configuration to YAML."""from paddleocr import DocPreprocessor
import cv2
# Initialize with orientation correction only
preprocessor = DocPreprocessor(
use_doc_orientation_classify=True,
use_doc_unwarping=False
)
# Correct rotated documents
result = preprocessor.predict('rotated_doc.jpg')
# Save corrected image
corrected_img = result[0]['img']
cv2.imwrite('corrected_doc.jpg', corrected_img)
preprocessor.close()from paddleocr import DocPreprocessor
import cv2
# Initialize with unwarping only
preprocessor = DocPreprocessor(
use_doc_orientation_classify=False,
use_doc_unwarping=True
)
# Unwarp curved or perspective-distorted document
result = preprocessor.predict('curved_doc.jpg')
# Save unwarped image
unwarped_img = result[0]['img']
cv2.imwrite('unwarped_doc.jpg', unwarped_img)
preprocessor.close()from paddleocr import DocPreprocessor
import cv2
# Initialize with both corrections
preprocessor = DocPreprocessor(
use_doc_orientation_classify=True,
use_doc_unwarping=True
)
# Process document with both orientation and distortion issues
result = preprocessor.predict('phone_photo.jpg')
# Access corrected image
preprocessed = result[0]
print(f"Original orientation: {preprocessed.get('orientation_angle', 'N/A')}")
print(f"Applied unwarping: {preprocessed.get('unwarping_applied', False)}")
cv2.imwrite('preprocessed_doc.jpg', preprocessed['img'])
preprocessor.close()from paddleocr import DocPreprocessor
import cv2
import os
preprocessor = DocPreprocessor(
use_doc_orientation_classify=True,
use_doc_unwarping=True
)
# Process directory of scanned documents
results = preprocessor.predict('scanned_docs/')
# Save all preprocessed images
os.makedirs('preprocessed/', exist_ok=True)
for result in results:
input_name = os.path.basename(result['input_path'])
output_path = os.path.join('preprocessed', input_name)
cv2.imwrite(output_path, result['img'])
preprocessor.close()from paddleocr import DocPreprocessor
import cv2
preprocessor = DocPreprocessor(
use_doc_orientation_classify=True,
use_doc_unwarping=True
)
# Process large batch with iterator
for result in preprocessor.predict_iter('large_document_set/'):
# Process one at a time
corrected = result['img']
# Do something with the image
cv2.imwrite(f"output/{result['input_path']}", corrected)
preprocessor.close()from paddleocr import DocPreprocessor
preprocessor = DocPreprocessor()
# Only correct orientation for some images
rotated_results = preprocessor.predict(
'rotated_docs/',
use_doc_orientation_classify=True,
use_doc_unwarping=False
)
# Only unwarp for others
curved_results = preprocessor.predict(
'curved_docs/',
use_doc_orientation_classify=False,
use_doc_unwarping=True
)
preprocessor.close()[
{
"input_path": "path/to/document.jpg",
"img": numpy.ndarray, # Preprocessed image (H, W, C)
"orientation_angle": 90, # Detected rotation (0, 90, 180, 270)
"orientation_score": 0.98, # Confidence of orientation detection
"orientation_applied": True, # Whether rotation was applied
"unwarping_applied": True, # Whether unwarping was applied
"original_shape": (1024, 768, 3), # Original image dimensions
"preprocessed_shape": (1024, 768, 3) # Final image dimensions
}
]device='gpu' for faster processing of large batchespredict_iter() for large datasets to reduce memory usage