import json
|
import subprocess
|
import sys
|
import tempfile
|
import unittest
|
from pathlib import Path
|
|
from PIL import Image
|
|
|
ROOT = Path(__file__).resolve().parents[3]
|
SCRIPT = ROOT / "capabilities" / "02-semantic-mapping" / "run_semantic_segmentation.py"
|
|
|
class SemanticSegmentationTests(unittest.TestCase):
|
def test_small_rgb_input_produces_raster_vector_and_metadata(self):
|
with tempfile.TemporaryDirectory() as temp:
|
work = Path(temp)
|
image = Image.new("RGB", (96, 64), (80, 80, 80))
|
pixels = image.load()
|
for y in range(20):
|
for x in range(32):
|
pixels[x, y] = (35, 180, 55)
|
for y in range(20, 44):
|
for x in range(32, 72):
|
pixels[x, y] = (30, 100, 210)
|
source = work / "sample.png"
|
image.save(source)
|
output = work / "out"
|
result = subprocess.run([sys.executable, str(SCRIPT), "--input", str(source), "--output", str(output)], capture_output=True, text=True)
|
self.assertEqual(result.returncode, 0, result.stderr)
|
metadata = json.loads((output / "run_metadata.json").read_text(encoding="utf-8"))
|
self.assertEqual(metadata["input_count"], 1)
|
record = metadata["images"][0]
|
for key in ("mask_file", "overlay_file", "raster_file", "vector_file"):
|
self.assertTrue((output / record[key]).is_file(), key)
|
self.assertGreater(record["class_pixel_counts"]["vegetation"], 0)
|
self.assertGreater(record["class_pixel_counts"]["water"], 0)
|
|
|
if __name__ == "__main__":
|
unittest.main()
|