Add crop refinement, finger removal, and local LLM naming

- vision/refine.py: tighten crops to the paper band (removes mat
  margins and hands beside receipts) and inpaint border-connected
  skin regions so fingers disappear from output
- llm/vision.py: identify documents with a local Ollama vision model
  (qwen2.5vl); extracts vendor/date/total/form code and flags quality
  issues (fingers, blur, glare); falls back to Tesseract when down
- pipeline: drop blank pages, dedupe consecutive captures of the same
  document, record refine/LLM fields in report and export summary
This commit is contained in:
2026-07-08 18:23:50 -04:00
parent 2c28a623fb
commit a43ab20df6
22 changed files with 1497 additions and 111 deletions
+30
View File
@@ -47,3 +47,33 @@ def test_sharpness_prefers_sharp_frame():
sharp = synthetic_frame()
blurred = cv2.GaussianBlur(sharp, (31, 31), 0)
assert sharpness_score(sharp) > sharpness_score(blurred)
def test_detection_reports_source_and_confidence():
det = detect_document(synthetic_frame())
assert det.source in ("edges", "threshold")
assert 0.0 < det.confidence <= 1.0
def test_rejects_thin_sliver_candidate():
"""A narrow strip (e.g. a barcode edge) shouldn't be mistaken for a page."""
frame = np.full((720, 1280, 3), (60, 90, 120), dtype=np.uint8)
# 30x600 sliver: aspect ratio 20:1, well past max_aspect.
frame[60:660, 600:630] = 245
assert detect_document(frame, max_aspect=6.0) is None
def test_rejects_near_whole_frame_candidate():
"""A candidate covering almost the entire frame is probably background/lighting."""
frame = np.full((720, 1280, 3), 245, dtype=np.uint8)
frame[10:20, 10:20] = (60, 90, 120) # tiny dark speck so it's not perfectly blank
assert detect_document(frame, max_area_ratio=0.92) is None
def test_downscaled_detection_matches_full_resolution():
"""detect_width downscaling shouldn't change which document gets found."""
frame = synthetic_frame()
det_full = detect_document(frame, detect_width=10_000) # effectively no downscale
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