Download process/map_shipsear.py from OpenTSLab/MachineHearing: direct link, hf CLI and curl.
- Browser
- Download file 2.09 kB
-
https://huggingface.co/datasets/OpenTSLab/MachineHearing/resolve/main/process/map_shipsear.py
- Command line
-
hf download hf://datasets/OpenTSLab/MachineHearing/process/map_shipsear.py
-
curl -L -o map_shipsear.py https://huggingface.co/datasets/OpenTSLab/MachineHearing/resolve/main/process/map_shipsear.py
2.09 kB
| import argparse | |
| import json | |
| from pathlib import Path | |
| import soundfile as sf | |
| MHB_ROOT = Path(__file__).resolve().parent.parent | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("test_dir", type=Path) | |
| args = parser.parse_args() | |
| test_dir = args.test_dir.expanduser().resolve() | |
| assert test_dir.is_dir(), f"Not found: {test_dir}" | |
| count = 0 | |
| with (MHB_ROOT / "meta_data.jsonl").open("r", encoding="utf-8") as f: | |
| for line in f: | |
| if not line.strip(): | |
| continue | |
| record = json.loads(line) | |
| if record["task_name"] != "ShipsEar": | |
| continue | |
| name = record["meta_data"]["ori_file_path"] | |
| # Trawler 使用划分脚本生成的独立测试片段 | |
| if name.startswith("28__"): | |
| name = Path(name).stem + "_test.wav" | |
| matches = list(test_dir.glob(f"*{name}")) | |
| assert len(matches) == 1, f"Expected one source for {name}, found {len(matches)}" | |
| length = record["input_ts"]["length"] | |
| sr = record["input_ts"]["fs"] | |
| index = record["meta_data"]["seg_index"] | |
| assert length == sr * 30 | |
| # 按 30 秒切分 | |
| with sf.SoundFile(matches[0]) as audio: | |
| assert audio.samplerate == sr and audio.channels == 1, f"{matches[0], audio.samplerate, audio.channels}" | |
| start = index * (sr * 15) | |
| assert start + length <= audio.frames, f"Short recording: {matches[0]}" | |
| audio.seek(start) | |
| segment = audio.read(length, dtype="float32") | |
| # 保存到 meta_data.jsonl 指定的路径 | |
| dst = MHB_ROOT / record["input_ts"]["path"] | |
| assert dst.name == f"{record['id']}.wav" | |
| dst.parent.mkdir(parents=True, exist_ok=True) | |
| sf.write(dst, segment, sr, subtype="FLOAT") | |
| count += 1 | |
| assert count == 84, f"Expected 84 segments, got {count}" | |
| print(f"ShipsEar: created {count} WAV files") | |
| if __name__ == "__main__": | |
| main() | |