shuishen
3 hours ago 2ae460fc4a4c2419cf44329783d49a739e2a04ea
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
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()