mono-aeb 0.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. aeb/__init__.py +23 -0
  2. aeb/calibration.py +163 -0
  3. aeb/config.py +151 -0
  4. aeb/decision/__init__.py +3 -0
  5. aeb/decision/state_machine.py +67 -0
  6. aeb/detectors/__init__.py +27 -0
  7. aeb/detectors/base.py +14 -0
  8. aeb/detectors/dfine_detector.py +138 -0
  9. aeb/detectors/yolov8_detector.py +65 -0
  10. aeb/distance/__init__.py +4 -0
  11. aeb/distance/base.py +14 -0
  12. aeb/distance/ground_plane.py +33 -0
  13. aeb/ego_speed.py +84 -0
  14. aeb/in_path/__init__.py +4 -0
  15. aeb/in_path/base.py +12 -0
  16. aeb/in_path/fixed_roi.py +58 -0
  17. aeb/pipeline.py +94 -0
  18. aeb/tracker/__init__.py +4 -0
  19. aeb/tracker/base.py +14 -0
  20. aeb/tracker/byte_shims/cython_bbox.py +39 -0
  21. aeb/tracker/bytetrack.py +113 -0
  22. aeb/tracker/vendor/__init__.py +1 -0
  23. aeb/tracker/vendor/bytetrack/LICENSE +21 -0
  24. aeb/tracker/vendor/bytetrack/__init__.py +1 -0
  25. aeb/tracker/vendor/bytetrack/yolox/__init__.py +3 -0
  26. aeb/tracker/vendor/bytetrack/yolox/tracker/__init__.py +1 -0
  27. aeb/tracker/vendor/bytetrack/yolox/tracker/basetrack.py +52 -0
  28. aeb/tracker/vendor/bytetrack/yolox/tracker/byte_tracker.py +328 -0
  29. aeb/tracker/vendor/bytetrack/yolox/tracker/kalman_filter.py +270 -0
  30. aeb/tracker/vendor/bytetrack/yolox/tracker/matching.py +181 -0
  31. aeb/ttc/__init__.py +7 -0
  32. aeb/ttc/base.py +15 -0
  33. aeb/ttc/fusion.py +39 -0
  34. aeb/ttc/history.py +24 -0
  35. aeb/ttc/ttc_distance.py +53 -0
  36. aeb/ttc/ttc_scale.py +52 -0
  37. aeb/types.py +103 -0
  38. mono_aeb-0.1.2.dist-info/METADATA +352 -0
  39. mono_aeb-0.1.2.dist-info/RECORD +43 -0
  40. mono_aeb-0.1.2.dist-info/WHEEL +5 -0
  41. mono_aeb-0.1.2.dist-info/licenses/LICENSE +201 -0
  42. mono_aeb-0.1.2.dist-info/licenses/aeb/tracker/vendor/bytetrack/LICENSE +21 -0
  43. mono_aeb-0.1.2.dist-info/top_level.txt +1 -0
