| from datasets import ( |
| DatasetBuilder, |
| DownloadManager, |
| DatasetInfo, |
| Features, |
| Image, |
| ClassLabel, |
| Split, |
| SplitGenerator, |
| ) |
| import pandas as pd |
| import datasets |
|
|
| from pathlib import Path |
|
|
|
|
| _DESCRIPTION = """\ |
| This dataset includes spectrograms of ~950 afrobeats songs and 1k rock songs. |
| The spectrograms were generated with `librosa`. |
| """ |
|
|
| _NAMES = ["afrobeats", "rock"] |
|
|
| _URLS = { |
| "afrobeats": "https://huggingface.co/datasets/Kabilan108/spectrograms/resolve/main/data/afrobeats.zip", |
| "rock": "https://huggingface.co/datasets/Kabilan108/spectrograms/resolve/main/data/rock.zip", |
| } |
|
|
|
|
| |
| class Spectrograms(datasets.GeneratorBasedBuilder): |
| """Spectrograms Images Dataset""" |
|
|
| def _info(self): |
| return DatasetInfo( |
| description=_DESCRIPTION, |
| features=Features({"image": datasets.Value("string"), "label": ClassLabel(names=_NAMES)}), |
| supervised_keys=("image", "label"), |
| ) |
|
|
| def _split_generators(self, dl_manager: DownloadManager): |
| files = dl_manager.download_and_extract(_URLS) |
|
|
| return [ |
| SplitGenerator( |
| name=Split.TRAIN, |
| gen_kwargs={ |
| "afrobeats_dir": Path(files["afrobeats"]) / "afrobeats", |
| "rock_dir": Path(files["rock"]) / "rock", |
| }, |
| ) |
| ] |
|
|
| def _generate_examples(self, afrobeats_dir, rock_dir): |
| for genre_dir, label in [(afrobeats_dir, "afrobeats"), (rock_dir, "rock")]: |
| metadata = pd.read_csv(Path(genre_dir) / "metadata.tsv", sep="\t") |
|
|
| for _, row in metadata.iterrows(): |
| path = Path(genre_dir) / row["file_name"] |
|
|
| yield ( |
| f"{label}_{row['file_name'].replace('.png', '')}", |
| {"image": str(path), "label": label}, |
| ) |
|
|