Vocalset-Breath / loader.py
Ewakaa's picture
Upload folder using huggingface_hub
1d1568c verified
Raw
History Blame Contribute Delete
2.28 kB
#!/usr/bin/env python3
"""Load VocalSet-Breath: pair the breath labels in this repo with your local
VocalSet audio.
VocalSet audio is not redistributed here. Download it from Zenodo
(DOI 10.5281/zenodo.1193957) and pass its path:
python loader.py --vocalset /path/to/VocalSet
Or use it as a library:
from loader import load_records
records = load_records("labels", vocalset_dir="/path/to/VocalSet")
"""
from __future__ import annotations
import argparse
import json
from pathlib import Path
def load_records(labels_dir, vocalset_dir=None):
"""Return one record per labeled clip.
Each record: clip_id, audio_path (if vocalset_dir given), duration_sec,
breath_events as [(start_sec, end_sec), ...], and the full raw label dict.
"""
labels_dir = Path(labels_dir)
audio_index = _index_audio(Path(vocalset_dir)) if vocalset_dir else {}
records = []
for path in sorted(labels_dir.rglob("*.breath.json")):
label = json.loads(path.read_text())
clip_id = path.name.removesuffix(".breath.json")
records.append({
"clip_id": clip_id,
"audio_path": audio_index.get(label.get("audio_file", clip_id + ".wav")),
"duration_sec": label.get("duration_sec"),
"breath_events": [(e["start_sec"], e["end_sec"])
for e in label.get("breath_events", [])],
"label": label,
})
return records
def _index_audio(vocalset_dir):
"""Map filename -> absolute path for every .wav under a VocalSet tree."""
return {p.name: str(p) for p in vocalset_dir.rglob("*.wav")}
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--labels", default="labels")
ap.add_argument("--vocalset", default=None,
help="path to your local VocalSet download (audio is not shipped)")
args = ap.parse_args()
records = load_records(args.labels, args.vocalset)
n_breaths = sum(len(r["breath_events"]) for r in records)
print(f"{len(records)} clips · {n_breaths} breath events")
if args.vocalset:
missing = sum(1 for r in records if r["audio_path"] is None)
print(f"{len(records) - missing} clips matched to audio, {missing} unmatched")
if __name__ == "__main__":
main()