Move the five top-level scripts into src/rowing_stats, one module per pipeline stage, behind a single 'rowing-stats' command with a subcommand each (crop, classify, ocr, claude, tune). Add type hints throughout, packaging metadata, pinned requirements files, and a test suite. Failures are now typed exceptions under RowingStatsError instead of prints, None returns and sys.exit: unreadable or missing images, photos with no detectable screen, a missing tesseract binary, tesseract failures, unusable Claude replies, and missing optional dependencies, model weights or API credentials. Each command reports a bad file and carries on, stops once on a setup problem, and returns a non-zero exit code when something failed. Crops, classifier labels and OCR votes are unchanged. Other changes: - crop.find_screen takes a CropParams, so the tuner searches the real detection code instead of a copy of it. - OCR validation scores only images that have a reference reading; images without one were counted as wrong. - 'classify train --data-dir train/' works with the repository layout (it looked for train/train), and the CNN weights path defaults to the committed screen_classifier_model.pth instead of a missing model.pth. - The CNN is loaded once per run rather than once per image. - ocr and claude can write their readings with --output. - crop no longer defaults to /mnt/user-data paths. - Pure vote-counting moved to parsing.py; the unused 'expected' argument that let a caller steer the vote is removed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
85 lines
3.2 KiB
Python
85 lines
3.2 KiB
Python
import cv2
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from rowing_stats import crop
|
|
from rowing_stats.crop import CropParams, crop_screen, find_screen, order_corners
|
|
from rowing_stats.errors import ImageLoadError, ScreenNotFoundError
|
|
|
|
|
|
def test_order_corners_returns_tl_tr_br_bl():
|
|
shuffled = np.array([[90, 80], [10, 15], [12, 85], [95, 12]], dtype="float32")
|
|
assert order_corners(shuffled).tolist() == [[10, 15], [95, 12], [90, 80], [12, 85]]
|
|
|
|
|
|
def test_find_screen_locates_the_display(photo):
|
|
screen = find_screen(photo)
|
|
assert screen is not None
|
|
# The fixture draws the display at (300, 200)-(500, 380)
|
|
assert abs(screen.x - 300) <= 5 and abs(screen.y - 200) <= 5
|
|
assert abs(screen.w - 200) <= 10 and abs(screen.h - 180) <= 10
|
|
|
|
|
|
def test_find_screen_blank_image_is_none():
|
|
assert find_screen(np.full((600, 800, 3), 40, dtype=np.uint8)) is None
|
|
|
|
|
|
def test_find_screen_ignores_a_bright_region_with_no_content():
|
|
# A window or wall: right size and shape, but no text edges inside it
|
|
image = np.full((600, 800, 3), 40, dtype=np.uint8)
|
|
cv2.rectangle(image, (300, 200), (500, 380), (220, 220, 220), thickness=-1)
|
|
assert find_screen(image) is None
|
|
|
|
|
|
def test_find_screen_respects_params(photo):
|
|
assert find_screen(photo, CropParams(area_ratio_min=0.1)) is None
|
|
assert find_screen(photo, CropParams(aspect_max=1.0)) is None
|
|
assert find_screen(photo, CropParams(edge_density_min=0.9)) is None
|
|
|
|
|
|
def test_crop_screen_writes_padded_crop(photo_path, tmp_path):
|
|
out = tmp_path / "crop.jpg"
|
|
w, h = crop_screen(photo_path, out, padding=15)
|
|
|
|
saved = cv2.imread(str(out))
|
|
assert saved.shape[:2] == (h + 30, w + 30)
|
|
assert saved.mean() > 120 # the light display, not the dark wall
|
|
|
|
|
|
def test_crop_screen_unreadable_image(corrupt_path, tmp_path):
|
|
with pytest.raises(ImageLoadError, match="not a readable image"):
|
|
crop_screen(corrupt_path, tmp_path / "out.jpg")
|
|
with pytest.raises(ImageLoadError, match="file not found"):
|
|
crop_screen(tmp_path / "missing.jpg", tmp_path / "out.jpg")
|
|
assert not (tmp_path / "out.jpg").exists()
|
|
|
|
|
|
def test_crop_screen_no_screen(tmp_path):
|
|
blank = tmp_path / "blank.jpg"
|
|
cv2.imwrite(str(blank), np.full((600, 800, 3), 40, dtype=np.uint8))
|
|
with pytest.raises(ScreenNotFoundError):
|
|
crop_screen(blank, tmp_path / "out.jpg")
|
|
|
|
|
|
def test_cli_reports_each_file_and_keeps_going(photo_path, corrupt_path, tmp_path, capsys):
|
|
blank = tmp_path / "blank.jpg"
|
|
cv2.imwrite(str(blank), np.full((600, 800, 3), 40, dtype=np.uint8))
|
|
out_dir = tmp_path / "out"
|
|
|
|
code = crop.main([str(tmp_path), str(out_dir)])
|
|
|
|
out = capsys.readouterr().out
|
|
assert code == 1 # one file was unreadable
|
|
assert "OK: IMG_0001.jpg -> IMG_0001_screen.jpg" in out
|
|
assert "SKIP: No screen detected in blank.jpg" in out
|
|
assert "ERROR: corrupt.jpg: not a readable image" in out
|
|
assert "Done: 1/3 screens cropped" in out
|
|
assert [p.name for p in out_dir.iterdir()] == ["IMG_0001_screen.jpg"]
|
|
|
|
|
|
def test_cli_no_images(tmp_path, capsys):
|
|
assert crop.main([str(tmp_path)]) == 1
|
|
assert crop.main([str(tmp_path / "missing")]) == 1
|
|
err = capsys.readouterr().err
|
|
assert "No images found" in err and "is not a directory" in err
|