第 14 / 30 课。 前置:上一课。本篇的运行范围以实验说明和验证记录为准,构造样例不代表真实任务精度或板端性能。
检测框没有跨帧身份
检测器每帧给出一组框,它的数组顺序不能当作目标 ID。跟踪器先预测已有轨迹位置,再把当前检测与预测关联。匹配成功更新状态,未匹配轨迹暂存,持续丢失后删除,新检测才创建新 ID。
本课采用 SORT 的基本流程,手写教学版实现。状态为 [cx,cy,w,h,vx,vy,vw,vh],与官方 SORT 的面积/长宽比参数化不同;代码不冒充官方复现。
Kalman 的两个步骤
预测用恒速模型把位置推进 dt,协方差 P 随运动和过程噪声增加。更新比较观测 z 与预测 Hx,用 Kalman 增益校正状态。R 描述观测噪声,Q 描述模型误差;增大 R 通常让滤波器更相信预测。
脚本用 Joseph 形式更新协方差,减轻数值误差。这里 Q 是固定教学配置,实际可变 dt 系统应按运动模型重新设计噪声,而不是只改状态转移矩阵。
Hungarian 不会自动理解目标
代价为 1−IoU,不同类别或低于阈值的配对在求解前设为不可用。全局分配比逐个取最近框更一致,但完全遮挡、交叉和相似外观仍可能造成身份交换。
返回值包含 matched 与 missed。没有新检测时保留的预测框不等于已观察到目标,控制端必须能区分。
实验与练习
输入是两条构造轨迹,其中一帧漏掉一个目标,并打乱检测顺序。检查 ID 连续、短暂漏检时轨迹保留、连续空帧后删除。把 max_age 改小,观察新 ID;再扩大每帧位移,观察 IoU 门限失效。
交付逐帧 ID 表和失败片段。保持 ID 数少不是唯一目标:错误地将两个目标粘成同一 ID 也会让数字看似稳定。
配套实验与运行记录
实验:跟踪基础:运动预测、匹配与轨迹生命周期
查看完整代码:main.py
"""实现恒速 Kalman、IoU 匹配和轨迹管理,认识检测与身份关联的区别。"""
from tracker import Tracker
def det(x, y=10):
return {"box": [x, y, x + 20, y + 20], "score": 0.9, "class_id": 0}
def main():
tracker = Tracker(max_age=2)
frames = [[det(10), det(100)], [det(102), det(12)], [det(104)], [det(16), det(106)]]
seen = []
for i, detections in enumerate(frames):
rows = tracker.step(detections)
seen.append(rows)
print(i, [(r["id"], round(r["box"][0], 2), r["matched"]) for r in rows])
assert [r["id"] for r in seen[-1]] == [1, 2]
assert not seen[2][0]["matched"] and seen[3][0]["matched"]
for _ in range(3):
rows = tracker.step([])
assert rows == []
print("expiry passed")
if __name__ == "__main__":
main()
查看完整代码:tracker.py
"""教学版恒速 Kalman + IoU/Hungarian 跟踪;非官方 SORT/ByteTrack 实现。"""
import numpy as np
from scipy.optimize import linear_sum_assignment
def iou(a, b):
a = np.asarray(a, dtype=float)
b = np.asarray(b, dtype=float)
lo = np.maximum(a[:2], b[:2])
hi = np.minimum(a[2:], b[2:])
inter = np.maximum(hi - lo, 0).prod()
union = (
np.maximum(a[2:] - a[:2], 0).prod()
+ np.maximum(b[2:] - b[:2], 0).prod()
- inter
)
return float(inter / union) if union > 0 else 0.0
def measure(box):
box = np.asarray(box, dtype=float)
return np.r_[(box[:2] + box[2:]) / 2, box[2:] - box[:2]]
class Track:
def __init__(self, ident, detection):
self.id = ident
self.label = detection.get("class_id", 0)
self.x = np.r_[measure(detection["box"]), np.zeros(4)]
self.p = np.diag([10.0] * 4 + [100.0] * 4)
self.missed = 0
self.hits = 1
self.matched = True
def box(self):
cx, cy, w, h = self.x[:4]
w, h = max(w, 1), max(h, 1)
return np.array([cx - w / 2, cy - h / 2, cx + w / 2, cy + h / 2])
def predict(self, dt):
f = np.eye(8)
f[:4, 4:] = np.eye(4) * dt
self.x = f @ self.x
self.p = f @ self.p @ f.T + np.eye(8) * 0.1
self.missed += 1
self.matched = False
def update(self, detection):
h = np.eye(4, 8)
r = np.eye(4)
innovation = measure(detection["box"]) - h @ self.x
s = h @ self.p @ h.T + r
k = np.linalg.solve(s, (self.p @ h.T).T).T
self.x += k @ innovation
a = np.eye(8) - k @ h
self.p = a @ self.p @ a.T + k @ r @ k.T
self.missed = 0
self.hits += 1
self.matched = True
class Tracker:
def __init__(self, max_age=2, threshold=0.2):
self.max_age = max_age
self.threshold = threshold
self.tracks = []
self.next_id = 1
def associate(self, indices, detections):
if not indices or not detections:
return [], list(indices), list(range(len(detections)))
cost = np.full((len(indices), len(detections)), 1e6)
for r, index in enumerate(indices):
track = self.tracks[index]
for c, det in enumerate(detections):
overlap = iou(track.box, det["box"])
if track.label == det.get("class_id", 0) and overlap >= self.threshold:
cost[r, c] = 1 - overlap
rows, cols = linear_sum_assignment(cost)
pairs = [(indices[r], int(c)) for r, c in zip(rows, cols) if cost[r, c] < 1e6]
return (
pairs,
[i for i in indices if i not in {a for a, b in pairs}],
[j for j in range(len(detections)) if j not in {b for a, b in pairs}],
)
def step(self, detections, dt=1.0, low=None):
if not np.isfinite(dt) or dt <= 0:
raise ValueError("positive dt required")
for det in list(detections) + list(low or []):
b = np.asarray(det["box"])
if b.shape != (4,) or not np.isfinite(b).all() or np.any(b[2:] <= b[:2]):
raise ValueError("invalid xyxy")
for track in self.tracks:
track.predict(dt)
pairs, unmatched, new = self.associate(
list(range(len(self.tracks))), detections
)
for i, j in pairs:
self.tracks[i].update(detections[j])
if low:
pairs, _, _ = self.associate(unmatched, low)
for i, j in pairs:
self.tracks[i].update(low[j])
for j in new:
self.tracks.append(Track(self.next_id, detections[j]))
self.next_id += 1
self.tracks = [t for t in self.tracks if t.missed <= self.max_age]
return [
{
"id": t.id,
"box": t.box.tolist(),
"matched": t.matched,
"missed": t.missed,
"class_id": t.label,
}
for t in self.tracks
]
在项目根目录执行;下载包解压后,在解压目录执行 python3 main.py。
cd 'content/posts/AI-深度学习/嵌入式视觉模型进阶/14-跟踪基础/code' && python3 main.py
在本篇代码目录执行 python3 main.py。依赖安装、可选入口见包内 README.md;实际运行结果见 verification.md。下载包包含本课使用的辅助模块与样例。
参考与进一步阅读
原始论文或官方文档用于核对结构、接口和适用范围。本篇代码是围绕教学目标编写的最小实验;除明确注明的完整入口外,不视为对应论文模型的完整复现。