aeb/__init__.py ADDED
@@ -0,0 +1,23 @@
1
+ """AEB 前向碰撞预警链路(检测器无关)。
2
+
3
+ 整条链只消费 `[x1, y1, x2, y2, score, class]`,检测器可自由替换
4
+ (YOLOv8n 默认 / D-FINE 可选)。
5
+ """
6
+
7
+ from .types import (
8
+ AEB_CLASS_NAMES,
9
+ Detection,
10
+ FrameResult,
11
+ RiskFeature,
12
+ RiskLevel,
13
+ Track,
14
+ )
15
+
16
+ __all__ = [
17
+ "AEB_CLASS_NAMES",
18
+ "Detection",
19
+ "FrameResult",
20
+ "RiskFeature",
21
+ "RiskLevel",
22
+ "Track",
23
+ ]
aeb/calibration.py ADDED
@@ -0,0 +1,163 @@
1
+ """逐视频离线自标定:用「GPS ego 速度 + 免标定尺度 TTC」联合反解
2
+ 测距尺度 f·H 与地平线 horizon_y。
3
+
4
+ 原理:
5
+ 对(近似)静止目标,碰撞时间 τ = D / v_ego(接近速度 ≈ 自车速度),
6
+ 尺度 TTC 免标定给出 τ,故 D = v_ego · τ。
7
+ 地面平面模型:y_bottom = horizon_y + f·H / D,即
8
+ y_bottom = horizon_y + f·H · (1/D)
9
+ 对 (1/D, y_bottom) 做 Theil-Sen 稳健直线拟合,一次同时解出:
10
+ - 斜率 = f·H(测距尺度)
11
+ - 截距 = horizon_y(地平线 / 消失点,之前默认 360 未标定)
12
+
13
+ 早期实现(已移除)只解 f·H(horizon 固定 360),近距离目标 denom
14
+ ≈30px 时 ±38px 的地平线误差就会造成约 2 倍 f·H 偏差;联合拟合消除该偏差。
15
+ """
16
+
17
+ import numpy as np
18
+
19
+ from .config import CameraConfig
20
+ from .detectors import BaseDetector
21
+ from .tracker import ByteTrackTracker
22
+ from .ttc import ScaleTTC, TrackHistory
23
+
24
+ # f·H(= focal_px × 相机高)的物理合理范围:典型 dashcam focal_px≈400~1600、
25
+ # 相机高≈1.2~1.7m ⇒ f·H 约 500~2700。放宽到 [300, 3000] 只拦「明显失真」——
26
+ # 坏 track 的尺度反解会把 f·H 拉到 ~37 或 ~5000 这种任何车载相机都不可能的值,
27
+ # 此时测距整体缩放错 10×,宁可回落默认相机参数(f·H=1650)也不用垃圾值。
28
+ FOCAL_HEIGHT_MIN = 300.0
29
+ FOCAL_HEIGHT_MAX = 3000.0
30
+
31
+
32
+ def _theil_sen_line(x: np.ndarray, y: np.ndarray, max_pts: int = 2000):
33
+ """对 (x, y) 做 Theil-Sen 稳健直线拟合,返回 (slope, intercept)。
34
+
35
+ 对约 50% 离群不敏感;样本过多时系统抽稀到 max_pts 以控制 O(N²) 内存。
36
+ """
37
+ x = np.asarray(x, dtype=np.float64)
38
+ y = np.asarray(y, dtype=np.float64)
39
+ if len(x) > max_pts:
40
+ idx = np.linspace(0, len(x) - 1, max_pts).astype(int)
41
+ x, y = x[idx], y[idx]
42
+ n = len(x)
43
+ if n < 2:
44
+ return None, None
45
+ dx = x[:, None] - x[None, :]
46
+ dy = y[:, None] - y[None, :]
47
+ upper = np.triu(np.ones((n, n), dtype=bool), 1)
48
+ valid = upper & (dx != 0)
49
+ if not valid.any():
50
+ return None, None
51
+ slopes = dy[valid] / dx[valid]
52
+ slope = float(np.median(slopes))
53
+ intercept = float(np.median(y - slope * x))
54
+ return slope, intercept
55
+
56
+
57
+ def _collect_ground_samples(
58
+ detector: BaseDetector,
59
+ frames,
60
+ ego_speed_fn,
61
+ fps: float,
62
+ min_ego_speed: float,
63
+ min_history: int,
64
+ max_frames: int | None,
65
+ ):
66
+ """跑一遍 frames,收集 (track_id, D, y_bottom) 样本(D = v_ego·τ)。
67
+
68
+ ego_speed_fn(frame_idx) -> 自车速度 (m/s)。
69
+ 返回 (samples, n_tracks),samples 为 [(track_id, D, y_bottom), ...]。
70
+ """
71
+ tracker = ByteTrackTracker(fps=fps)
72
+ scale_ttc = ScaleTTC(fps)
73
+ history = TrackHistory(max_len=10)
74
+
75
+ samples = []
76
+ track_ids = set()
77
+
78
+ for frame_idx, frame in frames:
79
+ if max_frames is not None and frame_idx >= max_frames:
80
+ break
81
+ v_ego = float(ego_speed_fn(frame_idx))
82
+ dets = detector.detect(frame)
83
+ tracks = tracker.update(dets, frame)
84
+
85
+ active = set()
86
+ for t in tracks:
87
+ active.add(t.track_id)
88
+ history.push(t.track_id, frame_idx, 0.0, t.height)
89
+ history.prune(active)
90
+
91
+ if v_ego < min_ego_speed:
92
+ continue # 车速太低时 τ = D/v 数值不稳
93
+
94
+ for t in tracks:
95
+ if len(history.get(t.track_id)) < min_history:
96
+ continue
97
+ ttc = scale_ttc.estimate(t.track_id, history)
98
+ if not np.isfinite(ttc) or ttc <= 0:
99
+ continue
100
+ D = v_ego * ttc
101
+ y_bottom = t.bbox[3]
102
+ samples.append((t.track_id, D, y_bottom))
103
+ track_ids.add(t.track_id)
104
+
105
+ return samples, len(track_ids)
106
+
107
+
108
+ def estimate_ground_plane(
109
+ detector: BaseDetector,
110
+ frames,
111
+ camera: CameraConfig,
112
+ ego_speed_fn,
113
+ fps: float = 30.0,
114
+ min_ego_speed: float = 2.0,
115
+ min_history: int = 5,
116
+ min_samples: int = 30,
117
+ min_tracks: int = 1,
118
+ max_frames: int | None = None,
119
+ ):
120
+ """联合反解 (f·H, horizon_y):对 (1/D, y_bottom) 做 Theil-Sen 直线拟合。
121
+
122
+ 返回 (focal_height, horizon_y, n_samples, n_tracks);
123
+ 标定失败时 focal_height/horizon_y 为 None。
124
+ """
125
+ samples, n_tracks = _collect_ground_samples(
126
+ detector,
127
+ frames,
128
+ ego_speed_fn,
129
+ fps,
130
+ min_ego_speed,
131
+ min_history,
132
+ max_frames,
133
+ )
134
+ if len(samples) < min_samples or n_tracks < min_tracks:
135
+ return None, None, len(samples), n_tracks
136
+
137
+ D = np.array([s[1] for s in samples], dtype=np.float64)
138
+ y = np.array([s[2] for s in samples], dtype=np.float64)
139
+ x = 1.0 / D
140
+
141
+ # 1) horizon_y:联合拟合截距(对 D 的乘性偏差不敏感,稳健)
142
+ _, horizon = _theil_sen_line(x, y)
143
+ if not np.isfinite(horizon) or not (0.0 <= horizon <= camera.img_h):
144
+ return None, None, len(samples), n_tracks # 地平线必须在可见图像内
145
+
146
+ # 2) f·H:用解出的 horizon 做每轨迹中位(Theil-Sen 斜率对 Kalman 平滑滞后
147
+ # 放大更明显,中位法对 f·H 更稳),再跨轨迹取中位
148
+ per_track = {}
149
+ for tid, D_i, y_bottom in samples:
150
+ denom = y_bottom - horizon
151
+ if denom <= 0:
152
+ continue
153
+ per_track.setdefault(tid, []).append(D_i * denom)
154
+ track_medians = [float(np.median(v)) for v in per_track.values() if v]
155
+ if not track_medians:
156
+ return None, None, len(samples), n_tracks
157
+ fh = float(np.median(track_medians))
158
+
159
+ # 3) f·H 物理合理性:超出 [300, 3000] 判标定失败(见模块级常量注释)
160
+ if not (FOCAL_HEIGHT_MIN <= fh <= FOCAL_HEIGHT_MAX):
161
+ return None, None, len(samples), n_tracks
162
+
163
+ return fh, float(horizon), len(samples), n_tracks
aeb/config.py ADDED
@@ -0,0 +1,151 @@
1
+ """集中配置:相机标定 / 自车路径 / 风险阈值 / 检测跟踪参数。
2
+
3
+ 相机标定(BDD100K 众包采集、相机逐视频不同,无统一内参):
4
+ - 测距只用 f·H 这一个「尺度」量(focal_height 属性),f、H 存在尺度简并;
5
+ - horizon_y 由车道线消失点逐视频求;
6
+ - 尺度逐视频自标定(实际入口):examples/calibrate_video.py 用 calibration.py 的
7
+ estimate_ground_plane() 联合反解 f·H 与 horizon_y,再经 calibrate_scale()/
8
+ calibrate_horizon() 落到本类;
9
+ - calibrate_from_ego_speed()(ego 速度 + 尺度 TTC)与 calibrate_from_known_width()
10
+ (已知车宽)为**接口预留,当前无调用点**;
11
+ - 下面这组默认值只是逐视频标定的初始化/fallback,不是全局真值;
12
+ - 免标定尺度 TTC(TTC_scale = h/ḣ)不经过本类,见 ttc 模块。
13
+ """
14
+
15
+ from collections.abc import Sequence
16
+ from dataclasses import dataclass, field
17
+
18
+
19
+ @dataclass
20
+ class CameraConfig:
21
+ """地面平面相机模型(测距 + ROI 共用同一套参数)。"""
22
+
23
+ cam_height_m: float = 1.5 # 相机离地高度 (m);与 focal_px 只以乘积进入测距
24
+ focal_px: float = 1100.0 # 焦距 (像素,占位值);ROI 横向投影需单独用 f
25
+ horizon_y: float = 360.0 # 地平线像素行(受 pitch 影响,720/2 近似,逐视频校正)
26
+ principal_x: float = 640.0 # 主点 x(图像中心)
27
+ img_w: int = 1280
28
+ img_h: int = 720
29
+
30
+ @property
31
+ def focal_height(self) -> float:
32
+ """测距唯一尺度参数 f·H(像素·米)。
33
+
34
+ 地面平面公式 D = f·H / (y_bottom − horizon_y) 中 f、H 只以乘积出现
35
+ (尺度简并),逐视频标定只需拟合这一个量;横向 ROI 投影仍用 focal_px。
36
+ """
37
+ return self.focal_px * self.cam_height_m
38
+
39
+ def calibrate_horizon(self, horizon_y: float) -> None:
40
+ """逐视频设置地平线(pitch),来自车道线消失点。"""
41
+ self.horizon_y = float(horizon_y)
42
+
43
+ def calibrate_scale(self, focal_height: float) -> None:
44
+ """逐视频设置测距尺度 f·H,落到 focal_px 上(cam_height_m 保持物理含义)。"""
45
+ fh = float(focal_height)
46
+ if fh <= 0:
47
+ raise ValueError(f"focal_height must be positive, got {fh}")
48
+ self.focal_px = fh / self.cam_height_m
49
+
50
+ def calibrate_from_ego_speed(
51
+ self, ttc_scale: float, y_bottom: float, ego_speed_mps: float
52
+ ) -> None:
53
+ """用 ego 速度 + 尺度 TTC 反解测距尺度 f·H(**接口预留,当前无调用点**)。
54
+
55
+ 实际逐视频标定走 examples/calibrate_video.py → calibration.estimate_ground_plane(),
56
+ 其结果经 calibrate_scale() 落到本类;本方法保留为单次反解的对外接口。
57
+
58
+ 尺度 TTC 免标定可得:τ = Δt·s/Δs(s 为跟踪框高/宽)。
59
+ 对(近似)静止目标:D = ego_speed_mps · τ,
60
+ 再 D = f·H/(y_bottom − horizon_y) ⇒ f·H = D·(y_bottom − horizon_y)。
61
+ 多帧/多目标取中位数以抑制噪声(聚合由调用方负责,这里做单次反解)。
62
+ """
63
+ denom = float(y_bottom) - self.horizon_y
64
+ if ttc_scale <= 0 or denom <= 0:
65
+ raise ValueError(
66
+ f"invalid inverse-solve input: ttc_scale={ttc_scale}, y_bottom-horizon={denom}"
67
+ )
68
+ d = float(ego_speed_mps) * ttc_scale
69
+ self.calibrate_scale(d * denom)
70
+
71
+ def calibrate_from_known_width(
72
+ self,
73
+ widths_px: Sequence[float],
74
+ y_bottoms: Sequence[float],
75
+ real_width_m: float = 1.8,
76
+ horizon_y: float | None = None,
77
+ ) -> None:
78
+ """由已知目标宽度反标定相机高度 H(轿车 real_width_m≈1.8m)。
79
+
80
+ w_px = (W/H)·(y_bottom − horizon_y):像素宽对 (y_bottom − horizon_y)
81
+ 是过原点直线,斜率 = W/H ⇒ H = W/斜率。
82
+ 注意:宽度只决定 H 与真实尺寸的比例,不决定绝对尺度 f;
83
+ 绝对尺度还需 calibrate_from_ego_speed() 或额外假设相机高度。
84
+ """
85
+ if horizon_y is not None:
86
+ self.calibrate_horizon(horizon_y)
87
+ xs = [float(y) - self.horizon_y for y in y_bottoms]
88
+ ws = [float(w) for w in widths_px]
89
+ if len(xs) != len(ws) or len(xs) == 0:
90
+ raise ValueError("widths_px and y_bottoms must have equal length and be non-empty")
91
+ if any(x <= 0 for x in xs) or any(w <= 0 for w in ws):
92
+ raise ValueError("y_bottom must be below horizon and widths must be positive")
93
+ # 过原点最小二乘:slope = Σ(w·x) / Σ(x²)
94
+ slope = sum(w * x for w, x in zip(ws, xs, strict=True)) / sum(x * x for x in xs)
95
+ if slope <= 0:
96
+ raise ValueError("fitted slope is non-positive; cannot calibrate")
97
+ self.cam_height_m = float(real_width_m) / slope
98
+
99
+
100
+ @dataclass
101
+ class EgoPathConfig:
102
+ """自车路径走廊:地面平面上的矩形 → 投影成图像梯形。"""
103
+
104
+ lane_width_m: float = 3.5 # 车道宽 (m)
105
+ z_near_m: float = 4.5 # 走廊近端纵向距离 (m)(≈图像底边对应距离)
106
+ z_far_m: float = 80.0 # 走廊远端纵向距离 (m)
107
+ lateral_offset_m: float = 0.0 # 自车中心横向偏移 (m),正=右
108
+
109
+
110
+ @dataclass
111
+ class RiskThresholds:
112
+ """状态机阈值。
113
+
114
+ AEB 触发点随自车速度自适应:固定 TTC 阈值在高速时太激进、
115
+ 低速时太保守,改为按制动模型动态计算
116
+ TTC_AEB(v) = t_react + v / (2·a_max)
117
+ FCW / ATTENTION 在此基础上加固定前置余量,整条决策梯度随车速整体平移。
118
+ """
119
+
120
+ t_react: float = 0.5 # 驾驶员反应时间 (s)
121
+ a_max: float = 6.86 # 紧急制动最大减速度 (m/s²,≈0.7g)
122
+ ttc_fcw_margin: float = 1.0 # FCW 相对 AEB 的前置余量 (s)
123
+ ttc_attention_margin: float = 2.0 # ATTENTION 相对 AEB 的前置余量 (s)
124
+ d_safe: float = 5.0 # 安全距离 (m),用于 a_req
125
+
126
+ def ladder(self, v_ego_mps: float) -> tuple[float, float, float]:
127
+ """返回 (ttc_attention, ttc_fcw, ttc_aeb),随自车速度动态变化。"""
128
+ v = max(v_ego_mps, 0.0)
129
+ aeb = self.t_react + v / (2.0 * self.a_max)
130
+ return (aeb + self.ttc_attention_margin, aeb + self.ttc_fcw_margin, aeb)
131
+
132
+
133
+ @dataclass
134
+ class AEBConfig:
135
+ camera: CameraConfig = field(default_factory=CameraConfig)
136
+ ego_path: EgoPathConfig = field(default_factory=EgoPathConfig)
137
+ risk: RiskThresholds = field(default_factory=RiskThresholds)
138
+
139
+ # 检测器
140
+ det_conf: float = 0.25
141
+ # 跟踪(ByteTrack 默认)
142
+ track_thresh: float = 0.5
143
+ track_buffer: int = 30
144
+ match_thresh: float = 0.8
145
+ fps: float = 30.0
146
+ # 时序
147
+ history_len: int = 10 # N = 5~15
148
+ persist_k: int = 3 # 连续 K 帧低于阈值才升级
149
+ # 自车速度 (m/s):demo/probe 从 samples-1k/info/*.json 的 GPS speed 读真实值,
150
+ # 未提供时用 12 m/s(≈43 km/h 城市)作为 fallback 默认
151
+ ego_speed_mps: float = 12.0
@@ -0,0 +1,3 @@
1
+ from .state_machine import RiskStateMachine
2
+
3
+ __all__ = ["RiskStateMachine"]
@@ -0,0 +1,67 @@
1
+ """4 状态风险状态机,带迟滞避免抖动。
2
+
3
+ NORMAL(0) → ATTENTION(1) → FCW(2) → AEB_WARNING(3)
4
+
5
+ 升级(危险度上升):越过阈值立即进入。
6
+ 降级(危险度下降):需 TTC 超过阈值 + 迟滞带才降,防边缘抖动。
7
+ """
8
+
9
+ import numpy as np
10
+
11
+ from ..config import RiskThresholds
12
+ from ..types import RiskFeature, RiskLevel
13
+
14
+
15
+ class RiskStateMachine:
16
+ def __init__(
17
+ self, thresholds: RiskThresholds, hysteresis: float = 0.3, ego_speed_mps: float = 12.0
18
+ ):
19
+ self.thr = thresholds
20
+ self.hys = hysteresis
21
+ self.ego_speed = ego_speed_mps
22
+ self._level = {} # track_id -> RiskLevel
23
+
24
+ def set_ego_speed(self, v_mps: float):
25
+ """更新自车速度,阈值梯随之平移(demo 逐帧/逐视频更新)。"""
26
+ self.ego_speed = v_mps
27
+
28
+ def _ladder(self) -> tuple[float, float, float]:
29
+ # (ttc_attention, ttc_fcw, ttc_aeb)
30
+ return self.thr.ladder(self.ego_speed)
31
+
32
+ def _raw_level(self, ttc: float) -> RiskLevel:
33
+ if not np.isfinite(ttc):
34
+ return RiskLevel.NORMAL
35
+ att, fcw, aeb = self._ladder()
36
+ if ttc < aeb:
37
+ return RiskLevel.AEB_WARNING
38
+ if ttc < fcw:
39
+ return RiskLevel.FCW
40
+ if ttc < att:
41
+ return RiskLevel.ATTENTION
42
+ return RiskLevel.NORMAL
43
+
44
+ def _down_threshold(self, level: RiskLevel) -> float:
45
+ # 从 level 降到下一级,TTC 需大于该阈值(含迟滞)
46
+ att, fcw, aeb = self._ladder()
47
+ if level == RiskLevel.AEB_WARNING:
48
+ return aeb + self.hys
49
+ if level == RiskLevel.FCW:
50
+ return fcw + self.hys
51
+ if level == RiskLevel.ATTENTION:
52
+ return att + self.hys
53
+ return 0.0
54
+
55
+ def decide(self, risk: RiskFeature) -> RiskLevel:
56
+ ttc = risk.ttc_fused
57
+ raw = self._raw_level(ttc)
58
+ prev = self._level.get(risk.track_id, RiskLevel.NORMAL)
59
+
60
+ if raw >= prev: # noqa: SIM108 —— 显式分支配注释比嵌套三元更易读
61
+ new = raw # 上升或持平 → 立即响应
62
+ else:
63
+ # 下降 → 需明显越过阈值才降级(迟滞)
64
+ new = raw if ttc > self._down_threshold(prev) else prev
65
+
66
+ self._level[risk.track_id] = new
67
+ return new
@@ -0,0 +1,27 @@
1
+ """检测器后端:BaseDetector + YOLOv8 + D-FINE。
2
+
3
+ 默认只导出 BaseDetector(无重依赖)。YOLOv8Detector / DFineDetector / COCO_TO_AEB
4
+ 按需懒加载:`from aeb.detectors import YOLOv8Detector` 仍可用,但只有真正访问时才
5
+ import ultralytics / torch,保证 `--demo`(只用 BaseDetector)不拉入重依赖。
6
+ """
7
+
8
+ from .base import BaseDetector
9
+
10
+ __all__ = ["COCO_TO_AEB", "BaseDetector", "DFineDetector", "YOLOv8Detector"]
11
+
12
+ _LAZY = {
13
+ "YOLOv8Detector": (".yolov8_detector", "YOLOv8Detector"),
14
+ "COCO_TO_AEB": (".yolov8_detector", "COCO_TO_AEB"),
15
+ "DFineDetector": (".dfine_detector", "DFineDetector"),
16
+ }
17
+
18
+
19
+ def __getattr__(name):
20
+ if name in _LAZY:
21
+ import importlib
22
+
23
+ mod_name, attr = _LAZY[name]
24
+ value = getattr(importlib.import_module(mod_name, __name__), attr)
25
+ globals()[name] = value # 缓存,避免重复 import
26
+ return value
27
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
aeb/detectors/base.py ADDED
@@ -0,0 +1,14 @@
1
+ """检测器抽象接口。"""
2
+
3
+ from abc import ABC, abstractmethod
4
+
5
+ import numpy as np
6
+
7
+ from ..types import Detection
8
+
9
+
10
+ class BaseDetector(ABC):
11
+ @abstractmethod
12
+ def detect(self, frame: np.ndarray) -> list[Detection]:
13
+ """输入 BGR 帧,输出检测列表(xyxy + score + class)。"""
14
+ ...
@@ -0,0 +1,138 @@
1
+ """D-FINE 检测器适配(可选高精度后端,optional high-accuracy backend)。
2
+
3
+ 加载 `dfine_hgnetv2_m_aeb.yml` 训练的权重(best_stg1.pth),用 D-FINE 官方
4
+ `tools/inference/torch_inf.py` 的 deploy 方式推理,输出与本链一致的
5
+ `Detection`(AEB 7 类 id,无需再映射)。
6
+
7
+ 预处理与训练 val 完全一致(见 configs/dfine/include/dataloader.yml):
8
+ Resize((640, 640)) 直接拉伸 + ToTensor([0,1],无 mean/std 归一化)。
9
+ D-FINE 是 DETR 风格,boxes 在归一化 [0,1] 坐标里回归,postprocessor 再乘
10
+ 原图尺寸还原,所以拉伸不破坏检测。
11
+
12
+ 用法:
13
+ from aeb.detectors import DFineDetector
14
+ det = DFineDetector(weights="/path/to/best_stg1.pth")
15
+ dets = det.detect(bgr_frame) # -> list[Detection]
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import os
21
+ import sys
22
+ from pathlib import Path
23
+
24
+ import numpy as np
25
+
26
+ from ..types import AEB_CLASS_NAMES, Detection
27
+ from .base import BaseDetector
28
+
29
+ # D-FINE 后端依赖上游仓库,通过环境变量定位,避免硬编码本地路径:
30
+ # DFINE_ROOT D-FINE 上游仓库根目录(内含 src/)
31
+ # MONO_AEB_DFINE_WEIGHTS 微调权重路径(BDD100K 许可受限,权重走受限分发,需自行获取)
32
+ _DFINE_ROOT = os.environ.get("DFINE_ROOT")
33
+ _DEFAULT_WEIGHTS = os.environ.get("MONO_AEB_DFINE_WEIGHTS")
34
+
35
+
36
+ def _resolve_device(device: str):
37
+ """把 '0'/'cpu'/'cuda:0' 归一化成 torch.device,缺 CUDA 时退回 CPU。"""
38
+ import torch
39
+
40
+ if device is None or device == "cpu":
41
+ return torch.device("cpu")
42
+ if str(device).isdigit():
43
+ device = f"cuda:{device}"
44
+ d = torch.device(device)
45
+ if d.type == "cuda" and not torch.cuda.is_available():
46
+ print("[dfine] CUDA unavailable, falling back to CPU")
47
+ d = torch.device("cpu")
48
+ return d
49
+
50
+
51
+ class DFineDetector(BaseDetector):
52
+ def __init__(
53
+ self,
54
+ weights: str = _DEFAULT_WEIGHTS,
55
+ config: str | None = None,
56
+ conf: float = 0.25,
57
+ device: str = "cuda:0",
58
+ ):
59
+ import torch
60
+ import torch.nn as nn
61
+ import torchvision.transforms as T
62
+
63
+ if not _DFINE_ROOT:
64
+ raise RuntimeError(
65
+ "未设置 DFINE_ROOT 环境变量(D-FINE 上游仓库路径),无法加载 D-FINE 后端"
66
+ )
67
+ if str(_DFINE_ROOT) not in sys.path:
68
+ sys.path.insert(0, str(_DFINE_ROOT))
69
+ from src.core import YAMLConfig
70
+
71
+ weights = weights or _DEFAULT_WEIGHTS
72
+ if not weights:
73
+ raise RuntimeError(
74
+ "未提供 D-FINE 权重:请传 weights 参数或设置 MONO_AEB_DFINE_WEIGHTS 环境变量"
75
+ )
76
+ config = (
77
+ Path(config)
78
+ if config
79
+ else (Path(_DFINE_ROOT) / "configs" / "dfine" / "dfine_hgnetv2_m_aeb.yml")
80
+ )
81
+ if not config.exists():
82
+ raise FileNotFoundError(f"D-FINE config not found: {config}")
83
+ if not Path(weights).exists():
84
+ raise FileNotFoundError(f"D-FINE weights not found: {weights}")
85
+
86
+ cfg = YAMLConfig(str(config), resume=str(weights))
87
+ # 用训练权重整体加载,禁掉 HGNetv2 预训练 backbone(避免重复加载)
88
+ if "HGNetv2" in cfg.yaml_cfg:
89
+ cfg.yaml_cfg["HGNetv2"]["pretrained"] = False
90
+
91
+ checkpoint = torch.load(weights, map_location="cpu")
92
+ state = checkpoint["ema"]["module"] if "ema" in checkpoint else checkpoint["model"]
93
+ cfg.model.load_state_dict(state)
94
+
95
+ class _Model(nn.Module):
96
+ def __init__(self, model, postprocessor):
97
+ super().__init__()
98
+ self.model = model
99
+ self.postprocessor = postprocessor
100
+
101
+ def forward(self, images, orig_target_sizes):
102
+ outputs = self.model(images)
103
+ return self.postprocessor(outputs, orig_target_sizes)
104
+
105
+ self.conf = conf
106
+ self._device = _resolve_device(device)
107
+ self._model = _Model(cfg.model.deploy(), cfg.postprocessor.deploy())
108
+ self._model.to(self._device).eval()
109
+ # 预处理尺寸与 config 的 eval_spatial_size([h, w])一致,训练/推理分布不漂移。
110
+ # 640 配置→640 拉伸;960 续训配置→960 拉伸(小目标优化)。
111
+ eval_h, eval_w = cfg.yaml_cfg.get("eval_spatial_size", [640, 640])
112
+ self._transform = T.Compose([T.Resize((eval_h, eval_w)), T.ToTensor()])
113
+
114
+ def detect(self, frame: np.ndarray) -> list[Detection]:
115
+ import torch
116
+ from PIL import Image
117
+
118
+ im_pil = Image.fromarray(frame[:, :, ::-1]) # BGR -> RGB
119
+ w, h = im_pil.size
120
+ orig_size = torch.tensor([[w, h]], device=self._device)
121
+
122
+ im_data = self._transform(im_pil).unsqueeze(0).to(self._device)
123
+
124
+ with torch.no_grad():
125
+ labels, boxes, scores = self._model(im_data, orig_size)
126
+
127
+ labels = labels[0].cpu().numpy()
128
+ boxes = boxes[0].cpu().numpy()
129
+ scores = scores[0].cpu().numpy()
130
+
131
+ dets: list[Detection] = []
132
+ for c, (x1, y1, x2, y2), s in zip(labels, boxes, scores, strict=False):
133
+ if s < self.conf:
134
+ continue
135
+ c = int(c)
136
+ if 0 <= c < len(AEB_CLASS_NAMES):
137
+ dets.append(Detection(float(x1), float(y1), float(x2), float(y2), float(s), c))
138
+ return dets
@@ -0,0 +1,65 @@
1
+ """YOLOv8 检测器适配。
2
+
3
+ 默认假设模型输出的 class id 已是 AEB 7 类 id(person=0…motor=6),
4
+ 即 `data.yaml` 精调的 baseline / D-FINE 都是这种。若用 COCO 预训练的
5
+ 原始 yolov8n.pt,则传入 `class_map=COCO_TO_AEB` 做一次映射。
6
+ """
7
+
8
+ import numpy as np
9
+ from ultralytics import YOLO
10
+
11
+ from ..types import AEB_CLASS_NAMES, Detection
12
+ from .base import BaseDetector
13
+
14
+ # COCO id → AEB id(person/rider/car/truck/bus/bike/motor)
15
+ COCO_TO_AEB = {
16
+ 0: 0, # person → person
17
+ 1: 5, # bicycle → bike
18
+ 2: 2, # car → car
19
+ 3: 6, # motorcycle → motor
20
+ 5: 4, # bus → bus
21
+ 7: 3, # truck → truck
22
+ # 注意:COCO 无 "rider",baseline 里 rider 不出现(正常)
23
+ }
24
+
25
+
26
+ class YOLOv8Detector(BaseDetector):
27
+ def __init__(
28
+ self,
29
+ weights: str,
30
+ conf: float = 0.25,
31
+ imgsz: int = 640,
32
+ device: str = "cuda:0",
33
+ class_map: dict | None = None,
34
+ ):
35
+ self.model = YOLO(weights)
36
+ self.conf = conf
37
+ self.imgsz = imgsz
38
+ self.device = device
39
+ self.class_map = class_map # None = 模型已输出 AEB id
40
+
41
+ def detect(self, frame: np.ndarray) -> list[Detection]:
42
+ res = self.model.predict(
43
+ frame,
44
+ conf=self.conf,
45
+ imgsz=self.imgsz,
46
+ device=self.device,
47
+ verbose=False,
48
+ )[0]
49
+
50
+ boxes = res.boxes
51
+ if boxes is None or len(boxes) == 0:
52
+ return []
53
+
54
+ xyxy = boxes.xyxy.cpu().numpy()
55
+ confs = boxes.conf.cpu().numpy()
56
+ clss = boxes.cls.cpu().numpy().astype(int)
57
+
58
+ dets = []
59
+ for (x1, y1, x2, y2), s, c in zip(xyxy, confs, clss, strict=False):
60
+ aeb_c = int(c) if self.class_map is None else self.class_map.get(int(c))
61
+ if aeb_c is not None and 0 <= aeb_c < len(AEB_CLASS_NAMES):
62
+ dets.append(
63
+ Detection(float(x1), float(y1), float(x2), float(y2), float(s), int(aeb_c))
64
+ )
65
+ return dets
@@ -0,0 +1,4 @@
1
+ from .base import DistanceEstimator
2
+ from .ground_plane import GroundPlaneDistance
3
+
4
+ __all__ = ["DistanceEstimator", "GroundPlaneDistance"]
aeb/distance/base.py ADDED
@@ -0,0 +1,14 @@
1
+ """距离估计抽象接口。"""
2
+
3
+ from abc import ABC, abstractmethod
4
+
5
+ import numpy as np
6
+
7
+ from ..types import Track
8
+
9
+
10
+ class DistanceEstimator(ABC):
11
+ @abstractmethod
12
+ def estimate(self, track: Track, frame: np.ndarray) -> float:
13
+ """返回目标纵向距离(米);不可测时返回 +inf。"""
14
+ ...