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