File size: 3,158 Bytes
db613b6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | """Shared pytest fixtures: builds a minimal fake KITTI raw date/drive tree."""
from __future__ import annotations
from pathlib import Path
import numpy as np
import pytest
from PIL import Image
IMAGE_SIZE = (32, 32) # (width, height)
def make_fake_kitti_date(
date_dir: Path,
drive: str = "0001",
num_frames: int = 4,
baseline_shift_units: float = -40.0,
focal_length: float = 700.0,
) -> Path:
"""Builds a minimal fake KITTI raw <date>/ directory: per-date calib files + one drive.
`baseline_shift_units` is P_rect_02[0, 3] in KITTI's raw projection-matrix
units (not meters) -- nonzero by default so tests can catch a dropped
stereo-baseline correction (see kitti_calib.py's docstring). Passing 0.0
reproduces the (buggy, cam0-only-correct) behavior for comparison tests.
"""
date_dir.mkdir(parents=True, exist_ok=True)
drive_name = f"{date_dir.name}_drive_{drive}_sync"
drive_dir = date_dir / drive_name
image_dir = drive_dir / "image_02" / "data"
lidar_dir = drive_dir / "velodyne_points" / "data"
image_dir.mkdir(parents=True, exist_ok=True)
lidar_dir.mkdir(parents=True, exist_ok=True)
rng = np.random.default_rng(0)
width, height = IMAGE_SIZE
for frame in range(num_frames):
image = rng.integers(0, 255, size=(height, width, 3), dtype=np.uint8)
Image.fromarray(image).save(image_dir / f"{frame:010d}.png")
num_points = 32
points = rng.uniform(-5.0, 5.0, size=(num_points, 3)).astype(np.float32)
points[:, 2] = rng.uniform(2.0, 20.0, size=num_points).astype(np.float32) # plausible forward depth
reflectance = rng.uniform(0.0, 1.0, size=(num_points, 1)).astype(np.float32)
scan = np.concatenate([points, reflectance], axis=1)
scan.tofile(lidar_dir / f"{frame:010d}.bin")
(date_dir / "calib_imu_to_velo.txt").write_text(
"calib_time: 15-Mar-2012 11:37:16\n"
"R: 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0\n"
"T: 0.8 0.0 0.3\n"
)
(date_dir / "calib_velo_to_cam.txt").write_text(
"calib_time: 15-Mar-2012 11:37:16\n"
"R: 7.533745e-03 -9.999714e-01 -6.166020e-04 "
"1.480249e-02 7.280733e-04 -9.998902e-01 "
"9.998621e-01 7.523790e-03 1.480755e-02\n"
"T: -4.069766e-03 -7.631618e-02 -2.717806e-01\n"
)
p_rect_00 = f"{focal_length} 0.0 16.0 0.0 0.0 {focal_length} 16.0 0.0 0.0 0.0 1.0 0.0"
p_rect_02 = f"{focal_length} 0.0 16.0 {baseline_shift_units} 0.0 {focal_length} 16.0 0.0 0.0 0.0 1.0 0.0"
(date_dir / "calib_cam_to_cam.txt").write_text(
"calib_time: 15-Mar-2012 11:37:16\n"
"R_rect_00: 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0\n"
f"S_rect_00: {float(width)} {float(height)}\n"
f"P_rect_00: {p_rect_00}\n"
f"S_rect_02: {float(width)} {float(height)}\n"
f"P_rect_02: {p_rect_02}\n"
)
return date_dir
@pytest.fixture
def fake_kitti_root(tmp_path: Path) -> Path:
"""A fake KITTI raw root with one date (`2011_09_30`) and one drive (`0016`), 4 frames."""
make_fake_kitti_date(tmp_path / "2011_09_30", drive="0016", num_frames=4)
return tmp_path
|