Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

Ocsort (#113)

Rory Clear4994ac74cbfd
diff --git a/clearcam.py b/clearcam.py
index ab5fb55..41311ca 100644
--- a/clearcam.py
+++ b/clearcam.py
@@ -482,33 +482,32 @@ class VideoCapture:
frame = Tensor(frame)
pre = preprocess(frame)
preds = do_inf(pre, yolo_infer)[0].numpy()
- if track:
- thresh = (self.settings.get("threshold") if self.settings else 0.5) or 0.5 #todo clean!
- online_targets = tracker.update(preds, [1280,1280], [1280,1280], thresh) #todo, zone in js also hardcoded to 1280
- preds = []
- for x in online_targets:
- if x.tracklet_len < 1: continue # dont alert for 1 frame, too many false positives
- outside = False
- if hasattr(self, "settings") and self.settings is not None and self.settings.get("coords"):
- outside = point_not_in_polygon([[x.tlwh[0], x.tlwh[1]],[(x.tlwh[0]+x.tlwh[2]), x.tlwh[1]],[(x.tlwh[0]), (x.tlwh[1]+x.tlwh[3])],[(x.tlwh[0]+x.tlwh[2]), (x.tlwh[1]+x.tlwh[3])]], self.settings["coords"])
- outside = outside ^ self.settings["outside"]
- non_zone_alert = False
- if outside: # check if any alerts don't use zone
- for _, alert in self.alert_counters.items():
- if not alert.zone:
- non_zone_alert = True
- break
- if not non_zone_alert and outside: continue
- preds.append(np.array([x.tlwh[0],x.tlwh[1],(x.tlwh[0]+x.tlwh[2]),(x.tlwh[1]+x.tlwh[3]),x.score,x.class_id,x.track_id]))
- if (classes is None or str(int(x.class_id)) in classes):
- new = int(x.track_id) not in self.object_set
- new_in_zone = int(x.track_id) not in self.object_set_zone and not outside
- if new:
- self.object_set.add(int(x.track_id))
- self.counter.add(int(x.class_id))
- if new_in_zone: self.object_set_zone.add(int(x.track_id))
- for _, alert in self.alert_counters.items():
- if not alert.get_counts()[1] and ((new and not alert.zone) or (new_in_zone and alert.zone)): alert.add(int(x.class_id))
+ thresh = (self.settings.get("threshold") if self.settings else 0.5) or 0.5 #todo clean!
+ online_targets = tracker.update(preds, [1280,1280], [1280,1280], thresh) #todo, zone in js also hardcoded to 1280
+ preds = []
+ for x in online_targets:
+ if x.tracklet_len < 1: continue # dont alert for 1 frame, too many false positives
+ outside = False
+ if hasattr(self, "settings") and self.settings is not None and self.settings.get("coords"):
+ outside = point_not_in_polygon([[x.tlwh[0], x.tlwh[1]],[(x.tlwh[0]+x.tlwh[2]), x.tlwh[1]],[(x.tlwh[0]), (x.tlwh[1]+x.tlwh[3])],[(x.tlwh[0]+x.tlwh[2]), (x.tlwh[1]+x.tlwh[3])]], self.settings["coords"])
+ outside = outside ^ self.settings["outside"]
+ non_zone_alert = False
+ if outside: # check if any alerts don't use zone
+ for _, alert in self.alert_counters.items():
+ if not alert.zone:
+ non_zone_alert = True
+ break
+ if not non_zone_alert and outside: continue
+ preds.append(np.array([x.tlwh[0],x.tlwh[1],(x.tlwh[0]+x.tlwh[2]),(x.tlwh[1]+x.tlwh[3]),x.score,x.class_id,x.track_id]))
+ if (classes is None or str(int(x.class_id)) in classes):
+ new = int(x.track_id) not in self.object_set
+ new_in_zone = int(x.track_id) not in self.object_set_zone and not outside
+ if new:
+ self.object_set.add(int(x.track_id))
+ self.counter.add(int(x.class_id))
+ if new_in_zone: self.object_set_zone.add(int(x.track_id))
+ for _, alert in self.alert_counters.items():
+ if not alert.get_counts()[1] and ((new and not alert.zone) or (new_in_zone and alert.zone)): alert.add(int(x.class_id))
preds = np.array(preds)
preds = scale_boxes(pre.shape[:2], preds, frame.shape)
@@ -1549,16 +1548,8 @@ if __name__ == "__main__":
cam_name = next((arg.split("=", 1)[1] for arg in sys.argv[1:] if arg.startswith("--cam_name=")), "my_camera")
- track = True #for now
- if track:
- from yolox.tracker.byte_tracker import BYTETracker
- class Args:
- def __init__(self):
- self.track_buffer = 60 # frames, was 30
- self.mot20 = False
- self.match_thresh = 0.9
-
- tracker = BYTETracker(Args())
+ from ocsort_tracker import ocsort
+ tracker = ocsort.OCSort()
live_link = dict()
if rtsp_url is None:
diff --git a/detection/__pycache__/yolov9.cpython-311.pyc b/detection/__pycache__/yolov9.cpython-311.pyc
deleted file mode 100644
index 808caa5..0000000
Binary files a/detection/__pycache__/yolov9.cpython-311.pyc and /dev/null differ
diff --git a/ocsort_tracker/STrack.py b/ocsort_tracker/STrack.py
new file mode 100644
index 0000000..93198ac
--- /dev/null
+++ b/ocsort_tracker/STrack.py
@@ -0,0 +1,67 @@
+from collections import defaultdict
+import numpy as np
+
+class STrack:
+ def __init__(self, tlwh, score, class_id, track_id=None, age=0):
+ self.occurrences = defaultdict(float)
+ # wait activate
+ self._tlwh = np.asarray(tlwh, dtype=np.float64)
+ self.kalman_filter = None
+ self.mean, self.covariance = None, None
+ self.is_activated = False
+
+ self.score = score
+ self.class_id = class_id
+ self.track_id = track_id
+ self.tracklet_len = age
+
+ @property
+ # @jit(nopython=True)
+ def tlwh(self):
+ """Get current position in bounding box format `(top left x, top left y,
+ width, height)`.
+ """
+ if self.mean is None:
+ return self._tlwh.copy()
+ ret = self.mean[:4].copy()
+ ret[2] *= ret[3]
+ ret[:2] -= ret[2:] / 2
+ return ret
+
+ @property
+ # @jit(nopython=True)
+ def tlbr(self):
+ """Convert bounding box to format `(min x, min y, max x, max y)`, i.e.,
+ `(top left, bottom right)`.
+ """
+ ret = self.tlwh.copy()
+ ret[2:] += ret[:2]
+ return ret
+
+ @staticmethod
+ # @jit(nopython=True)
+ def tlwh_to_xyah(tlwh):
+ """Convert bounding box to format `(center x, center y, aspect ratio,
+ height)`, where the aspect ratio is `width / height`.
+ """
+ ret = np.asarray(tlwh).copy()
+ ret[:2] += ret[2:] / 2
+ ret[2] /= ret[3]
+ return ret
+
+ def to_xyah(self):
+ return self.tlwh_to_xyah(self.tlwh)
+
+ @staticmethod
+ # @jit(nopython=True)
+ def tlbr_to_tlwh(tlbr):
+ ret = np.asarray(tlbr).copy()
+ ret[2:] -= ret[:2]
+ return ret
+
+ @staticmethod
+ # @jit(nopython=True)
+ def tlwh_to_tlbr(tlwh):
+ ret = np.asarray(tlwh).copy()
+ ret[2:] += ret[:2]
+ return ret
\ No newline at end of file
diff --git a/ocsort_tracker/__pycache__/STrack.cpython-311.pyc b/ocsort_tracker/__pycache__/STrack.cpython-311.pyc
new file mode 100644
index 0000000..d5ef3cc
Binary files /dev/null and b/ocsort_tracker/__pycache__/STrack.cpython-311.pyc differ
diff --git a/ocsort_tracker/__pycache__/association.cpython-311.pyc b/ocsort_tracker/__pycache__/association.cpython-311.pyc
new file mode 100644
index 0000000..d6f1a3f
Binary files /dev/null and b/ocsort_tracker/__pycache__/association.cpython-311.pyc differ
diff --git a/ocsort_tracker/__pycache__/kalmanfilter.cpython-311.pyc b/ocsort_tracker/__pycache__/kalmanfilter.cpython-311.pyc
new file mode 100644
index 0000000..354a442
Binary files /dev/null and b/ocsort_tracker/__pycache__/kalmanfilter.cpython-311.pyc differ
diff --git a/ocsort_tracker/__pycache__/ocsort.cpython-311.pyc b/ocsort_tracker/__pycache__/ocsort.cpython-311.pyc
new file mode 100644
index 0000000..a804ef7
Binary files /dev/null and b/ocsort_tracker/__pycache__/ocsort.cpython-311.pyc differ
diff --git a/ocsort_tracker/association.py b/ocsort_tracker/association.py
new file mode 100644
index 0000000..54ca7ce
--- /dev/null
+++ b/ocsort_tracker/association.py
@@ -0,0 +1,240 @@
+import numpy as np
+
+def iou_batch(bboxes1, bboxes2):
+ """
+ From SORT: Computes IOU between two bboxes in the form [x1,y1,x2,y2]
+ """
+ bboxes2 = np.expand_dims(bboxes2, 0)
+ bboxes1 = np.expand_dims(bboxes1, 1)
+
+ xx1 = np.maximum(bboxes1[..., 0], bboxes2[..., 0])
+ yy1 = np.maximum(bboxes1[..., 1], bboxes2[..., 1])
+ xx2 = np.minimum(bboxes1[..., 2], bboxes2[..., 2])
+ yy2 = np.minimum(bboxes1[..., 3], bboxes2[..., 3])
+ w = np.maximum(0., xx2 - xx1)
+ h = np.maximum(0., yy2 - yy1)
+ wh = w * h
+ o = wh / ((bboxes1[..., 2] - bboxes1[..., 0]) * (bboxes1[..., 3] - bboxes1[..., 1])
+ + (bboxes2[..., 2] - bboxes2[..., 0]) * (bboxes2[..., 3] - bboxes2[..., 1]) - wh)
+ return(o)
+
+def speed_direction_batch(dets, tracks):
+ tracks = tracks[..., np.newaxis]
+ CX1, CY1 = (dets[:,0] + dets[:,2])/2.0, (dets[:,1]+dets[:,3])/2.0
+ CX2, CY2 = (tracks[:,0] + tracks[:,2]) /2.0, (tracks[:,1]+tracks[:,3])/2.0
+ dx = CX1 - CX2
+ dy = CY1 - CY2
+ norm = np.sqrt(dx**2 + dy**2) + 1e-6
+ dx = dx / norm
+ dy = dy / norm
+ return dy, dx # size: num_track x num_det
+
+
+def linear_assignment_scipy(cost_matrix):
+ from scipy.optimize import linear_sum_assignment
+ x, y = linear_sum_assignment(cost_matrix)
+ return np.array(list(zip(x, y)))
+
+
+def linear_assignment(cost_matrix):
+ if cost_matrix.size == 0:
+ return np.empty((0, 2), dtype=int)
+ cost = cost_matrix.copy()
+ rows, cols = cost.shape
+ assignments = []
+ row_used = np.zeros(rows, dtype=bool)
+ col_used = np.zeros(cols, dtype=bool)
+ flat_indices = np.argsort(cost, axis=None)
+ row_indices = flat_indices // cols
+ col_indices = flat_indices % cols
+
+ for r, c in zip(row_indices, col_indices):
+ if not row_used[r] and not col_used[c]:
+ assignments.append([r, c])
+ row_used[r] = True
+ col_used[c] = True
+ if row_used.all() or col_used.all():
+ break
+
+ return np.array(assignments)
+
+def associate_detections_to_trackers(detections,trackers,iou_threshold = 0.3):
+ """
+ Assigns detections to tracked object (both represented as bounding boxes)
+ Returns 3 lists of matches, unmatched_detections and unmatched_trackers
+ """
+ if(len(trackers)==0):
+ return np.empty((0,2),dtype=int), np.arange(len(detections)), np.empty((0,5),dtype=int)
+
+ iou_matrix = iou_batch(detections, trackers)
+
+ if min(iou_matrix.shape) > 0:
+ a = (iou_matrix > iou_threshold).astype(np.int32)
+ if a.sum(1).max() == 1 and a.sum(0).max() == 1:
+ matched_indices = np.stack(np.where(a), axis=1)
+ else:
+ matched_indices = linear_assignment(-iou_matrix)
+ else:
+ matched_indices = np.empty(shape=(0,2))
+
+ unmatched_detections = []
+ for d, det in enumerate(detections):
+ if(d not in matched_indices[:,0]):
+ unmatched_detections.append(d)
+ unmatched_trackers = []
+ for t, trk in enumerate(trackers):
+ if(t not in matched_indices[:,1]):
+ unmatched_trackers.append(t)
+
+ #filter out matched with low IOU
+ matches = []
+ for m in matched_indices:
+ if(iou_matrix[m[0], m[1]]<iou_threshold):
+ unmatched_detections.append(m[0])
+ unmatched_trackers.append(m[1])
+ else:
+ matches.append(m.reshape(1,2))
+ if(len(matches)==0):
+ matches = np.empty((0,2),dtype=int)
+ else:
+ matches = np.concatenate(matches,axis=0)
+
+ return matches, np.array(unmatched_detections), np.array(unmatched_trackers)
+
+
+def associate(detections, trackers, iou_threshold, velocities, previous_obs, vdc_weight):
+ if(len(trackers)==0):
+ return np.empty((0,2),dtype=int), np.arange(len(detections)), np.empty((0,5),dtype=int)
+
+ Y, X = speed_direction_batch(detections, previous_obs)
+ inertia_Y, inertia_X = velocities[:,0], velocities[:,1]
+ inertia_Y = np.repeat(inertia_Y[:, np.newaxis], Y.shape[1], axis=1)
+ inertia_X = np.repeat(inertia_X[:, np.newaxis], X.shape[1], axis=1)
+ diff_angle_cos = inertia_X * X + inertia_Y * Y
+ diff_angle_cos = np.clip(diff_angle_cos, a_min=-1, a_max=1)
+ diff_angle = np.arccos(diff_angle_cos)
+ diff_angle = (np.pi /2.0 - np.abs(diff_angle)) / np.pi
+
+ valid_mask = np.ones(previous_obs.shape[0])
+ valid_mask[np.where(previous_obs[:,4]<0)] = 0
+
+ iou_matrix = iou_batch(detections, trackers)
+ scores = np.repeat(detections[:,-1][:, np.newaxis], trackers.shape[0], axis=1)
+ # iou_matrix = iou_matrix * scores # a trick sometiems works, we don't encourage this
+ valid_mask = np.repeat(valid_mask[:, np.newaxis], X.shape[1], axis=1)
+
+ angle_diff_cost = (valid_mask * diff_angle) * vdc_weight
+ angle_diff_cost = angle_diff_cost.T
+ angle_diff_cost = angle_diff_cost * scores
+
+ if min(iou_matrix.shape) > 0:
+ a = (iou_matrix > iou_threshold).astype(np.int32)
+ if a.sum(1).max() == 1 and a.sum(0).max() == 1:
+ matched_indices = np.stack(np.where(a), axis=1)
+ else:
+ matched_indices = linear_assignment(-(iou_matrix+angle_diff_cost))
+ else:
+ matched_indices = np.empty(shape=(0,2))
+
+ unmatched_detections = []
+ for d, det in enumerate(detections):
+ if(d not in matched_indices[:,0]):
+ unmatched_detections.append(d)
+ unmatched_trackers = []
+ for t, trk in enumerate(trackers):
+ if(t not in matched_indices[:,1]):
+ unmatched_trackers.append(t)
+
+ # filter out matched with low IOU
+ matches = []
+ for m in matched_indices:
+ if(iou_matrix[m[0], m[1]]<iou_threshold):
+ unmatched_detections.append(m[0])
+ unmatched_trackers.append(m[1])
+ else:
+ matches.append(m.reshape(1,2))
+ if(len(matches)==0):
+ matches = np.empty((0,2),dtype=int)
+ else:
+ matches = np.concatenate(matches,axis=0)
+
+ return matches, np.array(unmatched_detections), np.array(unmatched_trackers)
+
+
+def associate_kitti(detections, trackers, det_cates, iou_threshold,
+ velocities, previous_obs, vdc_weight):
+ if(len(trackers)==0):
+ return np.empty((0,2),dtype=int), np.arange(len(detections)), np.empty((0,5),dtype=int)
+
+ """
+ Cost from the velocity direction consistency
+ """
+ Y, X = speed_direction_batch(detections, previous_obs)
+ inertia_Y, inertia_X = velocities[:,0], velocities[:,1]
+ inertia_Y = np.repeat(inertia_Y[:, np.newaxis], Y.shape[1], axis=1)
+ inertia_X = np.repeat(inertia_X[:, np.newaxis], X.shape[1], axis=1)
+ diff_angle_cos = inertia_X * X + inertia_Y * Y
+ diff_angle_cos = np.clip(diff_angle_cos, a_min=-1, a_max=1)
+ diff_angle = np.arccos(diff_angle_cos)
+ diff_angle = (np.pi /2.0 - np.abs(diff_angle)) / np.pi
+
+ valid_mask = np.ones(previous_obs.shape[0])
+ valid_mask[np.where(previous_obs[:,4]<0)]=0
+ valid_mask = np.repeat(valid_mask[:, np.newaxis], X.shape[1], axis=1)
+
+ scores = np.repeat(detections[:,-1][:, np.newaxis], trackers.shape[0], axis=1)
+ angle_diff_cost = (valid_mask * diff_angle) * vdc_weight
+ angle_diff_cost = angle_diff_cost.T
+ angle_diff_cost = angle_diff_cost * scores
+
+ """
+ Cost from IoU
+ """
+ iou_matrix = iou_batch(detections, trackers)
+
+
+ """
+ With multiple categories, generate the cost for catgory mismatch
+ """
+ num_dets = detections.shape[0]
+ num_trk = trackers.shape[0]
+ cate_matrix = np.zeros((num_dets, num_trk))
+ for i in range(num_dets):
+ for j in range(num_trk):
+ if det_cates[i] != trackers[j, 4]:
+ cate_matrix[i][j] = -1e6
+
+ cost_matrix = - iou_matrix -angle_diff_cost - cate_matrix
+
+ if min(iou_matrix.shape) > 0:
+ a = (iou_matrix > iou_threshold).astype(np.int32)
+ if a.sum(1).max() == 1 and a.sum(0).max() == 1:
+ matched_indices = np.stack(np.where(a), axis=1)
+ else:
+ matched_indices = linear_assignment(cost_matrix)
+ else:
+ matched_indices = np.empty(shape=(0,2))
+
+ unmatched_detections = []
+ for d, det in enumerate(detections):
+ if(d not in matched_indices[:,0]):
+ unmatched_detections.append(d)
+ unmatched_trackers = []
+ for t, trk in enumerate(trackers):
+ if(t not in matched_indices[:,1]):
+ unmatched_trackers.append(t)
+
+ #filter out matched with low IOU
+ matches = []
+ for m in matched_indices:
+ if(iou_matrix[m[0], m[1]]<iou_threshold):
+ unmatched_detections.append(m[0])
+ unmatched_trackers.append(m[1])
+ else:
+ matches.append(m.reshape(1,2))
+ if(len(matches)==0):
+ matches = np.empty((0,2),dtype=int)
+ else:
+ matches = np.concatenate(matches,axis=0)
+
+ return matches, np.array(unmatched_detections), np.array(unmatched_trackers)
\ No newline at end of file
diff --git a/ocsort_tracker/kalmanfilter.py b/ocsort_tracker/kalmanfilter.py
new file mode 100644
index 0000000..8bfe76b
--- /dev/null
+++ b/ocsort_tracker/kalmanfilter.py
@@ -0,0 +1,231 @@
+# https://github.com/noahcao/OC_SORT
+from __future__ import absolute_import, division
+
+from copy import deepcopy
+from math import log
+import sys
+import numpy as np
+from numpy import dot, zeros, eye, isscalar
+
+class KalmanFilterNew(object):
+ def __init__(self, dim_x, dim_z, dim_u=0):
+ if dim_x < 1:
+ raise ValueError('dim_x must be 1 or greater')
+ if dim_z < 1:
+ raise ValueError('dim_z must be 1 or greater')
+ if dim_u < 0:
+ raise ValueError('dim_u must be 0 or greater')
+
+ self.dim_x = dim_x
+ self.dim_z = dim_z
+ self.dim_u = dim_u
+
+ self.x = zeros((dim_x, 1)) # state
+ self.P = eye(dim_x) # uncertainty covariance
+ self.Q = eye(dim_x) # process uncertainty
+ self.B = None # control transition matrix
+ self.F = eye(dim_x) # state transition matrix
+ self.H = zeros((dim_z, dim_x)) # measurement function
+ self.R = eye(dim_z) # measurement uncertainty
+ self._alpha_sq = 1. # fading memory control
+ self.M = np.zeros((dim_x, dim_z)) # process-measurement cross correlation
+ self.z = np.array([[None]*self.dim_z]).T
+
+ # gain and residual are computed during the innovation step. We
+ # save them so that in case you want to inspect them for various
+ # purposes
+ self.K = np.zeros((dim_x, dim_z)) # kalman gain
+ self.y = zeros((dim_z, 1))
+ self.S = np.zeros((dim_z, dim_z)) # system uncertainty
+ self.SI = np.zeros((dim_z, dim_z)) # inverse system uncertainty
+
+ # identity matrix. Do not alter this.
+ self._I = np.eye(dim_x)
+
+ # these will always be a copy of x,P after predict() is called
+ self.x_prior = self.x.copy()
+ self.P_prior = self.P.copy()
+
+ # these will always be a copy of x,P after update() is called
+ self.x_post = self.x.copy()
+ self.P_post = self.P.copy()
+
+ # Only computed only if requested via property
+ self._log_likelihood = log(sys.float_info.min)
+ self._likelihood = sys.float_info.min
+ self._mahalanobis = None
+
+ # keep all observations
+ self.history_obs = []
+
+ self.inv = np.linalg.inv
+
+ self.attr_saved = None
+ self.observed = False
+
+ def predict(self, u=None, B=None, F=None, Q=None):
+ if B is None:
+ B = self.B
+ if F is None:
+ F = self.F
+ if Q is None:
+ Q = self.Q
+ elif isscalar(Q):
+ Q = eye(self.dim_x) * Q
+
+
+ # x = Fx + Bu
+ if B is not None and u is not None:
+ self.x = dot(F, self.x) + dot(B, u)
+ else:
+ self.x = dot(F, self.x)
+
+ # P = FPF' + Q
+ self.P = self._alpha_sq * dot(dot(F, self.P), F.T) + Q
+
+ # save prior
+ self.x_prior = self.x.copy()
+ self.P_prior = self.P.copy()
+
+
+
+ def freeze(self): self.attr_saved = deepcopy(self.__dict__)
+
+
+ def unfreeze(self):
+ if self.attr_saved is not None:
+ new_history = deepcopy(self.history_obs)
+ self.__dict__ = self.attr_saved
+ # self.history_obs = new_history
+ self.history_obs = self.history_obs[:-1]
+ occur = [int(d is None) for d in new_history]
+ indices = np.where(np.array(occur)==0)[0]
+ index1 = indices[-2]
+ index2 = indices[-1]
+ box1 = new_history[index1]
+ x1, y1, s1, r1 = box1
+ w1 = np.sqrt(s1 * r1)
+ h1 = np.sqrt(s1 / r1)
+ box2 = new_history[index2]
+ x2, y2, s2, r2 = box2
+ w2 = np.sqrt(s2 * r2)
+ h2 = np.sqrt(s2 / r2)
+ time_gap = index2 - index1
+ dx = (x2-x1)/time_gap
+ dy = (y2-y1)/time_gap
+ dw = (w2-w1)/time_gap
+ dh = (h2-h1)/time_gap
+ for i in range(index2 - index1):
+ """
+ The default virtual trajectory generation is by linear
+ motion (constant speed hypothesis), you could modify this
+ part to implement your own.
+ """
+ x = x1 + (i+1) * dx
+ y = y1 + (i+1) * dy
+ w = w1 + (i+1) * dw
+ h = h1 + (i+1) * dh
+ s = w * h
+ r = w / float(h)
+ new_box = np.array([x, y, s, r]).reshape((4, 1))
+ """
+ I still use predict-update loop here to refresh the parameters,
+ but this can be faster by directly modifying the internal parameters
+ as suggested in the paper. I keep this naive but slow way for
+ easy read and understanding
+ """
+ self.update(new_box)
+ if not i == (index2-index1-1):
+ self.predict()
+
+ def update(self, z, R=None, H=None):
+ # set to None to force recompute
+ self._log_likelihood = None
+ self._likelihood = None
+ self._mahalanobis = None
+
+ # append the observation
+ self.history_obs.append(z)
+
+ if z is None:
+ if self.observed:
+ """
+ Got no observation so freeze the current parameters for future
+ potential online smoothing.
+ """
+ self.freeze()
+ self.observed = False
+ self.z = np.array([[None]*self.dim_z]).T
+ self.x_post = self.x.copy()
+ self.P_post = self.P.copy()
+ self.y = zeros((self.dim_z, 1))
+ return
+
+ # self.observed = True
+ if not self.observed:
+ """
+ Get observation, use online smoothing to re-update parameters
+ """
+ self.unfreeze()
+ self.observed = True
+
+ if R is None:
+ R = self.R
+ elif isscalar(R):
+ R = eye(self.dim_z) * R
+
+ if H is None:
+ z = reshape_z(z, self.dim_z, self.x.ndim)
+ H = self.H
+
+ # y = z - Hx
+ # error (residual) between measurement and prediction
+ self.y = z - dot(H, self.x)
+
+ # common subexpression for speed
+ PHT = dot(self.P, H.T)
+
+ # S = HPH' + R
+ # project system uncertainty into measurement space
+ self.S = dot(H, PHT) + R
+ self.SI = self.inv(self.S)
+ # K = PH'inv(S)
+ # map system uncertainty into kalman gain
+ self.K = dot(PHT, self.SI)
+
+ # x = x + Ky
+ # predict new x with residual scaled by the kalman gain
+ self.x = self.x + dot(self.K, self.y)
+
+ # P = (I-KH)P(I-KH)' + KRK'
+ # This is more numerically stable
+ # and works for non-optimal K vs the equation
+ # P = (I-KH)P usually seen in the literature.
+
+ I_KH = self._I - dot(self.K, H)
+ self.P = dot(dot(I_KH, self.P), I_KH.T) + dot(dot(self.K, R), self.K.T)
+
+ # save measurement and posterior state
+ self.z = deepcopy(z)
+ self.x_post = self.x.copy()
+ self.P_post = self.P.copy()
+
+def reshape_z(z, dim_z, ndim):
+ """ensure z is a (dim_z, 1) shaped vector"""
+
+ z = np.atleast_2d(z)
+ if z.shape[1] == dim_z:
+ z = z.T
+
+ if z.shape != (dim_z, 1):
+ raise ValueError(
+ "z (shape {}) must be convertible to shape ({}, 1)".format(z.shape, dim_z)
+ )
+
+ if ndim == 1:
+ z = z[:, 0]
+
+ if ndim == 0:
+ z = z[0, 0]
+
+ return z
\ No newline at end of file
diff --git a/ocsort_tracker/ocsort.py b/ocsort_tracker/ocsort.py
new file mode 100644
index 0000000..398f8e1
--- /dev/null
+++ b/ocsort_tracker/ocsort.py
@@ -0,0 +1,321 @@
+"""
+ This script is adopted from the SORT script by Alex Bewley [email protected]
+"""
+from __future__ import print_function
+
+import numpy as np
+from .association import *
+from ocsort_tracker.STrack import STrack
+from collections import defaultdict
+
+def k_previous_obs(observations, cur_age, k):
+ if len(observations) == 0:
+ return [-1, -1, -1, -1, -1]
+ for i in range(k):
+ dt = k - i
+ if cur_age - dt in observations:
+ return observations[cur_age-dt]
+ max_age = max(observations.keys())
+ return observations[max_age]
+
+
+def convert_bbox_to_z(bbox):
+ """
+ Takes a bounding box in the form [x1,y1,x2,y2] and returns z in the form
+ [x,y,s,r] where x,y is the centre of the box and s is the scale/area and r is
+ the aspect ratio
+ """
+ w = bbox[2] - bbox[0]
+ h = bbox[3] - bbox[1]
+ x = bbox[0] + w/2.
+ y = bbox[1] + h/2.
+ s = w * h # scale is just area
+ r = w / float(h+1e-6)
+ return np.array([x, y, s, r]).reshape((4, 1))
+
+
+def convert_x_to_bbox(x, score=None):
+ """
+ Takes a bounding box in the centre form [x,y,s,r] and returns it in the form
+ [x1,y1,x2,y2] where x1,y1 is the top left and x2,y2 is the bottom right
+ """
+ w = np.sqrt(x[2] * x[3])
+ h = x[2] / w
+ if(score == None):
+ return np.array([x[0]-w/2., x[1]-h/2., x[0]+w/2., x[1]+h/2.]).reshape((1, 4))
+ else:
+ return np.array([x[0]-w/2., x[1]-h/2., x[0]+w/2., x[1]+h/2., score]).reshape((1, 5))
+
+
+def speed_direction(bbox1, bbox2):
+ cx1, cy1 = (bbox1[0]+bbox1[2]) / 2.0, (bbox1[1]+bbox1[3])/2.0
+ cx2, cy2 = (bbox2[0]+bbox2[2]) / 2.0, (bbox2[1]+bbox2[3])/2.0
+ speed = np.array([cy2-cy1, cx2-cx1])
+ norm = np.sqrt((cy2-cy1)**2 + (cx2-cx1)**2) + 1e-6
+ return speed / norm
+
+
+class KalmanBoxTracker(object):
+ """
+ This class represents the internal state of individual tracked objects observed as bbox.
+ """
+ count = 0
+
+ def __init__(self, bbox, delta_t=3, orig=False):
+ """
+ Initialises a tracker using initial bounding box.
+
+ """
+
+ from .kalmanfilter import KalmanFilterNew as KalmanFilter
+ self.kf = KalmanFilter(dim_x=7, dim_z=4)
+ self.kf.F = np.array([[1, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0, 1], [
+ 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 1]])
+ self.kf.H = np.array([[1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0],
+ [0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0]])
+
+ self.kf.R[2:, 2:] *= 10.
+ self.kf.P[4:, 4:] *= 1000. # give high uncertainty to the unobservable initial velocities
+ self.kf.P *= 10.
+ self.kf.Q[-1, -1] *= 0.01
+ self.kf.Q[4:, 4:] *= 0.01
+
+ self.kf.x[:4] = convert_bbox_to_z(bbox)
+ self.time_since_update = 0
+ self.id = KalmanBoxTracker.count
+ KalmanBoxTracker.count += 1
+ self.history = []
+ self.hits = 0
+ self.hit_streak = 0
+ self.age = 0
+ self.occurrences = defaultdict(float)
+ """
+ NOTE: [-1,-1,-1,-1,-1] is a compromising placeholder for non-observation status, the same for the return of
+ function k_previous_obs. It is ugly and I do not like it. But to support generate observation array in a
+ fast and unified way, which you would see below k_observations = np.array([k_previous_obs(...]]), let's bear it for now.
+ """
+ self.last_observation = np.array([-1, -1, -1, -1, -1]) # placeholder
+ self.observations = dict()
+ self.history_observations = []
+ self.velocity = None
+ self.delta_t = delta_t
+
+ def update(self, bbox, score=None, class_id=None):
+ """
+ Updates the state vector with observed bbox.
+ """
+ if bbox is not None:
+ if score is not None:
+ self.occurrences[class_id] += score
+ self.class_id = max(self.occurrences, key=self.occurrences.get)
+ if self.last_observation.sum() >= 0: # no previous observation
+ previous_box = None
+ for i in range(self.delta_t):
+ dt = self.delta_t - i
+ if self.age - dt in self.observations:
+ previous_box = self.observations[self.age-dt]
+ break
+ if previous_box is None:
+ previous_box = self.last_observation
+ """
+ Estimate the track speed direction with observations \Delta t steps away
+ """
+ self.velocity = speed_direction(previous_box, bbox)
+
+ """
+ Insert new observations. This is a ugly way to maintain both self.observations
+ and self.history_observations. Bear it for the moment.
+ """
+ self.last_observation = bbox
+ self.observations[self.age] = bbox
+ self.history_observations.append(bbox)
+
+ self.time_since_update = 0
+ self.history = []
+ self.hits += 1
+ self.hit_streak += 1
+ self.kf.update(convert_bbox_to_z(bbox))
+ else:
+ self.kf.update(bbox)
+
+ def predict(self):
+ """
+ Advances the state vector and returns the predicted bounding box estimate.
+ """
+ if((self.kf.x[6]+self.kf.x[2]) <= 0):
+ self.kf.x[6] *= 0.0
+
+ self.kf.predict()
+ self.age += 1
+ if(self.time_since_update > 0):
+ self.hit_streak = 0
+ self.time_since_update += 1
+ self.history.append(convert_x_to_bbox(self.kf.x))
+ return self.history[-1]
+
+ def get_state(self):
+ """
+ Returns the current bounding box estimate.
+ """
+ return convert_x_to_bbox(self.kf.x)
+
+class OCSort(object):
+ def __init__(self, det_thresh=0.25, max_age=30, min_hits=3,
+ iou_threshold=0.3, delta_t=3, asso_func="iou", inertia=0.2, use_byte=False):
+ """
+ Sets key parameters for SORT
+ """
+ self.max_age = max_age
+ self.min_hits = min_hits
+ self.iou_threshold = iou_threshold
+ self.trackers = []
+ self.frame_count = 0
+ self.det_thresh = det_thresh
+ self.delta_t = delta_t
+ self.asso_func = iou_batch
+ self.inertia = inertia
+ self.use_byte = use_byte
+ KalmanBoxTracker.count = 0
+
+ def update(self, output_results, img_info, img_size, det_thresh=0.25):
+ """
+ Params:
+ dets - a numpy array of detections in the format [[x1,y1,x2,y2,score],[x1,y1,x2,y2,score],...]
+ Requires: this method must be called once for each frame even with empty detections (use np.empty((0, 5)) for frames without detections).
+ Returns the a similar array, where the last column is the object ID.
+ NOTE: The number of objects returned may differ from the number of detections provided.
+ """
+ if output_results is None:
+ return np.empty((0, 5))
+
+ self.frame_count += 1
+ # post_process detections
+
+ scores = output_results[:, 4]
+ bboxes = output_results[:, :4] # x1y1x2y2
+ class_ids = output_results[:, 5].astype(int)
+
+ img_h, img_w = img_info[0], img_info[1]
+ scale = min(img_size[0] / float(img_h), img_size[1] / float(img_w))
+ bboxes /= scale
+ dets = np.concatenate((bboxes, np.expand_dims(scores, axis=-1)), axis=1)
+ inds_low = scores > 0.1
+ inds_high = scores < det_thresh
+ inds_second = np.logical_and(inds_low, inds_high) # self.det_thresh > score > 0.1, for second matching
+ dets_second = dets[inds_second] # detections for second matching
+ class_ids_second = class_ids[inds_second]
+ scores_second = scores[inds_second]
+ remain_inds = scores > det_thresh
+ dets = dets[remain_inds]
+ class_ids = class_ids[remain_inds]
+ scores = scores[remain_inds]
+
+ # get predicted locations from existing trackers.
+ trks = np.zeros((len(self.trackers), 5))
+ to_del = []
+ ret = []
+ for t, trk in enumerate(trks):
+ pos = self.trackers[t].predict()[0]
+ trk[:] = [pos[0], pos[1], pos[2], pos[3], 0]
+ if np.any(np.isnan(pos)):
+ to_del.append(t)
+ trks = np.ma.compress_rows(np.ma.masked_invalid(trks))
+ for t in reversed(to_del):
+ self.trackers.pop(t)
+
+ velocities = np.array(
+ [trk.velocity if trk.velocity is not None else np.array((0, 0)) for trk in self.trackers])
+ last_boxes = np.array([trk.last_observation for trk in self.trackers])
+ k_observations = np.array(
+ [k_previous_obs(trk.observations, trk.age, self.delta_t) for trk in self.trackers])
+
+ """
+ First round of association
+ """
+ matched, unmatched_dets, unmatched_trks = associate(
+ dets, trks, self.iou_threshold, velocities, k_observations, self.inertia)
+ for m in matched:
+ self.trackers[m[1]].update(dets[m[0], :], scores[m[0]], class_ids[m[0]])
+
+ """
+ Second round of associaton by OCR
+ """
+ # BYTE association
+ if self.use_byte and len(dets_second) > 0 and unmatched_trks.shape[0] > 0:
+ u_trks = trks[unmatched_trks]
+ iou_left = self.asso_func(dets_second, u_trks) # iou between low score detections and unmatched tracks
+ iou_left = np.array(iou_left)
+ if iou_left.max() > self.iou_threshold:
+ """
+ NOTE: by using a lower threshold, e.g., self.iou_threshold - 0.1, you may
+ get a higher performance especially on MOT17/MOT20 datasets. But we keep it
+ uniform here for simplicity
+ """
+ matched_indices = linear_assignment(-iou_left)
+ to_remove_trk_indices = []
+ for m in matched_indices:
+ det_ind, trk_ind = m[0], unmatched_trks[m[1]]
+ if iou_left[m[0], m[1]] < self.iou_threshold:
+ continue
+ self.trackers[trk_ind].update(dets_second[det_ind, :], scores_second[det_ind], class_ids_second[det_ind])
+ to_remove_trk_indices.append(trk_ind)
+ unmatched_trks = np.setdiff1d(unmatched_trks, np.array(to_remove_trk_indices))
+
+ if unmatched_dets.shape[0] > 0 and unmatched_trks.shape[0] > 0:
+ left_dets = dets[unmatched_dets]
+ left_trks = last_boxes[unmatched_trks]
+ iou_left = self.asso_func(left_dets, left_trks)
+ iou_left = np.array(iou_left)
+ if iou_left.max() > self.iou_threshold:
+ """
+ NOTE: by using a lower threshold, e.g., self.iou_threshold - 0.1, you may
+ get a higher performance especially on MOT17/MOT20 datasets. But we keep it
+ uniform here for simplicity
+ """
+ rematched_indices = linear_assignment(-iou_left)
+ to_remove_det_indices = []
+ to_remove_trk_indices = []
+ for m in rematched_indices:
+ det_ind, trk_ind = unmatched_dets[m[0]], unmatched_trks[m[1]]
+ if iou_left[m[0], m[1]] < self.iou_threshold:
+ continue
+ self.trackers[trk_ind].update(dets[det_ind, :], scores[det_ind], class_ids[det_ind])
+ to_remove_det_indices.append(det_ind)
+ to_remove_trk_indices.append(trk_ind)
+ unmatched_dets = np.setdiff1d(unmatched_dets, np.array(to_remove_det_indices))
+ unmatched_trks = np.setdiff1d(unmatched_trks, np.array(to_remove_trk_indices))
+
+ for m in unmatched_trks:
+ self.trackers[m].update(None)
+
+ # create and initialise new trackers for unmatched detections
+ for i in unmatched_dets:
+ trk = KalmanBoxTracker(dets[i, :], delta_t=self.delta_t)
+ trk.class_id = class_ids[i]
+ trk.score = scores[i]
+ trk.occurrences[class_ids[i]] += 1
+ self.trackers.append(trk)
+ i = len(self.trackers)
+ for trk in reversed(self.trackers):
+ if trk.last_observation.sum() < 0:
+ d = trk.get_state()[0]
+ else:
+ """
+ this is optional to use the recent observation or the kalman filter prediction,
+ we didn't notice significant difference here
+ """
+ d = trk.last_observation[:4]
+ if (trk.time_since_update < 1) and (trk.hit_streak >= self.min_hits or self.frame_count <= self.min_hits):
+ # +1 as MOT benchmark requires positive
+ ret.append(np.concatenate((d, [trk.id+1, trk.age, trk.class_id, trk.score])).reshape(1, -1))
+ i -= 1
+ # remove dead tracklet
+ if(trk.time_since_update > self.max_age):
+ self.trackers.pop(i)
+ # tlx tly w h, track_id, age, class_id, score
+ out = []
+ for x in ret:
+ out.append(STrack(tlwh=[x[0][0], x[0][1], (x[0][2] - x[0][0]), (x[0][3] - x[0][1])], score=x[0][7], class_id=x[0][6], track_id=x[0][4], age=x[0][5]))
+ return out
+
+
diff --git a/requirements.txt b/requirements.txt
index 4eec519..eeab2af 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,4 +1,3 @@
git+https://github.com/tinygrad/tinygrad.git@42f6cf3a906dd17bc47c63894b7b5ca8f9c99a66
numpy==2.0.0
-opencv-python-headless==4.10.0.84
-scipy==1.14.1
\ No newline at end of file
+opencv-python-headless==4.10.0.84
\ No newline at end of file
diff --git a/test/test.py b/test/test.py
deleted file mode 100644
index 268a9c4..0000000
--- a/test/test.py
+++ /dev/null
@@ -1,71 +0,0 @@
-import numpy as np
-from scipy.optimize import linear_sum_assignment as scipy_linear_sum_assignment
-from yolox.tracker.matching import linear_sum_assignment2
-import time
-from clearcam import YOLOv9, do_inf, preprocess, SIZES, scale_boxes, draw_predictions
-from tinygrad.nn.state import safe_load, load_state_dict
-from tinygrad import Tensor
-import cv2
-from collections import defaultdict
-from tinygrad.helpers import Context, fetch
-
-def test_linear_sum_assigment():
- for i in range(1,300):
- input = np.random.rand(i,i)
- a, b = linear_sum_assignment2(input)
- c, d = scipy_linear_sum_assignment(input)
- np.testing.assert_allclose(a,c)
- np.testing.assert_allclose(b,d)
-
-def test_linear_sum_assigment_speed():
- total_scipy = 0
- total = 0
- for i in range(1,300):
- input = np.random.rand(i,i)
- st = time.perf_counter()
- linear_sum_assignment2(input)
- t = time.perf_counter() - st
- total += t
- st = time.perf_counter()
- scipy_linear_sum_assignment(input)
- t_scipy = time.perf_counter() - st
- total_scipy += t_scipy
- print("time vs scipy =", f"{(total / total_scipy) * 100:.1f}%")
-
-
-import pickle
-from yolox.tracker.byte_tracker import BYTETracker
-from yolox.tracker.byte_tracker2 import BYTETracker2
-
-def test_bytetracker():
- class Args:
- def __init__(self):
- self.track_buffer = 60
- self.mot20 = False
- self.match_thresh = 0.9
- tracker = BYTETracker(Args())
- tracker2 = BYTETracker2(Args())
-
- inputs = pickle.load(open("test/tracker_inputs.pkl", "rb"))
- total_time = 0
- total_time2 = 0
- for i in range(len(inputs)):
- st = time.perf_counter()
- output = tracker.update(inputs[i], [1280,1280], [1280,1280], threshold=0.5)
- total_time += (time.perf_counter() - st)
- st = time.perf_counter()
- output2 = tracker2.update(inputs[i], [1280,1280], [1280,1280], threshold=0.5)
- total_time2 += (time.perf_counter() - st)
- assert len(output2) == len(output)
- for j in range(len(output)):
- np.testing.assert_allclose(output[j]._tlwh, output2[j]._tlwh)
- np.testing.assert_allclose(output[j].score, output2[j].score)
- np.testing.assert_allclose(output[j].is_activated, output2[j].is_activated)
- np.testing.assert_allclose(output[j].mean, output2[j].mean)
- np.testing.assert_allclose(output[j].covariance, output2[j].covariance)
- np.testing.assert_allclose(output[j].class_id, output2[j].class_id)
- assert total_time2 <= (total_time * 1.0), "slower"
-
-test_bytetracker()
-test_linear_sum_assigment()
-test_linear_sum_assigment_speed()
\ No newline at end of file
diff --git a/test/test_mot.py b/test/test_mot.py
index 2001d9d..8d045b5 100644
--- a/test/test_mot.py
+++ b/test/test_mot.py
@@ -35,58 +35,50 @@ def letterbox(im, new_shape=(1280, 1280), color=(114, 114, 114), auto=True, scal
return im, ratio, (dw, dh)
if __name__ == "__main__":
- from yolox.tracker.byte_tracker import BYTETracker
- from yolox.ocsort_tracker import ocsort
- class Args:
- def __init__(self):
- self.track_buffer = 60 # frames, was 30
- self.mot20 = False
- self.match_thresh = 0.9
- tracker = BYTETracker(Args())
- ocs_tracker = ocsort.OCSort(det_thresh=0.25)
+ from ocsort_tracker import ocsort
+ ocs_tracker = ocsort.OCSort()
size = "t"
model = YOLOv9(*SIZES[size]) if size in SIZES else YOLOv9()
state_dict = safe_load(fetch(f'https://huggingface.co/roryclear/yolov9/resolve/main/yolov9-{size}.safetensors'))
load_state_dict(model, state_dict)
- cap = cv2.VideoCapture("test/videos/MOT16-03.mp4")
- w, h = int(cap.get(3)), int(cap.get(4))
- out = cv2.VideoWriter(f"outputs/out_{size}.mp4", cv2.VideoWriter_fourcc(*"mp4v"), 30, (w, h))
class_labels = fetch('https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names').read_text().split("\n")
Path('./outputs').mkdir(parents=True, exist_ok=True)
- i = 0
- ppl = set()
- while True:
- ret, im0 = cap.read()
- if not ret: break
- im = letterbox(im0, new_shape=(960, 960), stride=32, auto=True)[0]
- im = im.transpose((2, 0, 1))[::-1]
- im = np.ascontiguousarray(im)
- im = Tensor(im).cast(dtypes.float32)
- im /= 255
- if len(im.shape) == 3:
- im = im[None]
- pred = do_inf(im, model).numpy()[0]
- online_targets = tracker.update(pred, [960,960], [960,960], 0.25)
- oc_online_targets = ocs_tracker.update(pred, [960,960], [960,960])
- preds = []
- for x in oc_online_targets:
- if x[6] == 0 and x[4] not in ppl: ppl.add(x[4])
- preds.append(np.array([x[0], x[1], x[2], x[3], x[4], x[6]]))
+ trackers = [ocs_tracker]
+ excepted_ppl = [288]
+ for j,t in enumerate(trackers):
- #preds = []
- #for x in online_targets:
- # if x.class_id == 0 and x.track_id not in ppl: ppl.add(x.track_id)
- # preds.append(np.array([x.tlwh[0],x.tlwh[1],(x.tlwh[0]+x.tlwh[2]),(x.tlwh[1]+x.tlwh[3]),x.score,x.class_id,x.track_id]))
+ cap = cv2.VideoCapture("test/videos/MOT16-03.mp4")
+ w, h = int(cap.get(3)), int(cap.get(4))
+ out = cv2.VideoWriter(f"outputs/out_{size}_{j}.mp4", cv2.VideoWriter_fourcc(*"mp4v"), 30, (w, h))
- print(oc_online_targets,"\n\n",preds)
- print("\n\n\n\n\n\n\n\n\n")
- print("ppl =",len(ppl))
- #pred = pred[pred[:, 4] >= 0.25]
- _, buffer = cv2.imencode(".jpg", im0)
- out.write(draw_bounding_boxes(buffer, preds, class_labels))
- i+=1
- print("frame",i)
- cap.release()
- out.release()
\ No newline at end of file
+ i = 0
+ ppl = set()
+ while True:
+ ret, im0 = cap.read()
+ if not ret: break
+ im = letterbox(im0, new_shape=(960, 960), stride=32, auto=True)[0]
+ im = im.transpose((2, 0, 1))[::-1]
+ im = np.ascontiguousarray(im)
+ im = Tensor(im).cast(dtypes.float32)
+ im /= 255
+ if len(im.shape) == 3:
+ im = im[None]
+ pred = do_inf(im, model).numpy()[0]
+ online_targets = t.update(pred, [960,960], [960,960], 0.25)
+
+ preds = []
+ for x in online_targets:
+ if x.class_id == 0 and x.track_id not in ppl: ppl.add(x.track_id)
+ preds.append(np.array([x.tlwh[0],x.tlwh[1],(x.tlwh[0]+x.tlwh[2]),(x.tlwh[1]+x.tlwh[3]),x.track_id,x.class_id,x.score]))
+ #tlx tly w h, track_id, age, class_id, score
+
+ print("ppl =",len(ppl))
+ _, buffer = cv2.imencode(".jpg", im0)
+ out.write(draw_bounding_boxes(buffer, preds, class_labels))
+ i+=1
+ print("frame",i)
+ cap.release()
+ out.release()
+ assert len(ppl) == excepted_ppl[j]
\ No newline at end of file
diff --git a/utils/__pycache__/clip_tokenizer.cpython-311.pyc b/utils/__pycache__/clip_tokenizer.cpython-311.pyc
deleted file mode 100644
index 85203b6..0000000
Binary files a/utils/__pycache__/clip_tokenizer.cpython-311.pyc and /dev/null differ
diff --git a/yolox/.DS_Store b/yolox/.DS_Store
deleted file mode 100644
index 7e85cb6..0000000
Binary files a/yolox/.DS_Store and /dev/null differ
diff --git a/yolox/tracker/__pycache__/basetrack.cpython-310.pyc b/yolox/tracker/__pycache__/basetrack.cpython-310.pyc
deleted file mode 100644
index 234c7ab..0000000
Binary files a/yolox/tracker/__pycache__/basetrack.cpython-310.pyc and /dev/null differ
diff --git a/yolox/tracker/__pycache__/byte_tracker.cpython-310.pyc b/yolox/tracker/__pycache__/byte_tracker.cpython-310.pyc
deleted file mode 100644
index cf1955c..0000000
Binary files a/yolox/tracker/__pycache__/byte_tracker.cpython-310.pyc and /dev/null differ
diff --git a/yolox/tracker/__pycache__/kalman_filter.cpython-310.pyc b/yolox/tracker/__pycache__/kalman_filter.cpython-310.pyc
deleted file mode 100644
index a331554..0000000
Binary files a/yolox/tracker/__pycache__/kalman_filter.cpython-310.pyc and /dev/null differ
diff --git a/yolox/tracker/__pycache__/matching.cpython-310.pyc b/yolox/tracker/__pycache__/matching.cpython-310.pyc
deleted file mode 100644
index b5ec181..0000000
Binary files a/yolox/tracker/__pycache__/matching.cpython-310.pyc and /dev/null differ
diff --git a/yolox/tracker/basetrack.py b/yolox/tracker/basetrack.py
deleted file mode 100644
index 4fe2233..0000000
--- a/yolox/tracker/basetrack.py
+++ /dev/null
@@ -1,52 +0,0 @@
-import numpy as np
-from collections import OrderedDict
-
-
-class TrackState(object):
- New = 0
- Tracked = 1
- Lost = 2
- Removed = 3
-
-
-class BaseTrack(object):
- _count = 0
-
- track_id = 0
- is_activated = False
- state = TrackState.New
-
- history = OrderedDict()
- features = []
- curr_feature = None
- score = 0
- start_frame = 0
- frame_id = 0
- time_since_update = 0
-
- # multi-camera
- location = (np.inf, np.inf)
-
- @property
- def end_frame(self):
- return self.frame_id
-
- @staticmethod
- def next_id():
- BaseTrack._count += 1
- return BaseTrack._count
-
- def activate(self, *args):
- raise NotImplementedError
-
- def predict(self):
- raise NotImplementedError
-
- def update(self, *args, **kwargs):
- raise NotImplementedError
-
- def mark_lost(self):
- self.state = TrackState.Lost
-
- def mark_removed(self):
- self.state = TrackState.Removed
diff --git a/yolox/tracker/byte_tracker.py b/yolox/tracker/byte_tracker.py
deleted file mode 100644
index 88a107d..0000000
--- a/yolox/tracker/byte_tracker.py
+++ /dev/null
@@ -1,325 +0,0 @@
-import numpy as np
-
-from .kalman_filter import KalmanFilter
-from yolox.tracker import matching
-from .basetrack import BaseTrack, TrackState
-from collections import defaultdict
-
-class STrack(BaseTrack):
- shared_kalman = KalmanFilter()
- def __init__(self, tlwh, score, class_id):
- self.occurrences = defaultdict(float)
- # wait activate
- self._tlwh = np.asarray(tlwh, dtype=np.float64)
- self.kalman_filter = None
- self.mean, self.covariance = None, None
- self.is_activated = False
-
- self.score = score
- self.class_id = class_id
- self.tracklet_len = 0
-
- def predict(self):
- mean_state = self.mean.copy()
- if self.state != TrackState.Tracked:
- mean_state[7] = 0
- self.mean, self.covariance = self.kalman_filter.predict(mean_state, self.covariance)
-
- @staticmethod
- def multi_predict(stracks):
- if len(stracks) > 0:
- multi_mean = np.asarray([st.mean.copy() for st in stracks])
- multi_covariance = np.asarray([st.covariance for st in stracks])
- for i, st in enumerate(stracks):
- if st.state != TrackState.Tracked:
- multi_mean[i][7] = 0
- multi_mean, multi_covariance = STrack.shared_kalman.multi_predict(multi_mean, multi_covariance)
- for i, (mean, cov) in enumerate(zip(multi_mean, multi_covariance)):
- stracks[i].mean = mean
- stracks[i].covariance = cov
-
- def activate(self, kalman_filter, frame_id):
- """Start a new tracklet"""
- self.kalman_filter = kalman_filter
- self.track_id = self.next_id()
- self.mean, self.covariance = self.kalman_filter.initiate(self.tlwh_to_xyah(self._tlwh))
-
- self.tracklet_len = 0
- self.state = TrackState.Tracked
- if frame_id == 1:
- self.is_activated = True
- # self.is_activated = True
- self.frame_id = frame_id
- self.start_frame = frame_id
-
- def re_activate(self, new_track, frame_id, new_id=False):
- self.mean, self.covariance = self.kalman_filter.update(
- self.mean, self.covariance, self.tlwh_to_xyah(new_track.tlwh)
- )
- self.tracklet_len = 0
- self.state = TrackState.Tracked
- self.is_activated = True
- self.frame_id = frame_id
- if new_id:
- self.track_id = self.next_id()
- self.score = new_track.score
-
- def update(self, new_track, frame_id):
- """
- Update a matched track
- :type new_track: STrack
- :type frame_id: int
- :type update_feature: bool
- :return:
- """
- self.frame_id = frame_id
- self.tracklet_len += 1
-
- new_tlwh = new_track.tlwh
- self.mean, self.covariance = self.kalman_filter.update(
- self.mean, self.covariance, self.tlwh_to_xyah(new_tlwh))
- self.state = TrackState.Tracked
- self.is_activated = True
-
- self.score = new_track.score
- self.occurrences[new_track.class_id] += new_track.score
- self.class_id = max(self.occurrences, key=self.occurrences.get) # use highest combined score
-
- @property
- # @jit(nopython=True)
- def tlwh(self):
- """Get current position in bounding box format `(top left x, top left y,
- width, height)`.
- """
- if self.mean is None:
- return self._tlwh.copy()
- ret = self.mean[:4].copy()
- ret[2] *= ret[3]
- ret[:2] -= ret[2:] / 2
- return ret
-
- @property
- # @jit(nopython=True)
- def tlbr(self):
- """Convert bounding box to format `(min x, min y, max x, max y)`, i.e.,
- `(top left, bottom right)`.
- """
- ret = self.tlwh.copy()
- ret[2:] += ret[:2]
- return ret
-
- @staticmethod
- # @jit(nopython=True)
- def tlwh_to_xyah(tlwh):
- """Convert bounding box to format `(center x, center y, aspect ratio,
- height)`, where the aspect ratio is `width / height`.
- """
- ret = np.asarray(tlwh).copy()
- ret[:2] += ret[2:] / 2
- ret[2] /= ret[3]
- return ret
-
- def to_xyah(self):
- return self.tlwh_to_xyah(self.tlwh)
-
- @staticmethod
- # @jit(nopython=True)
- def tlbr_to_tlwh(tlbr):
- ret = np.asarray(tlbr).copy()
- ret[2:] -= ret[:2]
- return ret
-
- @staticmethod
- # @jit(nopython=True)
- def tlwh_to_tlbr(tlwh):
- ret = np.asarray(tlwh).copy()
- ret[2:] += ret[:2]
- return ret
-
- def __repr__(self):
- return 'OT_{}_({}-{})'.format(self.track_id, self.start_frame, self.end_frame)
-
-
-class BYTETracker(object):
- def __init__(self, args, frame_rate=30):
- self.tracked_stracks = [] # type: list[STrack]
- self.lost_stracks = [] # type: list[STrack]
- self.removed_stracks = [] # type: list[STrack]
-
- self.frame_id = 0
- self.args = args
- self.buffer_size = int(frame_rate / 30.0 * args.track_buffer)
- self.max_time_lost = self.buffer_size
- self.kalman_filter = KalmanFilter()
-
- def update(self, output_results, img_info, img_size, threshold=0.5):
- self.frame_id += 1
- activated_starcks = []
- refind_stracks = []
- lost_stracks = []
- removed_stracks = []
-
- classes = output_results[:, 5]
- scores = output_results[:, 4]
- bboxes = output_results[:, :4]
-
- img_h, img_w = img_info[0], img_info[1]
- scale = min(img_size[0] / float(img_h), img_size[1] / float(img_w))
- bboxes /= scale
-
- remain_inds = scores > threshold # prev track thresh
- inds_low = scores > 0.1
- inds_high = scores < threshold # prev track thresh
-
- inds_second = np.logical_and(inds_low, inds_high)
- dets_second = bboxes[inds_second]
- dets = bboxes[remain_inds]
- scores_keep = scores[remain_inds]
- scores_second = scores[inds_second]
- classes_keep = classes[remain_inds]
- classes_second = classes[inds_second]
-
- if len(dets) > 0:
- '''Detections'''
- detections = [STrack(STrack.tlbr_to_tlwh(tlbr), s, c) for
- (tlbr, s, c) in zip(dets, scores_keep, classes_keep)]
- else:
- detections = []
-
- ''' Add newly detected tracklets to tracked_stracks'''
- unconfirmed = []
- tracked_stracks = [] # type: list[STrack]
- for track in self.tracked_stracks:
- if not track.is_activated:
- unconfirmed.append(track)
- else:
- tracked_stracks.append(track)
-
- ''' Step 2: First association, with high score detection boxes'''
- strack_pool = joint_stracks(tracked_stracks, self.lost_stracks)
- # Predict the current location with KF
- STrack.multi_predict(strack_pool)
- dists = matching.iou_distance(strack_pool, detections)
- if not self.args.mot20:
- dists = matching.fuse_score(dists, detections)
- matches, u_track, u_detection = matching.linear_assignment(dists, thresh=self.args.match_thresh)
-
- for itracked, idet in matches:
- track = strack_pool[itracked]
- det = detections[idet]
- if track.state == TrackState.Tracked:
- track.update(detections[idet], self.frame_id)
- activated_starcks.append(track)
- else:
- track.re_activate(det, self.frame_id, new_id=False)
- refind_stracks.append(track)
-
- ''' Step 3: Second association, with low score detection boxes'''
- # association the untrack to the low score detections
- if len(dets_second) > 0:
- '''Detections'''
- detections_second = [STrack(STrack.tlbr_to_tlwh(tlbr), s, c) for
- (tlbr, s, c) in zip(dets_second, scores_second, classes_second)]
- else:
- detections_second = []
- r_tracked_stracks = [strack_pool[i] for i in u_track if strack_pool[i].state == TrackState.Tracked]
- dists = matching.iou_distance(r_tracked_stracks, detections_second)
- matches, u_track, u_detection_second = matching.linear_assignment(dists, thresh=threshold) # prev 0.5
- for itracked, idet in matches:
- track = r_tracked_stracks[itracked]
- det = detections_second[idet]
- if track.state == TrackState.Tracked:
- track.update(det, self.frame_id)
- activated_starcks.append(track)
- else:
- track.re_activate(det, self.frame_id, new_id=False)
- refind_stracks.append(track)
-
- for it in u_track:
- track = r_tracked_stracks[it]
- if not track.state == TrackState.Lost:
- track.mark_lost()
- lost_stracks.append(track)
-
- '''Deal with unconfirmed tracks, usually tracks with only one beginning frame'''
- detections = [detections[i] for i in u_detection]
- dists = matching.iou_distance(unconfirmed, detections)
- if not self.args.mot20:
- dists = matching.fuse_score(dists, detections)
- matches, u_unconfirmed, u_detection = matching.linear_assignment(dists, thresh=0.7)
- for itracked, idet in matches:
- unconfirmed[itracked].update(detections[idet], self.frame_id)
- activated_starcks.append(unconfirmed[itracked])
- for it in u_unconfirmed:
- track = unconfirmed[it]
- track.mark_removed()
- removed_stracks.append(track)
-
- """ Step 4: Init new stracks"""
- for inew in u_detection:
- track = detections[inew]
- if track.score < threshold:
- continue
- track.activate(self.kalman_filter, self.frame_id)
- activated_starcks.append(track)
- """ Step 5: Update state"""
- for track in self.lost_stracks:
- if self.frame_id - track.end_frame > self.max_time_lost:
- track.mark_removed()
- removed_stracks.append(track)
-
- # print('Ramained match {} s'.format(t4-t3))
-
- self.tracked_stracks = [t for t in self.tracked_stracks if t.state == TrackState.Tracked]
- self.tracked_stracks = joint_stracks(self.tracked_stracks, activated_starcks)
- self.tracked_stracks = joint_stracks(self.tracked_stracks, refind_stracks)
- self.lost_stracks = sub_stracks(self.lost_stracks, self.tracked_stracks)
- self.lost_stracks.extend(lost_stracks)
- self.lost_stracks = sub_stracks(self.lost_stracks, self.removed_stracks)
- self.removed_stracks.extend(removed_stracks)
- self.tracked_stracks, self.lost_stracks = remove_duplicate_stracks(self.tracked_stracks, self.lost_stracks)
- # get scores of lost tracks
- output_stracks = [track for track in self.tracked_stracks if track.is_activated]
-
- return output_stracks
-
-
-def joint_stracks(tlista, tlistb):
- exists = {}
- res = []
- for t in tlista:
- exists[t.track_id] = 1
- res.append(t)
- for t in tlistb:
- tid = t.track_id
- if not exists.get(tid, 0):
- exists[tid] = 1
- res.append(t)
- return res
-
-
-def sub_stracks(tlista, tlistb):
- stracks = {}
- for t in tlista:
- stracks[t.track_id] = t
- for t in tlistb:
- tid = t.track_id
- if stracks.get(tid, 0):
- del stracks[tid]
- return list(stracks.values())
-
-
-def remove_duplicate_stracks(stracksa, stracksb):
- pdist = matching.iou_distance(stracksa, stracksb)
- pairs = np.where(pdist < 0.15)
- dupa, dupb = list(), list()
- for p, q in zip(*pairs):
- timep = stracksa[p].frame_id - stracksa[p].start_frame
- timeq = stracksb[q].frame_id - stracksb[q].start_frame
- if timep > timeq:
- dupb.append(q)
- else:
- dupa.append(p)
- resa = [t for i, t in enumerate(stracksa) if not i in dupa]
- resb = [t for i, t in enumerate(stracksb) if not i in dupb]
- return resa, resb
\ No newline at end of file
diff --git a/yolox/tracker/byte_tracker2.py b/yolox/tracker/byte_tracker2.py
deleted file mode 100644
index 19bbf9e..0000000
--- a/yolox/tracker/byte_tracker2.py
+++ /dev/null
@@ -1,332 +0,0 @@
-import numpy as np
-
-from .kalman_filter import KalmanFilter
-from yolox.tracker import matching
-from .basetrack import BaseTrack, TrackState
-from collections import defaultdict
-
-class STrack(BaseTrack):
- shared_kalman = KalmanFilter()
- def __init__(self, tlwh, score, class_id):
- self.occurrences = defaultdict(float)
- # wait activate
- self._tlwh = np.asarray(tlwh, dtype=np.float64)
- self.kalman_filter = None
- self.mean, self.covariance = None, None
- self.is_activated = False
-
- self.score = score
- self.class_id = class_id
- self.tracklet_len = 0
-
- def predict(self):
- mean_state = self.mean.copy()
- if self.state != TrackState.Tracked:
- mean_state[7] = 0
- self.mean, self.covariance = self.kalman_filter.predict(mean_state, self.covariance)
-
- @staticmethod
- def multi_predict(stracks):
- if len(stracks) > 0:
- multi_mean = np.asarray([st.mean.copy() for st in stracks])
- multi_covariance = np.asarray([st.covariance for st in stracks])
- for i, st in enumerate(stracks):
- if st.state != TrackState.Tracked:
- multi_mean[i][7] = 0
- multi_mean, multi_covariance = STrack.shared_kalman.multi_predict(multi_mean, multi_covariance)
- for i, (mean, cov) in enumerate(zip(multi_mean, multi_covariance)):
- stracks[i].mean = mean
- stracks[i].covariance = cov
-
- def activate(self, kalman_filter, frame_id):
- """Start a new tracklet"""
- self.kalman_filter = kalman_filter
- self.track_id = self.next_id()
- self.mean, self.covariance = self.kalman_filter.initiate(self.tlwh_to_xyah(self._tlwh))
-
- self.tracklet_len = 0
- self.state = TrackState.Tracked
- if frame_id == 1:
- self.is_activated = True
- # self.is_activated = True
- self.frame_id = frame_id
- self.start_frame = frame_id
-
- def re_activate(self, new_track, frame_id, new_id=False):
- self.mean, self.covariance = self.kalman_filter.update(
- self.mean, self.covariance, self.tlwh_to_xyah(new_track.tlwh)
- )
- self.tracklet_len = 0
- self.state = TrackState.Tracked
- self.is_activated = True
- self.frame_id = frame_id
- if new_id:
- self.track_id = self.next_id()
- self.score = new_track.score
-
- def update(self, new_track, frame_id):
- """
- Update a matched track
- :type new_track: STrack
- :type frame_id: int
- :type update_feature: bool
- :return:
- """
- self.frame_id = frame_id
- self.tracklet_len += 1
-
- new_tlwh = new_track.tlwh
- self.mean, self.covariance = self.kalman_filter.update(
- self.mean, self.covariance, self.tlwh_to_xyah(new_tlwh))
- self.state = TrackState.Tracked
- self.is_activated = True
-
- self.score = new_track.score
- self.occurrences[new_track.class_id] += new_track.score
- self.class_id = max(self.occurrences, key=self.occurrences.get) # use highest combined score
-
- @property
- # @jit(nopython=True)
- def tlwh(self):
- """Get current position in bounding box format `(top left x, top left y,
- width, height)`.
- """
- if self.mean is None:
- return self._tlwh.copy()
- ret = self.mean[:4].copy()
- ret[2] *= ret[3]
- ret[:2] -= ret[2:] / 2
- return ret
-
- @property
- # @jit(nopython=True)
- def tlbr(self):
- """Convert bounding box to format `(min x, min y, max x, max y)`, i.e.,
- `(top left, bottom right)`.
- """
- ret = self.tlwh.copy()
- ret[2:] += ret[:2]
- return ret
-
- @staticmethod
- # @jit(nopython=True)
- def tlwh_to_xyah(tlwh):
- """Convert bounding box to format `(center x, center y, aspect ratio,
- height)`, where the aspect ratio is `width / height`.
- """
- ret = np.asarray(tlwh).copy()
- ret[:2] += ret[2:] / 2
- ret[2] /= ret[3]
- return ret
-
- def to_xyah(self):
- return self.tlwh_to_xyah(self.tlwh)
-
- @staticmethod
- # @jit(nopython=True)
- def tlbr_to_tlwh(tlbr):
- ret = np.asarray(tlbr).copy()
- ret[2:] -= ret[:2]
- return ret
-
- @staticmethod
- # @jit(nopython=True)
- def tlwh_to_tlbr(tlwh):
- ret = np.asarray(tlwh).copy()
- ret[2:] += ret[:2]
- return ret
-
- def __repr__(self):
- return 'OT_{}_({}-{})'.format(self.track_id, self.start_frame, self.end_frame)
-
-
-class BYTETracker2(object):
- def __init__(self, args, frame_rate=30):
- self.tracked_stracks = [] # type: list[STrack]
- self.lost_stracks = [] # type: list[STrack]
- self.removed_stracks = [] # type: list[STrack]
-
- self.frame_id = 0
- self.args = args
- self.buffer_size = int(frame_rate / 30.0 * args.track_buffer)
- self.max_time_lost = self.buffer_size
- self.kalman_filter = KalmanFilter()
-
- def update(self, output_results, img_info, img_size, threshold=0.5):
- self.frame_id += 1
- activated_starcks = []
- refind_stracks = []
- lost_stracks = []
- removed_stracks = []
-
- classes = output_results[:, 5]
- scores = output_results[:, 4]
- bboxes = output_results[:, :4]
-
- img_h, img_w = img_info[0], img_info[1]
- scale = min(img_size[0] / float(img_h), img_size[1] / float(img_w))
- bboxes /= scale
-
- remain_inds = scores > threshold # prev track thresh
- inds_low = scores > 0.1
- inds_high = scores < threshold # prev track thresh
-
- inds_second = np.logical_and(inds_low, inds_high)
- dets_second = bboxes[inds_second]
- dets = bboxes[remain_inds]
- scores_keep = scores[remain_inds]
- scores_second = scores[inds_second]
- classes_keep = classes[remain_inds]
- classes_second = classes[inds_second]
-
- if len(dets) > 0:
- '''Detections'''
- detections = [STrack(STrack.tlbr_to_tlwh(tlbr), s, c) for
- (tlbr, s, c) in zip(dets, scores_keep, classes_keep)]
- else:
- detections = []
-
- ''' Add newly detected tracklets to tracked_stracks'''
- unconfirmed = []
- tracked_stracks = [] # type: list[STrack]
- for track in self.tracked_stracks:
- if not track.is_activated:
- unconfirmed.append(track)
- else:
- tracked_stracks.append(track)
-
- ''' Step 2: First association, with high score detection boxes'''
- strack_pool = joint_stracks(tracked_stracks, self.lost_stracks)
- # Predict the current location with KF
- STrack.multi_predict(strack_pool)
-
- atlbrs = [track.tlbr for track in strack_pool]
- btlbrs = [track.tlbr for track in detections]
-
- ious = np.zeros((len(atlbrs), len(btlbrs)), dtype=np.float64)
- if ious.size == 0:
- dists = 1 - ious
- else:
- atlbrs = np.asarray(atlbrs)
- btlbrs = np.asarray(btlbrs)
- N = atlbrs.shape[0]
- K = btlbrs.shape[0]
-
- boxes_area = ((atlbrs[:, 2] - atlbrs[:, 0] + 1) * (atlbrs[:, 3] - atlbrs[:, 1] + 1)).reshape(N, 1)
- query_areas = ((btlbrs[:, 2] - btlbrs[:, 0] + 1) * (btlbrs[:, 3] - btlbrs[:, 1] + 1)).reshape(1, K)
-
- ixmin = np.maximum(atlbrs[:, 0].reshape(N, 1), btlbrs[:, 0].reshape(1, K))
- iymin = np.maximum(atlbrs[:, 1].reshape(N, 1), btlbrs[:, 1].reshape(1, K))
- ixmax = np.minimum(atlbrs[:, 2].reshape(N, 1), btlbrs[:, 2].reshape(1, K))
- iymax = np.minimum(atlbrs[:, 3].reshape(N, 1), btlbrs[:, 3].reshape(1, K))
- iw = np.maximum(ixmax - ixmin + 1, 0)
- ih = np.maximum(iymax - iymin + 1, 0)
- intersection = iw * ih
- union = boxes_area + query_areas - intersection
- overlaps = np.where((iw > 0) & (ih > 0), intersection / union, np.zeros_like(intersection))
- dists = 1 - overlaps
-
- dists = matching.fuse_score(dists, detections)
- matches, u_track, u_detection = matching.linear_assignment(dists, thresh=self.args.match_thresh)
-
- for itracked, idet in matches:
- track = strack_pool[itracked]
- det = detections[idet]
- if track.state == TrackState.Tracked:
- track.update(detections[idet], self.frame_id)
- activated_starcks.append(track)
- else:
- track.re_activate(det, self.frame_id, new_id=False)
- refind_stracks.append(track)
-
- ''' Step 3: Second association, with low score detection boxes'''
- # association the untrack to the low score detections
- if len(dets_second) > 0:
- '''Detections'''
- detections_second = [STrack(STrack.tlbr_to_tlwh(tlbr), s, c) for
- (tlbr, s, c) in zip(dets_second, scores_second, classes_second)]
- else:
- detections_second = []
- r_tracked_stracks = [strack_pool[i] for i in u_track if strack_pool[i].state == TrackState.Tracked]
- dists = matching.iou_distance(r_tracked_stracks, detections_second)
- matches, u_track, _ = matching.linear_assignment(dists, thresh=threshold) # prev 0.5
- for itracked, idet in matches:
- track = r_tracked_stracks[itracked]
- det = detections_second[idet]
- if track.state == TrackState.Tracked:
- track.update(det, self.frame_id)
- activated_starcks.append(track)
- else:
- track.re_activate(det, self.frame_id, new_id=False)
- refind_stracks.append(track)
-
- for it in u_track:
- track = r_tracked_stracks[it]
- if not track.state == TrackState.Lost:
- track.mark_lost()
- lost_stracks.append(track)
-
- '''Deal with unconfirmed tracks, usually tracks with only one beginning frame'''
- detections = [detections[i] for i in u_detection]
- dists = matching.iou_distance(unconfirmed, detections)
- dists = matching.fuse_score(dists, detections)
- matches, u_unconfirmed, u_detection = matching.linear_assignment(dists, thresh=0.7)
- for itracked, idet in matches:
- unconfirmed[itracked].update(detections[idet], self.frame_id)
- activated_starcks.append(unconfirmed[itracked])
- for it in u_unconfirmed:
- track = unconfirmed[it]
- track.mark_removed()
- removed_stracks.append(track)
-
- """ Step 4: Init new stracks"""
- for inew in u_detection:
- track = detections[inew]
- if track.score < threshold:
- continue
- track.activate(self.kalman_filter, self.frame_id)
- activated_starcks.append(track)
- """ Step 5: Update state"""
- for track in self.lost_stracks:
- if self.frame_id - track.end_frame > self.max_time_lost:
- removed_stracks.append(track)
-
- # print('Ramained match {} s'.format(t4-t3))
-
- self.tracked_stracks = [t for t in self.tracked_stracks if t.state == TrackState.Tracked]
- self.tracked_stracks = joint_stracks(self.tracked_stracks, activated_starcks)
- self.tracked_stracks = joint_stracks(self.tracked_stracks, refind_stracks)
- self.lost_stracks = sub_stracks(self.lost_stracks, self.tracked_stracks)
- self.lost_stracks.extend(lost_stracks)
- self.lost_stracks = sub_stracks(self.lost_stracks, self.removed_stracks)
- self.removed_stracks.extend(removed_stracks)
-
- # get scores of lost tracks
- output_stracks = [track for track in self.tracked_stracks if track.is_activated]
-
- return output_stracks
-
-
-def joint_stracks(tlista, tlistb):
- return list(dict.fromkeys(tlista + tlistb))
-
-
-def sub_stracks(tlista, tlistb):
- tlistb_ids = {t.track_id for t in tlistb}
- return [t for t in tlista if t.track_id not in tlistb_ids]
-
-
-def remove_duplicate_stracks(stracksa, stracksb):
- pdist = matching.iou_distance(stracksa, stracksb)
- pairs = np.where(pdist < 0.15)
- dupa, dupb = list(), list()
- for p, q in zip(*pairs):
- timep = stracksa[p].frame_id - stracksa[p].start_frame
- timeq = stracksb[q].frame_id - stracksb[q].start_frame
- if timep > timeq:
- dupb.append(q)
- else:
- dupa.append(p)
- resa = [t for i, t in enumerate(stracksa) if not i in dupa]
- resb = [t for i, t in enumerate(stracksb) if not i in dupb]
- return resa, resb
\ No newline at end of file
diff --git a/yolox/tracker/kalman_filter.py b/yolox/tracker/kalman_filter.py
deleted file mode 100644
index 930c6b8..0000000
--- a/yolox/tracker/kalman_filter.py
+++ /dev/null
@@ -1,222 +0,0 @@
-# vim: expandtab:ts=4:sw=4
-import numpy as np
-
-"""
-Table for the 0.95 quantile of the chi-square distribution with N degrees of
-freedom (contains values for N=1, ..., 9). Taken from MATLAB/Octave's chi2inv
-function and used as Mahalanobis gating threshold.
-"""
-chi2inv95 = {
- 1: 3.8415,
- 2: 5.9915,
- 3: 7.8147,
- 4: 9.4877,
- 5: 11.070,
- 6: 12.592,
- 7: 14.067,
- 8: 15.507,
- 9: 16.919}
-
-
-class KalmanFilter(object):
- """
- A simple Kalman filter for tracking bounding boxes in image space.
-
- The 8-dimensional state space
-
- x, y, a, h, vx, vy, va, vh
-
- contains the bounding box center position (x, y), aspect ratio a, height h,
- and their respective velocities.
-
- Object motion follows a constant velocity model. The bounding box location
- (x, y, a, h) is taken as direct observation of the state space (linear
- observation model).
-
- """
-
- def __init__(self):
- ndim, dt = 4, 1.
-
- # Create Kalman filter model matrices.
- self._motion_mat = np.eye(2 * ndim, 2 * ndim)
- for i in range(ndim):
- self._motion_mat[i, ndim + i] = dt
- self._update_mat = np.eye(ndim, 2 * ndim)
-
- # Motion and observation uncertainty are chosen relative to the current
- # state estimate. These weights control the amount of uncertainty in
- # the model. This is a bit hacky.
- self._std_weight_position = 1. / 20
- self._std_weight_velocity = 1. / 160
-
- def initiate(self, measurement):
- """Create track from unassociated measurement.
-
- Parameters
- ----------
- measurement : ndarray
- Bounding box coordinates (x, y, a, h) with center position (x, y),
- aspect ratio a, and height h.
-
- Returns
- -------
- (ndarray, ndarray)
- Returns the mean vector (8 dimensional) and covariance matrix (8x8
- dimensional) of the new track. Unobserved velocities are initialized
- to 0 mean.
-
- """
- mean_pos = measurement
- mean_vel = np.zeros_like(mean_pos)
- mean = np.r_[mean_pos, mean_vel]
-
- std = [
- 2 * self._std_weight_position * measurement[3],
- 2 * self._std_weight_position * measurement[3],
- 1e-2,
- 2 * self._std_weight_position * measurement[3],
- 10 * self._std_weight_velocity * measurement[3],
- 10 * self._std_weight_velocity * measurement[3],
- 1e-5,
- 10 * self._std_weight_velocity * measurement[3]]
- covariance = np.diag(np.square(std))
- return mean, covariance
-
- def predict(self, mean, covariance):
- """Run Kalman filter prediction step.
-
- Parameters
- ----------
- mean : ndarray
- The 8 dimensional mean vector of the object state at the previous
- time step.
- covariance : ndarray
- The 8x8 dimensional covariance matrix of the object state at the
- previous time step.
-
- Returns
- -------
- (ndarray, ndarray)
- Returns the mean vector and covariance matrix of the predicted
- state. Unobserved velocities are initialized to 0 mean.
-
- """
- std_pos = [
- self._std_weight_position * mean[3],
- self._std_weight_position * mean[3],
- 1e-2,
- self._std_weight_position * mean[3]]
- std_vel = [
- self._std_weight_velocity * mean[3],
- self._std_weight_velocity * mean[3],
- 1e-5,
- self._std_weight_velocity * mean[3]]
- motion_cov = np.diag(np.square(np.r_[std_pos, std_vel]))
-
- #mean = np.dot(self._motion_mat, mean)
- mean = np.dot(mean, self._motion_mat.T)
- covariance = np.linalg.multi_dot((
- self._motion_mat, covariance, self._motion_mat.T)) + motion_cov
-
- return mean, covariance
-
- def project(self, mean, covariance):
- """Project state distribution to measurement space.
-
- Parameters
- ----------
- mean : ndarray
- The state's mean vector (8 dimensional array).
- covariance : ndarray
- The state's covariance matrix (8x8 dimensional).
-
- Returns
- -------
- (ndarray, ndarray)
- Returns the projected mean and covariance matrix of the given state
- estimate.
-
- """
- std = [
- self._std_weight_position * mean[3],
- self._std_weight_position * mean[3],
- 1e-1,
- self._std_weight_position * mean[3]]
- innovation_cov = np.diag(np.square(std))
-
- mean = np.dot(self._update_mat, mean)
- covariance = np.linalg.multi_dot((
- self._update_mat, covariance, self._update_mat.T))
- return mean, covariance + innovation_cov
-
- def multi_predict(self, mean, covariance):
- """Run Kalman filter prediction step (Vectorized version).
- Parameters
- ----------
- mean : ndarray
- The Nx8 dimensional mean matrix of the object states at the previous
- time step.
- covariance : ndarray
- The Nx8x8 dimensional covariance matrics of the object states at the
- previous time step.
- Returns
- -------
- (ndarray, ndarray)
- Returns the mean vector and covariance matrix of the predicted
- state. Unobserved velocities are initialized to 0 mean.
- """
- std_pos = [
- self._std_weight_position * mean[:, 3],
- self._std_weight_position * mean[:, 3],
- 1e-2 * np.ones_like(mean[:, 3]),
- self._std_weight_position * mean[:, 3]]
- std_vel = [
- self._std_weight_velocity * mean[:, 3],
- self._std_weight_velocity * mean[:, 3],
- 1e-5 * np.ones_like(mean[:, 3]),
- self._std_weight_velocity * mean[:, 3]]
- sqr = np.square(np.r_[std_pos, std_vel]).T
-
- motion_cov = []
- for i in range(len(mean)):
- motion_cov.append(np.diag(sqr[i]))
- motion_cov = np.asarray(motion_cov)
-
- mean = np.dot(mean, self._motion_mat.T)
- left = np.dot(self._motion_mat, covariance).transpose((1, 0, 2))
- covariance = np.dot(left, self._motion_mat.T) + motion_cov
-
- return mean, covariance
-
- def update(self, mean, covariance, measurement):
- """Run Kalman filter correction step.
-
- Parameters
- ----------
- mean : ndarray
- The predicted state's mean vector (8 dimensional).
- covariance : ndarray
- The state's covariance matrix (8x8 dimensional).
- measurement : ndarray
- The 4 dimensional measurement vector (x, y, a, h), where (x, y)
- is the center position, a the aspect ratio, and h the height of the
- bounding box.
-
- Returns
- -------
- (ndarray, ndarray)
- Returns the measurement-corrected state distribution.
-
- """
- projected_mean, projected_cov = self.project(mean, covariance)
-
- chol_factor = np.linalg.cholesky(projected_cov)
- y = np.linalg.solve(chol_factor, np.dot(covariance, self._update_mat.T).T)
- kalman_gain = np.linalg.solve(chol_factor.T, y).T
- innovation = measurement - projected_mean
-
- new_mean = mean + np.dot(innovation, kalman_gain.T)
- new_covariance = covariance - np.linalg.multi_dot((
- kalman_gain, projected_cov, kalman_gain.T))
- return new_mean, new_covariance
\ No newline at end of file
diff --git a/yolox/tracker/matching.py b/yolox/tracker/matching.py
deleted file mode 100644
index 8b02531..0000000
--- a/yolox/tracker/matching.py
+++ /dev/null
@@ -1,189 +0,0 @@
-import numpy as np
-from scipy.optimize import linear_sum_assignment
-
-def linear_assignment(cost_matrix, thresh):
- if cost_matrix.size == 0:
- return np.empty((0, 2), dtype=int), tuple(range(cost_matrix.shape[0])), tuple(range(cost_matrix.shape[1]))
- matches, unmatched_a, unmatched_b = [], [], []
- x, y = lapjv(cost=cost_matrix, cost_limit=thresh)
- for ix, mx in enumerate(x):
- if mx >= 0:
- matches.append([ix, mx])
- unmatched_a = np.where(x < 0)[0]
- unmatched_b = np.where(y < 0)[0]
- matches = np.asarray(matches)
- return matches, unmatched_a, unmatched_b
-
-def lapjv(cost: np.ndarray, cost_limit=np.inf):
- n_rows, n_cols = cost.shape
- if cost_limit < np.inf:
- n = n_rows + n_cols
- cost_extended = np.full((n, n), cost_limit / 2.0)
- cost_extended[n_rows:, n_cols:] = 0
- cost_extended[:n_rows, :n_cols] = cost
- else:
- n = max(n_rows, n_cols)
- cost_extended = np.zeros((n, n))
- cost_extended[:n_rows, :n_cols] = cost
- row_ind, col_ind = linear_sum_assignment(cost_extended)
- x = -np.ones(n_rows, dtype=int)
- y = -np.ones(n_cols, dtype=int)
- for r, c in zip(row_ind, col_ind):
- if r < n_rows and c < n_cols:
- if cost[r, c] < cost_limit:
- x[r] = c
- y[c] = r
- return x, y
-
-# todo try # https://github.com/ShawxingKwok/Kwok-algorithm/blob/main/Python.py
-def linear_sum_assignment2(cost): # 50x slower vs scipy
- cost = np.asarray(cost, float)
- nr, nc = cost.shape
- u = np.zeros(nr)
- v = np.zeros(nc)
- shortest = np.full(nc, np.inf)
- path = np.full(nc, -1, int)
- col4row = np.full(nr, -1, int)
- row4col = np.full(nc, -1, int)
-
- for curRow in range(nr):
- minVal = 0.0
- remaining = np.arange(nc - 1, -1, -1)
- num_remaining = nc
-
- SR = np.zeros(nr, bool)
- SC = np.zeros(nc, bool)
- shortest[:] = np.inf
-
- sink = -1
- localRow = curRow
-
- while sink < 0:
- SR[localRow] = True
- jlist = remaining[:num_remaining]
- rcost = minVal + cost[localRow, jlist] - u[localRow] - v[jlist]
- mask_better = rcost < shortest[jlist]
- if mask_better.any():
- idxs = jlist[mask_better]
- shortest[idxs] = rcost[mask_better]
- path[idxs] = localRow
- best_idx = -1
- lowest = np.inf
- for idx_in_remaining, j in enumerate(jlist):
- val = shortest[j]
- if val < lowest or (val == lowest and row4col[j] == -1):
- lowest = val
- best_idx = idx_in_remaining
- if lowest == np.inf:
- return np.arange(nr), -np.ones(nr, int)
-
- minVal = lowest
- j = jlist[best_idx]
-
- SC[j] = True
-
- if row4col[j] == -1:
- sink = j
- else:
- localRow = row4col[j]
-
- remaining[best_idx], remaining[num_remaining - 1] = (
- remaining[num_remaining - 1],
- remaining[best_idx],
- )
- num_remaining -= 1
-
- u[curRow] += minVal
- SR_indices = np.nonzero(SR)[0]
- for i in SR_indices:
- if i == curRow:
- continue
- assigned_col = col4row[i]
- if assigned_col >= 0:
- u[i] += minVal - shortest[assigned_col]
- else:
- u[i] += minVal
- SC_indices = np.nonzero(SC)[0]
- for jidx in SC_indices:
- v[jidx] -= minVal - shortest[jidx]
-
- j = sink
- while True:
- i = path[j]
- row4col[j] = i
- prev = col4row[i]
- col4row[i] = j
- j = prev
- if i == curRow:
- break
-
- a = np.arange(nr, dtype=int)
- b = col4row.copy()
- return a, b
-
-def ious(atlbrs, btlbrs):
- """
- Compute cost based on IoU
- :type atlbrs: list[tlbr] | np.ndarray
- :type atlbrs: list[tlbr] | np.ndarray
-
- :rtype ious np.ndarray
- """
- ious = np.zeros((len(atlbrs), len(btlbrs)), dtype=np.float64)
- if ious.size == 0:
- return ious
-
- ious = bbox_ious(
- np.ascontiguousarray(atlbrs, dtype=np.float64),
- np.ascontiguousarray(btlbrs, dtype=np.float64)
- )
-
- return ious
-
-def bbox_ious(boxes, query_boxes):
- N = boxes.shape[0]
- K = query_boxes.shape[0]
-
- boxes_area = ((boxes[:, 2] - boxes[:, 0] + 1) * (boxes[:, 3] - boxes[:, 1] + 1)).reshape(N, 1)
- query_areas = ((query_boxes[:, 2] - query_boxes[:, 0] + 1) * (query_boxes[:, 3] - query_boxes[:, 1] + 1)).reshape(1, K)
-
- ixmin = np.maximum(boxes[:, 0].reshape(N, 1), query_boxes[:, 0].reshape(1, K))
- iymin = np.maximum(boxes[:, 1].reshape(N, 1), query_boxes[:, 1].reshape(1, K))
- ixmax = np.minimum(boxes[:, 2].reshape(N, 1), query_boxes[:, 2].reshape(1, K))
- iymax = np.minimum(boxes[:, 3].reshape(N, 1), query_boxes[:, 3].reshape(1, K))
- iw = np.maximum(ixmax - ixmin + 1, 0)
- ih = np.maximum(iymax - iymin + 1, 0)
- intersection = iw * ih
- union = boxes_area + query_areas - intersection
- overlaps = np.where((iw > 0) & (ih > 0), intersection / union, np.zeros_like(intersection))
- return overlaps
-
-def iou_distance(atracks, btracks):
- """
- Compute cost based on IoU
- :type atracks: list[STrack]
- :type btracks: list[STrack]
-
- :rtype cost_matrix np.ndarray
- """
-
- if (len(atracks)>0 and isinstance(atracks[0], np.ndarray)) or (len(btracks) > 0 and isinstance(btracks[0], np.ndarray)):
- atlbrs = atracks
- btlbrs = btracks
- else:
- atlbrs = [track.tlbr for track in atracks]
- btlbrs = [track.tlbr for track in btracks]
- _ious = ious(atlbrs, btlbrs)
- cost_matrix = 1 - _ious
-
- return cost_matrix
-
-def fuse_score(cost_matrix, detections):
- if cost_matrix.size == 0:
- return cost_matrix
- iou_sim = 1 - cost_matrix
- det_scores = np.array([det.score for det in detections])
- det_scores = np.expand_dims(det_scores, axis=0).repeat(cost_matrix.shape[0], axis=0)
- fuse_sim = iou_sim * det_scores
- fuse_cost = 1 - fuse_sim
- return fuse_cost
\ No newline at end of file