File size: 4,528 Bytes
7677a5d
 
d2b06b9
 
 
7677a5d
 
 
d2b06b9
 
7677a5d
 
 
 
 
 
d2b06b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7677a5d
 
 
 
 
 
 
 
 
 
 
 
 
d2b06b9
7677a5d
d2b06b9
 
 
7677a5d
 
 
 
 
 
d2b06b9
 
7677a5d
 
 
 
 
 
 
 
 
d2b06b9
 
 
 
7677a5d
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Exercise the real HF loader and embedded media, without a Hub upload."""
import unittest
import json
import subprocess
import tempfile
from pathlib import Path

import yaml
from datasets import Image, Video, load_dataset
from PIL import Image as PILImage


ROOT = Path(__file__).resolve().parents[1]


class ReleaseTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.cache = tempfile.TemporaryDirectory(prefix="sprite-release-test-")
        cls.addClassCleanup(cls.cache.cleanup)

    def test_playable_video_preserves_selected_source_timing(self):
        for name in ("loops", "scene_boundaries"):
            with self.subTest(config=name):
                ds = load_dataset(str(ROOT), name, split="train", cache_dir=self.cache.name).cast_column("video", Video(decode=False))
                row = ds[0]
                folder = "shots" if name == "loops" else "animations"
                with PILImage.open(ROOT / "data" / folder / (row["id"] + ".webp")) as image:
                    start = row.get("start_frame", 0)
                    end = row.get("end_frame_exclusive", image.n_frames)
                    milliseconds = 0
                    for index in range(start, end):
                        image.seek(index)
                        image.load()
                        milliseconds += image.info["duration"]
                video = row["video"]["bytes"]
                probe = subprocess.run(
                    ["ffprobe", "-v", "error", "-show_entries", "stream=codec_name,width,height,duration",
                     "-of", "json", "-i", "pipe:0"], input=video, capture_output=True, check=True,
                )
                stream = json.loads(probe.stdout)["streams"][0]
                self.assertEqual(stream["codec_name"], "h264")
                self.assertLessEqual(max(stream["width"], stream["height"]), 320)
                self.assertAlmostEqual(float(stream["duration"]), milliseconds / 1000, places=3)
                subprocess.run(
                    ["ffmpeg", "-v", "error", "-xerror", "-i", "pipe:0", "-f", "null", "-"],
                    input=video, capture_output=True, check=True,
                )

    def test_viewer_configs_load_with_images(self):
        metadata = yaml.safe_load((ROOT / "README.md").read_text().split("---")[1])
        configs = metadata.get("configs", [])
        self.assertEqual(
            {c["config_name"] for c in configs},
            {"matting", "loops", "scene_boundaries"},
        )
        expected = {"matting": 1080, "loops": 248, "scene_boundaries": 257}
        for config in configs:
            name = config["config_name"]
            with self.subTest(config=name):
                # Resolves README configs just as consumers do; do not bypass them
                # by loading an individual Parquet file.
                ds = load_dataset(str(ROOT), name, split="train", cache_dir=self.cache.name)
                self.assertEqual(len(ds), expected[name])
                if name != "matting":
                    self.assertIsInstance(ds.features.get("video"), Video)
                    ds = ds.cast_column("video", Video(decode=False))
                image_columns = [k for k, v in ds.features.items() if isinstance(v, Image)]
                self.assertTrue(image_columns)
                ids = set()
                for row in ds:
                    self.assertNotIn(row["id"], ids)
                    ids.add(row["id"])
                    if name != "matting":
                        self.assertEqual(row["video"]["bytes"][4:8], b"ftyp")
                    for key in image_columns:
                        row[key].load()
                        self.assertGreater(row[key].width, 0)
                    if name == "matting":
                        self.assertEqual(row["cutout"].mode, "RGBA")
                        self.assertEqual(row["image"].size, row["matte"].size)
                    elif name == "loops":
                        self.assertLess(row["start_frame"], row["end_frame_exclusive"])
                        self.assertLessEqual(row["end_frame_exclusive"], row["num_frames"])
                stream = load_dataset(str(ROOT), name, split="train", streaming=True, cache_dir=self.cache.name)
                if name != "matting":
                    stream = stream.cast_column("video", Video(decode=False))
                streamed = next(iter(stream))
                self.assertEqual(streamed["id"], ds[0]["id"])


if __name__ == "__main__":
    unittest.main()