Add Lab gamma darkening and optional 1.5x cubic upscale (crops under 1800px) so faint TD-style ribbon ink is readable without slowing sharp captures. Add .gitleaks.toml so the pre-commit secret scan can load default rules.
53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
import cv2
|
|
import numpy as np
|
|
|
|
from paperpod.vision.enhance import enhance_for_ocr, normalize_illumination
|
|
|
|
|
|
def _shaded_page() -> np.ndarray:
|
|
"""White page with a synthetic lighting gradient (bright left, dim right)."""
|
|
page = np.full((300, 400, 3), 255, dtype=np.uint8)
|
|
gradient = np.tile(np.linspace(1.0, 0.4, 400), (300, 1))
|
|
for c in range(3):
|
|
page[:, :, c] = (page[:, :, c] * gradient).astype(np.uint8)
|
|
return page
|
|
|
|
|
|
def test_normalize_illumination_flattens_gradient():
|
|
page = _shaded_page()
|
|
normalized = normalize_illumination(page)
|
|
# Brightness across the page should become far more uniform.
|
|
raw_gray = cv2.cvtColor(page, cv2.COLOR_BGR2GRAY)
|
|
assert normalized.std() < raw_gray.std()
|
|
|
|
|
|
def test_normalize_illumination_returns_grayscale_shape():
|
|
page = _shaded_page()
|
|
normalized = normalize_illumination(page)
|
|
assert normalized.ndim == 2
|
|
assert normalized.shape == page.shape[:2]
|
|
|
|
|
|
def test_enhance_for_ocr_returns_bgr():
|
|
page = _shaded_page()
|
|
# Page is narrower than the default upscale threshold; disable it here so
|
|
# this test only checks color/shape handling (upscaling is covered below).
|
|
enhanced = enhance_for_ocr(page, upscale_factor=1.0)
|
|
assert enhanced.shape == page.shape
|
|
# Grayscale-derived: all 3 channels should be identical.
|
|
assert np.array_equal(enhanced[:, :, 0], enhanced[:, :, 1])
|
|
assert np.array_equal(enhanced[:, :, 1], enhanced[:, :, 2])
|
|
|
|
|
|
def test_enhance_for_ocr_upscales_small_crops():
|
|
page = _shaded_page() # 400px wide, below the default 1800px threshold
|
|
enhanced = enhance_for_ocr(page, upscale_factor=1.5)
|
|
assert enhanced.shape[1] == round(page.shape[1] * 1.5)
|
|
assert enhanced.shape[0] == round(page.shape[0] * 1.5)
|
|
|
|
|
|
def test_enhance_for_ocr_skips_upscale_above_threshold():
|
|
page = _shaded_page()
|
|
enhanced = enhance_for_ocr(page, upscale_factor=1.5, upscale_below_width=100)
|
|
assert enhanced.shape == page.shape
|