sprite-dx-data / tests /test_release.py
Candle
Add playable animation previews to dataset viewer
d2b06b9
Raw History Blame Contribute Delete
4.53 kB
"""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()