How2Sign / how2sign.py
tanthinhdt's picture
fix(builder): rectify generate_examples
d311e8d verified
# Copyright 2023 Thinh T. Duong
import os
import datasets
from glob import glob
logger = datasets.logging.get_logger(__name__)
_CITATION = """
"""
_DESCRIPTION = """
"""
_HOMEPAGE = "https://how2sign.github.io/index.html"
_REPO_URL = "https://huggingface.co/datasets/VieSignLang/how2sign-clips/resolve/main"
_URLS = {
"meta": os.path.join(_REPO_URL, "how2sign_realigned_{split}.csv"),
"videos": os.path.join(_REPO_URL, "{split}_{subset}/*.zip"),
}
class How2SignConfig(datasets.BuilderConfig):
"""How2Sign configuration."""
def __init__(self, name, **kwargs):
"""
Parameters
----------
name : str
Name of subset.
kwargs : dict
Keyword arguments.
"""
super(How2SignConfig, self).__init__(
name=name,
version=datasets.Version("1.0.0"),
description=_DESCRIPTION,
**kwargs,
)
class How2Sign(datasets.GeneratorBasedBuilder):
"""How2Sign dataset."""
BUILDER_CONFIGS = [
How2SignConfig(name="raw_videos"),
How2SignConfig(name="rgb_side_raw_videos"),
How2SignConfig(name="rgb_front_clips"),
How2SignConfig(name="rgb_side_clips"),
How2SignConfig(name="2D_keypoints"),
]
DEFAULT_CONFIG_NAME = "rgb_front_clips"
def _info(self) -> datasets.DatasetInfo:
features = datasets.Features({
"VIDEO_ID": datasets.Value("string"),
"VIDEO_NAME": datasets.Value("string"),
"SENTENCE_ID": datasets.Value("string"),
"SENTENCE_NAME": datasets.Value("string"),
"START_REALIGNED": datasets.Value("float64"),
"END_REALIGNED": datasets.Value("float64"),
"SENTENCE": datasets.Value("string"),
"VIDEO": datasets.Value("string"),
})
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
citation=_CITATION,
)
def _split_generators(
self, dl_manager: datasets.DownloadManager
) -> list[datasets.SplitGenerator]:
"""
Get splits.
Parameters
----------
dl_manager : datasets.DownloadManager
Download manager.
Returns
-------
list[datasets.SplitGenerator]
Split generators.
"""
split_dict = {
"train": datasets.Split.TRAIN,
"test": datasets.Split.TEST,
"val": datasets.Split.VALIDATION,
}
return [
datasets.SplitGenerator(
name=name,
gen_kwargs={
"metadata_path": dl_manager.download(
_URLS["meta"].format(split=split)
),
"video_dirs": dl_manager.download_and_extract(
glob(
_URLS["videos"].format(
split=split,
subset=self.config.name,
)
)
),
},
)
for split, name in split_dict.items()
]
def _generate_examples(
self, metadata_path: str,
video_dirs: list[str],
) -> tuple[int, dict]:
"""
Generate examples.
Parameters
----------
metadata_path : str
Path to metadata.
video_dirs : list[str]
List of video directories.
Returns
-------
tuple[int, dict]
Index and sample.
"""
split = datasets.load_dataset(
"csv",
data_files=metadata_path,
split="train",
delimiter="\t",
)
for i, sample in enumerate(split):
for video_dir in video_dirs:
if self.config.name in ["raw_videos", "rgb_side_raw_videos"]:
video_path = os.path.join(video_dir, sample["VIDEO_NAME"] + ".mp4")
else:
video_path = os.path.join(video_dir, sample["SENTENCE_NAME"] + ".mp4")
if os.path.exists(video_path):
yield i, {
"VIDEO_ID": sample["VIDEO_ID"],
"VIDEO_NAME": sample["VIDEO_NAME"],
"SENTENCE_ID": sample["SENTENCE_ID"],
"SENTENCE_NAME": sample["SENTENCE_NAME"],
"START_REALIGNED": sample["START_REALIGNED"],
"END_REALIGNED": sample["END_REALIGNED"],
"SENTENCE": sample["SENTENCE"],
"VIDEO": video_path,
}