"""Dense DOA scan per clip: 0.5 s windows, keep loud windows (>= clip 60th pct energy) with a clear peak; report the fraction coming from > 45 deg off the wearer's forward axis (sides/behind).""" import json, os, sys, numpy as np 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' sc = json.load(open(f'{H}/scores_doa.json')) byrec = {} for c in sc: byrec.setdefault(c['recording'], []).append(c) for rec, clips in byrec.items(): 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(f'/mnt/data_new/AEA/{rec}/recording.vrs') for c in clips: a, b = c['clip_start_sec'] + t0, c['clip_end_sec'] + t0 x = D.audio(a, b); W = 24000 en = np.array([np.mean((x[i:i + W] - x[i:i + W].mean(0)) ** 2) for i in range(0, len(x) - W, W)]) thr = np.percentile(en, 60) if len(en) else 0 res = [] for k, e in enumerate(en): if e < thr: continue r = D.estimate(a + k * 0.5, a + k * 0.5 + 0.5) if r and r['peak_ratio'] >= 1.5: res.append(r['az_deg']) az = np.abs(np.array(res)) c['dense_doa'] = {'n_windows': int(len(az)), 'frac_offaxis45': round(float(np.mean(az > 45)) if len(az) else 0.0, 3), 'frac_behind90': round(float(np.mean(az > 90)) if len(az) else 0.0, 3), 'az_hist_30deg': np.histogram(np.array(res), bins=np.arange(-180, 181, 30))[0].tolist() if res else []} print(f"{c['seg_id']:34s} win={len(az):4d} off45={c['dense_doa']['frac_offaxis45']:.2f} behind={c['dense_doa']['frac_behind90']:.2f}", flush=True) json.dump(sc, open(f'{H}/scores_dense.json', 'w'), indent=1, ensure_ascii=False) print('DENSE_DONE')