Spaces:
Running on Zero
Running on Zero
Download tests/test_alpha_preview.py from broyang/3dai: direct link, hf CLI and curl.
- Browser
- Download file 5.24 kB
-
https://huggingface.co/spaces/broyang/3dai/resolve/main/tests/test_alpha_preview.py
- Command line
-
hf download hf://spaces/broyang/3dai/tests/test_alpha_preview.py
-
curl -L -o test_alpha_preview.py https://huggingface.co/spaces/broyang/3dai/resolve/main/tests/test_alpha_preview.py
5.24 kB
| import os | |
| import shutil | |
| import subprocess | |
| import sys | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| from unittest import mock | |
| ROOT = Path(__file__).resolve().parent.parent | |
| sys.path.insert(0, str(ROOT)) | |
| sys.path.insert(0, str(ROOT / "tests")) | |
| os.environ["LABS_FALLBACK_SIGNING_KEY"] = "unit-signing-key" | |
| os.environ.pop("SPACE_ID", None) | |
| os.environ.setdefault("GRADIO_ANALYTICS_ENABLED", "False") | |
| import imageio_ffmpeg # noqa: E402 | |
| import numpy as np # noqa: E402 | |
| import fallback_stubs # noqa: E402 | |
| fallback_stubs.install() | |
| import app # noqa: E402 | |
| SIZE = 96 | |
| COLOUR = (40, 160, 230) | |
| def matte_passes(size=SIZE): | |
| black = np.zeros((size, size, 3), np.uint8) | |
| white = np.full((size, size, 3), 255, np.uint8) | |
| yy, xx = np.mgrid[0:size, 0:size] | |
| disc = (xx - size / 2) ** 2 + (yy - size / 2) ** 2 < (size / 4) ** 2 | |
| black[disc] = COLOUR | |
| white[disc] = COLOUR | |
| return [black], [white] | |
| def decode_rgba(path, size=SIZE, decoder="libvpx"): | |
| ffmpeg = shutil.which("ffmpeg") or imageio_ffmpeg.get_ffmpeg_exe() | |
| raw = subprocess.run( | |
| [ffmpeg, "-v", "error", "-c:v", decoder, "-i", path, "-frames:v", "1", | |
| "-f", "rawvideo", "-pix_fmt", "rgba", "-"], | |
| capture_output=True, check=True, | |
| ).stdout | |
| return np.frombuffer(raw[: size * size * 4], dtype=np.uint8).reshape(size, size, 4) | |
| class AlphaMatte(unittest.TestCase): | |
| def test_two_pass_frames_recover_alpha_and_colour(self): | |
| frames = app.compose_alpha_frames(*matte_passes()) | |
| self.assertEqual(len(frames), 1) | |
| frame = frames[0] | |
| self.assertEqual(frame.shape, (SIZE, SIZE, 4)) | |
| self.assertEqual(tuple(frame[SIZE // 2, SIZE // 2]), (*COLOUR, 255)) | |
| self.assertEqual(tuple(frame[1, 1]), (0, 0, 0, 0)) | |
| def test_short_matte_pass_keeps_the_premultiplied_frames(self): | |
| black, white = matte_passes() | |
| frames = app.compose_alpha_frames(black + black, white) | |
| self.assertEqual(len(frames), 2) | |
| self.assertEqual(frames[0].shape[-1], 3) | |
| class PreviewSuffix(unittest.TestCase): | |
| def test_suffix_needs_the_gate_and_a_fourth_channel(self): | |
| rgb = np.zeros((4, 4, 3), np.uint8) | |
| rgba = np.zeros((4, 4, 4), np.uint8) | |
| with mock.patch.object(app, "TRANSPARENT_PREVIEW", True): | |
| self.assertEqual(app.preview_video_suffix([rgba]), "webm") | |
| self.assertEqual(app.preview_video_suffix([rgb]), "mp4") | |
| self.assertEqual(app.preview_video_suffix([]), "mp4") | |
| with mock.patch.object(app, "TRANSPARENT_PREVIEW", False): | |
| self.assertEqual(app.preview_video_suffix([rgba]), "mp4") | |
| class AlphaWebM(unittest.TestCase): | |
| def setUp(self): | |
| self.directory = tempfile.mkdtemp() | |
| self.addCleanup(shutil.rmtree, self.directory, True) | |
| def test_encoded_webm_keeps_a_transparent_and_an_opaque_pixel(self): | |
| path = os.path.join(self.directory, "alpha.webm") | |
| frames = app.compose_alpha_frames(*matte_passes()) | |
| self.assertTrue(app.write_alpha_webm(path, frames)) | |
| self.assertGreater(os.path.getsize(path), 0) | |
| decoded = decode_rgba(path) | |
| self.assertEqual(int(decoded[SIZE // 2, SIZE // 2][3]), 255) | |
| self.assertEqual(int(decoded[1, 1][3]), 0) | |
| centre = [int(value) for value in decoded[SIZE // 2, SIZE // 2][:3]] | |
| self.assertTrue( | |
| all(abs(actual - expected) <= 6 for actual, expected in zip(centre, COLOUR)), centre, | |
| ) | |
| def test_alpha_writer_refuses_empty_frames(self): | |
| self.assertFalse(app.write_alpha_webm(os.path.join(self.directory, "alpha.webm"), [])) | |
| class PreviewWriter(unittest.TestCase): | |
| def setUp(self): | |
| self.directory = tempfile.mkdtemp() | |
| self.addCleanup(shutil.rmtree, self.directory, True) | |
| def test_matte_frames_are_written_as_webm(self): | |
| frames = app.compose_alpha_frames(*matte_passes()) | |
| with mock.patch.object(app, "TRANSPARENT_PREVIEW", True), \ | |
| mock.patch.object(app, "TMP_DIR", self.directory): | |
| path = app.write_preview_video(frames) | |
| self.assertTrue(path.endswith(".webm"), path) | |
| self.assertGreater(os.path.getsize(path), 0) | |
| def test_alpha_failure_falls_back_to_the_opaque_mp4(self): | |
| frames = app.compose_alpha_frames(*matte_passes()) | |
| with mock.patch.object(app, "TRANSPARENT_PREVIEW", True), \ | |
| mock.patch.object(app, "TMP_DIR", self.directory), \ | |
| mock.patch.object(app, "write_alpha_webm", return_value=False), \ | |
| mock.patch.object(app, "write_mp4", return_value=True) as opaque: | |
| path = app.write_preview_video(frames) | |
| self.assertTrue(path.endswith(".mp4"), path) | |
| self.assertEqual(opaque.call_args.args[1][0].shape[-1], 3) | |
| def test_opaque_rendering_stays_mp4(self): | |
| frames = [np.zeros((32, 32, 3), np.uint8)] | |
| with mock.patch.object(app, "TRANSPARENT_PREVIEW", True), \ | |
| mock.patch.object(app, "TMP_DIR", self.directory), \ | |
| mock.patch.object(app, "write_mp4", return_value=True): | |
| path = app.write_preview_video(frames) | |
| self.assertTrue(path.endswith(".mp4"), path) | |
| if __name__ == "__main__": | |
| unittest.main() | |