File size: 4,527 Bytes
d262279 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | """Tracker multi-objeto robusto, do zero.
- Kalman [x,y,vx,vy] por jogador
- Compensacao de ego-motion: shift global estimado por phase correlation
entre frames consecutivos (camera broadcast pan/tilt)
- Matching por custo combinado: distancia + cor do uniforme + forma da caixa
"""
import cv2
import numpy as np
class Kalman:
def __init__(self, x, y):
self.X = np.array([x, y, 0.0, 0.0])
self.P = np.diag([10.0, 10.0, 50.0, 50.0])
self.F = np.array([[1, 0, 1, 0], [0, 1, 0, 1],
[0, 0, 1, 0], [0, 0, 0, 1]], float)
self.H = np.array([[1, 0, 0, 0], [0, 1, 0, 0]], float)
self.Q = np.diag([0.5, 0.5, 2.0, 2.0])
self.R = np.eye(2) * 4.0
def predict(self):
self.X = self.F @ self.X
self.P = self.F @ self.P @ self.F.T + self.Q
return self.X[:2]
def update(self, z):
S = self.H @ self.P @ self.H.T + self.R
K = self.P @ self.H.T @ np.linalg.inv(S)
self.X = self.X + K @ (np.asarray(z) - self.H @ self.X)
self.P = (np.eye(4) - K @ self.H) @ self.P
class Track:
_next_id = 0
def __init__(self, box):
self.id = Track._next_id
Track._next_id += 1
self.box = np.array(box, float)
self.kf = Kalman(*self.centroid)
self.trail = [self.centroid]
self.color = None # cor media do uniforme (BGR)
self.team_votes = []
self.hits, self.misses = 1, 0
@property
def centroid(self):
x, y, w, h = self.box
return np.array([x + w / 2, y + h / 2])
def predict(self, shift=(0.0, 0.0)):
c = self.kf.predict()
self.box[:2] = c - self.box[2:] / 2
self.box[0] += shift[0] # compensa movimento da camera
self.box[1] += shift[1]
self.misses += 1
def update(self, box):
self.kf.update([box[0] + box[2] / 2, box[1] + box[3] / 2])
self.box = 0.5 * self.box + 0.5 * np.array(box, float)
self.trail.append(self.centroid.copy())
if len(self.trail) > 40:
self.trail.pop(0)
self.hits += 1
self.misses = 0
def add_color(self, c):
if c is None:
return
self.color = c if self.color is None else 0.9 * self.color + 0.1 * c
def color_dist(self, c):
if self.color is None or c is None:
return 0.0
return min(float(np.linalg.norm(self.color - c)) / 100.0, 2.0)
def global_shift(prev_gray, gray):
"""Phase correlation -> deslocamento global (dx, dy)."""
d, _ = cv2.phaseCorrelate(prev_gray, gray)
return d
class Tracker:
def __init__(self, max_dist=30, max_misses=15, min_hits=5,
w_color=1.5, w_shape=0.5):
self.tracks = []
self.max_dist, self.max_misses, self.min_hits = \
max_dist, max_misses, min_hits
self.w_color, self.w_shape = w_color, w_shape
def _cost(self, t, box, color):
x, y, w, h = box
d = np.linalg.norm(t.centroid - (x + w / 2, y + h / 2))
if d > self.max_dist:
return None
c = t.color_dist(color)
tw, th = t.box[2], t.box[3]
s = abs(np.log((w + 1e-3) / (tw + 1e-3))) + abs(np.log((h + 1e-3) / (th + 1e-3)))
return d + self.w_color * c + self.w_shape * s
def step(self, boxes, colors=None, shift=(0.0, 0.0)):
colors = colors or [None] * len(boxes)
for t in self.tracks:
t.predict(shift)
# matching guloso por custo combinado
pairs = []
for ti, t in enumerate(self.tracks):
for i in range(len(boxes)):
c = self._cost(t, boxes[i], colors[i])
if c is not None:
pairs.append((c, ti, i))
pairs.sort()
used_t, used_b = set(), set()
for c, ti, i in pairs:
if ti in used_t or i in used_b:
continue
used_t.add(ti)
used_b.add(i)
self.tracks[ti].update(boxes[i])
self.tracks[ti].add_color(colors[i])
for i in range(len(boxes)):
if i not in used_b:
t = Track(boxes[i])
t.add_color(colors[i])
self.tracks.append(t)
self.tracks = [t for t in self.tracks if t.misses <= self.max_misses]
return [t for t in self.tracks if t.hits >= self.min_hits]
|