48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
|
|
|
|
SCRIPT = Path(__file__).parents[2] / "scripts" / "build_grayscale_yolo_dataset.py"
|
|
|
|
|
|
def test_grayscale_builder_preserves_labels_and_split(tmp_path: Path) -> None:
|
|
source = tmp_path / "source"
|
|
(source / "images" / "train").mkdir(parents=True)
|
|
(source / "labels" / "train").mkdir(parents=True)
|
|
image = source / "images" / "train" / "tile.png"
|
|
label = source / "labels" / "train" / "tile.txt"
|
|
Image.new("RGB", (8, 8), (255, 0, 0)).save(image)
|
|
label.write_text("0 0.5 0.5 0.5 0.5\n", encoding="utf-8")
|
|
summary = source / "yolo_tile_dataset_summary.json"
|
|
summary.write_text(
|
|
json.dumps(
|
|
{
|
|
"tiles": [
|
|
{
|
|
"split": "train",
|
|
"image_path": str(image),
|
|
"label_path": str(label),
|
|
}
|
|
]
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
output = tmp_path / "gray"
|
|
subprocess.run(
|
|
[sys.executable, str(SCRIPT), "--summary", str(summary), "--output-dir", str(output)],
|
|
check=True,
|
|
)
|
|
converted = Image.open(output / "images" / "train" / "tile.png")
|
|
r, g, b = converted.getpixel((0, 0))
|
|
assert r == g == b
|
|
assert (output / "labels" / "train" / "tile.txt").read_text() == label.read_text()
|
|
evidence = json.loads((output / "grayscale-dataset-evidence.json").read_text())
|
|
assert evidence["converted_tile_count"] == 1
|