import numpy as np from utils.curve import compute_letterbox, sample_lane_at_ys from utils.mask_to_lanes import mask_to_lanes def test_letterbox_roundtrip(): lb = compute_letterbox(src_w=1280, src_h=720, out_w=640, out_h=360) pts = np.array([[100.0, 200.0], [1000.0, 500.0]], dtype=np.float32) warped = lb.apply_points(pts) restored = lb.invert_points(warped) assert np.allclose(pts, restored, atol=1e-3) def test_letterbox_stays_in_bounds(): lb = compute_letterbox(src_w=1280, src_h=720, out_w=640, out_h=360) corners = np.array([[0, 0], [1279, 719]], dtype=np.float32) warped = lb.apply_points(corners) assert (warped[:, 0] >= -1).all() and (warped[:, 0] <= 641).all() assert (warped[:, 1] >= -1).all() and (warped[:, 1] <= 361).all() def test_sample_lane_at_ys_interpolates_and_masks(): lane = [(0.0, 0.0), (10.0, 1.0)] # x = 10*y sample_ys = np.array([0.0, 0.5, 1.0, 1.5], dtype=np.float32) xs, valid = sample_lane_at_ys(lane, sample_ys) assert valid.tolist() == [True, True, True, False] assert np.allclose(xs[:3], [0.0, 5.0, 10.0], atol=1e-4) def test_mask_to_lanes_separates_instances(): mask = np.zeros((100, 200), dtype=np.uint8) mask[:, 20:24] = 255 # vertical strip -> one lane instance mask[:, 150:154] = 255 # another, far apart -> separate instance lanes = mask_to_lanes(mask) assert len(lanes) == 2 for lane in lanes: assert len(lane) >= 5 def test_mask_to_lanes_drops_small_noise_blobs(): mask = np.zeros((100, 200), dtype=np.uint8) mask[10:12, 10:12] = 255 # tiny 2x2 JPEG-noise-like blob, below MIN_BLOB_AREA lanes = mask_to_lanes(mask) assert lanes == []