How2Sign / how2sign-clips.py
tanthinhdt's picture
chore: add builder
5f1e8a2
raw
history blame
3.63 kB
# Copyright 2023 Thinh T. Duong
import os
import datasets
logger = datasets.logging.get_logger(__name__)
_CITATION = """
"""
_DESCRIPTION = """
This dataset contain short clips of Vietnamese speakers.
"""
_HOMEPAGE = "https://how2sign.github.io/index.html"
_REPO_URL = "https://huggingface.co/VieSignLang/how2sign-clips/resolve/main"
_URLS = {
"meta": f"{_REPO_URL}/metadata/" + "{split}.parquet",
"video": f"{_REPO_URL}/videos/" + "{type}/{split}.zip",
}
class How2SignClipsConfig(datasets.BuilderConfig):
"""Vietnamese Speaker Clip configuration."""
def __init__(self, name, **kwargs):
"""
:param name: Name of subset.
:param kwargs: Arguments.
"""
super(How2SignClipsConfig, self).__init__(
name=name,
version=datasets.Version("1.0.0"),
description=_DESCRIPTION,
**kwargs,
)
class How2SignClips(datasets.GeneratorBasedBuilder):
"""Vietnamese Speaker Clip dataset."""
BUILDER_CONFIGS = [
How2SignClipsConfig(name="rgb"),
How2SignClipsConfig(name="keypoints"),
]
DEFAULT_CONFIG_NAME = "rgb"
def _info(self) -> datasets.DatasetInfo:
features = datasets.Features({
"id": datasets.Value("string"),
"type": datasets.Value("string"),
"view": datasets.Value("string"),
"text": datasets.Value("string"),
"video": datasets.Value("large_binary"),
})
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
citation=_CITATION,
)
def _split_generators(
self, dl_manager: datasets.DownloadManager
) -> list[datasets.SplitGenerator]:
"""
Get splits.
:param dl_manager: Download manager.
:return: Splits.
"""
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_dir": dl_manager.download_and_extract(
_URLS["video"].format(
type=self.config.name,
split=split
)
),
},
)
for split, name in split_dict.items()
]
def _generate_examples(
self, metadata_paths: str,
video_dir: str,
) -> tuple[int, dict]:
"""
Generate examples from metadata.
:param metadata_path: Path to metadata.
:param visual_dir: Directory of visual data.
:yield: Example.
"""
dataset = datasets.load_dataset(
"parquet",
data_files=metadata_paths,
split="train",
)
for i, sample in enumerate(dataset):
video_path = os.path.join(video_dir, sample["id"] + ".mp4")
yield i, {
"id": sample["id"],
"type": sample["type"],
"view": sample["view"],
"text": sample["text"],
"video": self.__get_binary_data(video_path),
}
def __get_binary_data(self, path):
with open(path, "rb") as f:
return f.read()