"""Add mic-array DOA to every non-speech sound event of every protocol clip (needs official VRS).""" import json, os, sys, numpy as np sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from doa import ArrayDOA H = os.path.dirname(os.path.abspath(__file__)) B = '/mnt/work_new/weihanxu/aea_filtered_1105' sc = json.load(open(f'{H}/scores.json')) byrec = {} for c in sc: byrec.setdefault(c['recording'], []).append(c) for rec, clips in byrec.items(): vrs = f'/mnt/data_new/AEA/{rec}/recording.vrs' if not os.path.exists(vrs): print('NO VRS', rec); continue tg = json.load(open(f'{B}/annotations/legacy/traj_gaze/{rec}.json')) t0 = float(tg[0]['tracking_timestamp']) - tg[0]['frame_index'] / 20.0 D = ArrayDOA(vrs) for c in clips: for e in c['sound_events']: s, t = e['start'] + t0, e['end'] + t0 if t - s < 0.4: s, t = s - 0.2, s + 0.4 # short impulsive sounds: onset window t = min(t, s + 3.0) r = D.estimate(s, t) e['doa'] = r and {k: round(v, 2) for k, v in r.items()} ok = [e for e in c['sound_events'] if e.get('doa') and e['doa']['peak_ratio'] >= 1.5] off = [e for e in ok if abs(e['doa']['az_deg']) > 45] c['n_doa_events'] = len(ok); c['n_offaxis_doa'] = len(off) c['frac_offaxis_doa'] = round(len(off) / len(ok), 2) if ok else 0.0 c['offaxis_labels'] = sorted({e['label'] for e in off}) print(f"{c['seg_id']:34s} doa={len(ok):3d} off={len(off):3d} frac={c['frac_offaxis_doa']:.2f} {c['offaxis_labels'][:5]}", flush=True) json.dump(sc, open(f'{H}/scores_doa.json', 'w'), indent=1, ensure_ascii=False) print('SCORE_DOA_DONE')