"""Assemble subset_solo20/ (symlinks + extracted 7-ch wavs) inside the AEA-annotations staging dir.""" import json, os, shutil, sys, numpy as np from scipy.io import wavfile H = os.path.dirname(os.path.abspath(__file__)); sys.path.insert(0, H) from doa import ArrayDOA B = '/mnt/work_new/weihanxu/aea_filtered_1105'; AEA = '/mnt/data_new/AEA' OUT = '/mnt/work_new/weihanxu/hf_dataset_aea_annot_1107/subset_solo20' sel = json.load(open(f'{H}/selected20.json')) os.makedirs(OUT, exist_ok=True) def link(src, dst): os.makedirs(os.path.dirname(dst), exist_ok=True) if os.path.lexists(dst): os.remove(dst) os.symlink(src, dst) recs = sorted({c['recording'] for c in sel}) for r in recs: d = f'{OUT}/recordings/{r}' link(f'{AEA}/{r}/recording.vrs', f'{d}/recording.vrs') link(f'{B}/video/{r}.mp4', f'{d}/video_456px_20fps_stereo.mp4') for root, _, fs in os.walk(f'{AEA}/{r}'): for f in fs: if f == 'recording.vrs' or f.startswith('.') or f.endswith('.zip'): continue rel = os.path.relpath(os.path.join(root, f), f'{AEA}/{r}') link(os.path.join(root, f), f'{d}/official/{rel}') link(f'{B}/annotations/legacy/narration/{r}_narration.json', f'{d}/legacy/narration.json') link(f'{B}/annotations/legacy/sound/{r}_sound.json', f'{d}/legacy/sound.json') link(f'{B}/annotations/legacy/traj_gaze/{r}.json', f'{d}/legacy/traj_gaze_20fps.json') index = [] for c in sel: r = c['recording']; e = f'{OUT}/examples/{c["seg_id"]}' link(f'{B}/{c["clip"]}', f'{e}/clip.mp4') tg = json.load(open(f'{B}/annotations/legacy/traj_gaze/{r}.json')) t0 = float(tg[0]['tracking_timestamp']) - tg[0]['frame_index'] / 20.0 wav = f'{e}/mic_array_7ch_48k.wav' if not os.path.exists(wav): x = ArrayDOA(f'{AEA}/{r}/recording.vrs').audio(c['clip_start_sec'] + t0, c['clip_end_sec'] + t0) x = x / (np.abs(x).max() + 1e-9) * 0.9 wavfile.write(wav, 48000, x.astype(np.float32)) item = {k: c[k] for k in ('seg_id', 'recording', 'goal', 'steps', 'clip_start_sec', 'clip_end_sec', 'duration_sec', 'protocol_score')} item.update({'clip_start_device_sec': round(c['clip_start_sec'] + t0, 3), 'clip_end_device_sec': round(c['clip_end_sec'] + t0, 3), 'spatial_audio_score': c['spatial_audio_score'], 'dense_doa': c['dense_doa'], 'n_annotated_sound_events': c['n_doa_events'], 'n_annotated_offaxis_events': c['n_offaxis_doa'], 'head_yaw_range_deg': c['head_yaw_range_deg'], 'sound_events': c['sound_events'], 'files': {'clip': f'examples/{c["seg_id"]}/clip.mp4', 'mic_array_wav': f'examples/{c["seg_id"]}/mic_array_7ch_48k.wav', 'recording_dir': f'recordings/{r}/'}}) json.dump(item, open(f'{e}/meta.json', 'w'), indent=1, ensure_ascii=False) index.append(item); print('ok', c['seg_id'], flush=True) json.dump(index, open(f'{OUT}/index.json', 'w'), indent=1, ensure_ascii=False) print('BUILD_DONE', len(index), len(recs))