room-visualizer / test_lighting_transfer.py
github-actions[bot]
Automated Deploy: 481de343009e6b4c65440fc24152da9baba359bc
dfec6e1
Raw History Blame Contribute Delete
2.77 kB
import unittest
import numpy as np
from config.lighting import SHADE_MAP_MAX, SHADE_MAP_MIN
from services.lighting.luminance import decode_relative_map
from services.lighting.shading import build_luminance_lighting_maps
class LuminanceLightingTransferTests(unittest.TestCase):
def test_preserves_cast_shadow_and_broad_room_light(self):
height, width = 180, 240
image = np.full((height, width, 3), 150, dtype=np.uint8)
image[:, :80, :] = 60
image[:, 170:, :] = 215
surface_mask = np.ones((height, width), dtype=np.uint8)
maps = build_luminance_lighting_maps(image, surface_mask)
self.assertIsNotNone(maps)
shade = decode_relative_map(maps["shadeMap"], SHADE_MAP_MIN, SHADE_MAP_MAX)
dark = float(np.median(shade[:, 20:60]))
neutral = float(np.median(shade[:, 105:135]))
bright = float(np.median(shade[:, 190:225]))
self.assertLess(dark, neutral * 0.60)
self.assertGreater(bright, neutral * 1.03)
def test_rejects_high_frequency_source_floor_pattern(self):
height, width = 180, 240
image = np.full((height, width, 3), 165, dtype=np.uint8)
image[:, :90, :] = 78
for x in range(0, width, 8):
image[:, x:x + 2, :] = np.clip(
image[:, x:x + 2, :].astype(np.int16) - 35,
0,
255,
).astype(np.uint8)
surface_mask = np.ones((height, width), dtype=np.uint8)
maps = build_luminance_lighting_maps(image, surface_mask)
self.assertIsNotNone(maps)
shade = decode_relative_map(maps["shadeMap"], SHADE_MAP_MIN, SHADE_MAP_MAX)
broad_shadow_contrast = float(
np.median(shade[:, 120:180]) - np.median(shade[:, 20:70])
)
pattern_contrast = float(np.mean(np.abs(np.diff(shade[:, 120:180], axis=1))))
self.assertGreater(broad_shadow_contrast, 0.20)
self.assertLess(pattern_contrast, 0.04)
def test_replaced_rug_appearance_is_inpainted(self):
height, width = 180, 240
image = np.full((height, width, 3), 160, dtype=np.uint8)
image[65:145, 75:175, :] = 45
surface_mask = np.ones((height, width), dtype=np.uint8)
rug_mask = np.zeros((height, width), dtype=np.uint8)
rug_mask[65:145, 75:175] = 1
maps = build_luminance_lighting_maps(image, surface_mask, rug_mask)
self.assertIsNotNone(maps)
shade = decode_relative_map(maps["shadeMap"], SHADE_MAP_MIN, SHADE_MAP_MAX)
rug_value = float(np.median(shade[rug_mask > 0]))
surrounding_value = float(np.median(shade[rug_mask == 0]))
self.assertAlmostEqual(rug_value, surrounding_value, delta=0.12)
if __name__ == "__main__":
unittest.main()