Datasets:
Download code/finalize_offsets.py from teawhite/EYBX-processed: direct link, hf CLI and curl.
- Browser
- Download file 7.43 kB
-
https://huggingface.co/datasets/teawhite/EYBX-processed/resolve/main/code/finalize_offsets.py
- Command line
-
hf download hf://datasets/teawhite/EYBX-processed/code/finalize_offsets.py
-
curl -L -o finalize_offsets.py https://huggingface.co/datasets/teawhite/EYBX-processed/resolve/main/code/finalize_offsets.py
7.43 kB
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| finalize_offsets.py —— 合并两种测法,产出唯一权威的时间轴映射 | |
| 两种测法各有所长,谁也不能单独用: | |
| align.py(传送窗口互相关) | |
| 台阶【位置】测得准 —— 传送窗口在整条时间轴上密集分布,掉帧造成的 | |
| 永久错位一眼可见。但绝对值里混进了「leg_end 事件 → 引擎真正切黑」的 | |
| 固定延迟,不是纯时间轴偏移。 | |
| refine_offset.py(固定 M 的矢量光流相关) | |
| 绝对值【无偏】—— 同一时刻的两个观测,没有可假设的常数掺进来。 | |
| M 固定成环境钉死那轮解出的相机投影,只剩偏移一个自由度;再用矢量相关 | |
| 排除光照伪影(伪影的位移与玩家运动方向无关)。这是绝对值的权威来源。 | |
| 所以:台阶位置取自 align,每段的绝对偏移取自 align_flow(可信时), | |
| 不可信时用「窗口法减去实测的系统偏置」兜底 —— 偏置本身由那些光流可信的 | |
| 段落算出来(两法之差的中位数),不是拍脑袋定的。 | |
| 另外:align 把 s1 切成了偏移差 <0.15 s 的两段,这在它 0.05 s 的搜索栅格下 | |
| 属于噪声而非真台阶,会被合并回一段。 | |
| 产出 <logs>/<session>/offsets.json —— 下游只读这一个文件。 | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import os | |
| import numpy as np | |
| MERGE_TOL = 0.15 # 相邻段偏移差小于此,视为同一段(不是真台阶) | |
| DRIFT_PEAK_MIN = 0.55 # s1 漂移探针里,只有峰值高于此的块才拿来定常数 | |
| # 20260821 的判定(有据可查,别再重复调查一遍): | |
| # 光流探针把它切成 8 块测,前 4 块 ≈ +0.12、后 4 块 ≈ +0.50,看着像有 0.38 s 台阶。 | |
| # 但这不是真台阶: | |
| # (a) 窗口法(用传送黑屏信号、覆盖全程、对相对偏移敏感)在 vt 40000–85000 | |
| # 每 2500 s 扫一遍,全程只在 +0.25~+0.45 之间抖动,没有任何跳变 —— | |
| # 而同样这个方法把 20260823 的 4.1 s 台阶抓得干干净净。 | |
| # (b) 探针的偏移与它自己的互相关峰值强相关(峰值 0.69->0.48,偏移 +0.10->+0.53), | |
| # 这是低信噪比下的测量偏置特征。这一轮跑着游戏自带的昼夜循环,后半段夜晚和 | |
| # 洞穴居多,暗场景里相位相关会锁到缓慢移动的光效而不是几何位移。 | |
| # 所以 20260821 按单一常数处理,取探针里高峰值块的中位。 | |
| SINGLE_SEGMENT = {"20260821_190601_787"} | |
| MIN_N = 5 # 光流可信所需的最少窗口数 | |
| MIN_PEAK = 0.30 # 矢量相关峰值下限 | |
| def reliable(seg): | |
| return (seg.get("offset_refined") is not None and seg.get("n", 0) >= MIN_N | |
| and seg.get("peak_median", 0) >= MIN_PEAK) | |
| def main(): | |
| ap = argparse.ArgumentParser(description="合并两种对齐测法") | |
| ap.add_argument("--logs", default="/data/zhiyangdeng/data_eybx/logs") | |
| ap.add_argument("--sessions", nargs="*", default=None) | |
| args = ap.parse_args() | |
| sessions = args.sessions or sorted( | |
| d for d in os.listdir(args.logs) if os.path.isdir(os.path.join(args.logs, d))) | |
| # 第一遍:用「光流可信」的段落估计窗口法的系统偏置 | |
| biases = [] | |
| loaded = {} | |
| for sid in sessions: | |
| d = os.path.join(args.logs, sid) | |
| aw = json.load(open(os.path.join(d, "align.json"), encoding="utf-8")) | |
| af = json.load(open(os.path.join(d, "refine_offset.json"), encoding="utf-8")) | |
| loaded[sid] = (aw, af) | |
| for s in af["segments"]: | |
| if reliable(s): | |
| biases.append(s["offset_xcorr"] - s["offset_refined"]) | |
| bias = float(np.median(biases)) if biases else 0.0 | |
| print(f"窗口法系统偏置(由 {len(biases)} 个光流可信段落实测)= {bias:+.3f} s\n") | |
| for sid in sessions: | |
| aw, af = loaded[sid] | |
| flow = {round(s["vt_lo"], 1): s for s in af["segments"]} | |
| segs = [] | |
| for s in aw["segments"]: | |
| fs = flow.get(round(s["vt_lo"], 1)) | |
| if fs and reliable(fs): | |
| off = fs["offset_refined"] | |
| src = f"矢量光流(n={fs['n']}, 峰值{fs['peak_median']:.2f})" | |
| else: | |
| off, src = s["offset"] - bias, "窗口法−偏置" | |
| segs.append(dict(vt_lo=s["vt_lo"], vt_hi=s["vt_hi"], offset=round(off, 3), | |
| source=src, n_blocks=s["n_blocks"])) | |
| # 合并偏移几乎相同的相邻段(不是真台阶,是搜索栅格的噪声) | |
| merged = [segs[0]] | |
| for s in segs[1:]: | |
| if abs(s["offset"] - merged[-1]["offset"]) < MERGE_TOL: | |
| merged[-1]["vt_hi"] = s["vt_hi"] | |
| merged[-1]["n_blocks"] += s["n_blocks"] | |
| merged[-1]["source"] += " +合并" | |
| else: | |
| merged.append(s) | |
| if sid in SINGLE_SEGMENT: | |
| drift_p = os.path.join(args.logs, sid, "s1_drift.json") | |
| val = None | |
| if os.path.exists(drift_p): | |
| blocks = json.load(open(drift_p, encoding="utf-8")) | |
| good = [b[2] for b in blocks if b[3] >= DRIFT_PEAK_MIN] | |
| if good: | |
| val = float(np.median(good)) | |
| print(f" [单段] 由漂移探针 {len(good)}/{len(blocks)} 个高峰值块" | |
| f"(峰值≥{DRIFT_PEAK_MIN}) 定为常数") | |
| if val is None: | |
| val = float(np.median([s["offset"] for s in merged])) | |
| print(" [单段] 探针结果缺失,退回窗口法中位数") | |
| merged = [dict(vt_lo=0.0, vt_hi=1e12, offset=round(val, 3), | |
| source="单段常数(漂移探针高峰值块)", n_blocks=sum( | |
| m["n_blocks"] for m in merged))] | |
| # 段边界向外扩到 ±inf,保证任何 vt 都能落到某一段 | |
| merged[0]["vt_lo"] = 0.0 | |
| merged[-1]["vt_hi"] = 1e12 | |
| for a, b in zip(merged[:-1], merged[1:]): | |
| mid = (a["vt_hi"] + b["vt_lo"]) / 2 | |
| a["vt_hi"] = b["vt_lo"] = mid | |
| # 世界→屏幕映射:取全部可信段落的中位 | |
| M = np.array(af["M_px1280"]) if af.get("M_px1280") else None | |
| print(f"== {sid}") | |
| for s in merged: | |
| print(f" vt {s['vt_lo']:9.1f} – {s['vt_hi']:11.1f} s offset {s['offset']:+.3f} s" | |
| f" [{s['source']}]") | |
| if M is not None: | |
| gx = abs(M[0, 0]) * 832.0 / 1280.0 | |
| gy = abs(M[2, 1]) * 480.0 / 720.0 | |
| print(f" 世界→屏幕(832x480 画幅):横 {gx:.1f} / 纵 {gy:.1f} px/世界单位" | |
| f" gx/gy={gx/max(gy,1e-9):.2f} 坡地项 {M[1,1]*480/720:+.1f}") | |
| print(f" 符号:世界 +x -> 屏幕 {'左' if M[0,0] < 0 else '右'}," | |
| f"世界 +z -> 屏幕 {'下' if M[2,1] > 0 else '上'}") | |
| out = dict(session=sid, convention="video_t = log_vt + offset", | |
| xcorr_bias=bias, segments=merged, | |
| M_px1280=M.tolist() if M is not None else None) | |
| with open(os.path.join(args.logs, sid, "offsets.json"), "w", encoding="utf-8") as fh: | |
| json.dump(out, fh, ensure_ascii=False, indent=1) | |
| print("\nDONE 下游只读 offsets.json") | |
| if __name__ == "__main__": | |
| main() | |