Fix clipped receipt margins, add time-of-day naming, speed up rotation detection

- vision/document.py: pad_quad() expands the detected quad outward
  before warping so a straight-line contour fit doesn't clip the first
  character of every line on a slightly curled/wrinkled receipt
- ocr/extract.py + llm/vision.py: extract transaction time (HH:MM) so
  same-day repeat visits to a vendor get distinct filenames
  (2023-03-20_1525_walmart.pdf vs. a later same-day trip)
- vision/orient.py: downscale before Tesseract OSD/confidence-sweep
  rotation detection — this was the dominant cost in the detection
  phase (7x speedup: 472s -> 67s on a 36s test video)
This commit is contained in:
2026-07-08 18:55:37 -04:00
parent a43ab20df6
commit 5be9d5935d
11 changed files with 160 additions and 12 deletions
+18 -1
View File
@@ -1,7 +1,7 @@
import cv2
import numpy as np
from paperpod.vision.document import detect_document, warp_document
from paperpod.vision.document import detect_document, pad_quad, warp_document
from paperpod.vision.sharpness import sharpness_score
@@ -77,3 +77,20 @@ def test_downscaled_detection_matches_full_resolution():
det_small = detect_document(frame, detect_width=640)
assert det_full is not None and det_small is not None
assert abs(det_full.area_ratio - det_small.area_ratio) < 0.05
def test_pad_quad_expands_outward():
quad = np.array([[100, 100], [300, 100], [300, 300], [100, 300]], dtype=np.float32)
padded = pad_quad(quad, image_shape=(720, 1280), margin_fraction=0.1)
centroid = quad.mean(axis=0)
# Every corner should move further from the centroid, not closer.
orig_dist = np.linalg.norm(quad - centroid, axis=1)
new_dist = np.linalg.norm(padded - centroid, axis=1)
assert np.all(new_dist > orig_dist)
def test_pad_quad_clamps_to_frame_bounds():
quad = np.array([[5, 5], [1275, 5], [1275, 715], [5, 715]], dtype=np.float32)
padded = pad_quad(quad, image_shape=(720, 1280), margin_fraction=0.2)
assert padded[:, 0].min() >= 0 and padded[:, 0].max() <= 1279
assert padded[:, 1].min() >= 0 and padded[:, 1].max() <= 719