ViewingSoundPAQ / scripts /data_preparation /datasets /keep_rows_with_audio.py
xiaohaoy's picture
Initial release
16231ab verified
Raw History Blame Contribute Delete
1.11 kB
#!/usr/bin/env python3
"""Keep rows of an image-screened candidate manifest whose audio_path exists (reuse -ImagesOnly screens)."""
import sys as _sys
from pathlib import Path as _Path
_SCRIPTS = next(_p for _p in _Path(__file__).resolve().parents if _p.name == "scripts")
_sys.path[:0] = [str(_SCRIPTS.parent), str(_SCRIPTS.parent / "models")] + [str(_d) for _d in sorted(_SCRIPTS.rglob("*")) if _d.is_dir() and _d.name != "__pycache__"]
import argparse, os
from pathlib import Path
import pandas as pd
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--input", type=Path, required=True)
p.add_argument("--output", type=Path, required=True)
a = p.parse_args()
f = pd.read_csv(a.input, low_memory=False)
roots = {os.path.dirname(x) for x in f.audio_path.astype(str)}
present = set()
for r in roots:
if os.path.isdir(r):
present |= {os.path.join(r, n) for n in os.listdir(r)}
ok = f.audio_path.astype(str).isin(present)
f[ok].to_csv(a.output, index=False)
print(f"{int(ok.sum())} of {len(f)} screened frames have their audio ({f[ok].recording_id.nunique()} videos) -> {a.output}")