"""Wallpaper up to the real outline of objects in front of a wall (no models needed). python -m unittest test_wall_decor_matte """ import unittest import numpy as np from services.wall.components import WallPlane from services.wall.edges import matte_wall_decor from state import class_ids WALL_Z = 2.0 def scene(): """A grey wall 2 m away. A bouquet blob (labelled anything) with bare wall between its stems, a glass vase and a chair the wall's colour standing in front of it.""" img = np.full((200, 300, 3), 110, np.uint8) seg = np.full((200, 300), class_ids({"wall"})[0], np.int32) points = np.zeros((200, 300, 3), np.float32) ys, xs = np.mgrid[0:200, 0:300] points[..., 0] = (xs - 150) / 100.0 points[..., 1] = (ys - 100) / 100.0 points[..., 2] = WALL_Z seg[60:140, 100:180] = class_ids({"bulletin board"})[0] # a bouquet, misclassified for x in range(105, 180, 12): # its stems, 30 cm in front img[60:140, x:x + 3] = 235 points[60:140, x:x + 3, 2] = WALL_Z - 0.3 seg[60:140, 200:240] = class_ids({"vase"})[0] img[60:140, 200:240] = 112 # glass, the wall's colour, 20 cm in front points[60:140, 200:240, 2] = WALL_Z - 0.2 seg[170:200, 0:60] = class_ids({"chair"})[0] img[170:200, 0:60] = 112 points[170:200, 0:60, 2] = WALL_Z - 0.5 wall = (seg == class_ids({"wall"})[0]).astype(np.uint8) plane = WallPlane(origin=np.array([0.0, 0.0, WALL_Z]), normal=np.array([0.0, 0.0, 1.0])) return img, seg, wall, points, plane def coverage_of(mask, alpha): coverage = np.zeros(mask.shape, np.float32) coverage[mask > 0] = alpha[mask > 0] / 255.0 return coverage class WallDecorMatteTest(unittest.TestCase): def test_wall_shows_around_the_stems_and_solid_things_stay(self): img, seg, wall, points, plane = scene() mask, alpha = matte_wall_decor(img, seg, wall, points, plane) self.assertIsNotNone(alpha) coverage = coverage_of(mask, alpha) self.assertGreater(coverage[100, 111], 0.9) # bare wall between two stems: papered self.assertLess(coverage[100, 106], 0.1) # a stem: the photo self.assertEqual(coverage[100, 220], 0.0) # the glass vase, off the wall: untouched self.assertEqual(coverage[185, 30], 0.0) # the chair: untouched self.assertEqual(coverage[30, 30], 1.0) # open wall def test_without_depth_nothing_changes(self): img, seg, wall, _points, plane = scene() mask, alpha = matte_wall_decor(img, seg, wall, None, plane) self.assertIsNone(alpha) self.assertTrue(np.array_equal(mask, wall)) if __name__ == "__main__": unittest.main()