Skip to content

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

clean

roryclear9afb5e2741b9
diff --git a/ocsort_tracker/__pycache__/STrack.cpython-311.pyc b/ocsort_tracker/__pycache__/STrack.cpython-311.pyc
index d5ef3cc..112f61c 100644
Binary files a/ocsort_tracker/__pycache__/STrack.cpython-311.pyc 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
index d6f1a3f..b2b2a0a 100644
Binary files a/ocsort_tracker/__pycache__/association.cpython-311.pyc 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
index 354a442..62ea379 100644
Binary files a/ocsort_tracker/__pycache__/kalmanfilter.cpython-311.pyc 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
index a804ef7..5ea3308 100644
Binary files a/ocsort_tracker/__pycache__/ocsort.cpython-311.pyc and b/ocsort_tracker/__pycache__/ocsort.cpython-311.pyc differ
diff --git a/ocsort_tracker/kalmanfilter.py b/ocsort_tracker/kalmanfilter.py
index 8bfe76b..ccbee15 100644
--- a/ocsort_tracker/kalmanfilter.py
+++ b/ocsort_tracker/kalmanfilter.py
@@ -42,14 +42,6 @@ class KalmanFilterNew(object):
# 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
@@ -63,153 +55,71 @@ class KalmanFilterNew(object):
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 predict(self):
+ self.x = dot(self.F, self.x)
+ self.P = self._alpha_sq * dot(dot(self.F, self.P), self.F.T) + self.Q
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()
+ occur = [int(d is None) for d in self.history_obs]
+ indices = np.where(np.array(occur)==0)[0]
+ index1 = indices[-2]
+ index2 = indices[-1]
+ box1 = self.history_obs[index1]
+ x1, y1, s1, r1 = box1
+ w1 = np.sqrt(s1 * r1)
+ h1 = np.sqrt(s1 / r1)
+ box2 = self.history_obs[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
+ self.__dict__ = self.attr_saved
+ 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))
+ self.update(new_box)
+ if not i == (index2-index1-1): # self.predict
+ self.x = dot(self.F, self.x)
+ self.P = self._alpha_sq * dot(dot(self.F, self.P), self.F.T) + self.Q
- 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
+ def update(self, z, R=None, H=None):
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.attr_saved = deepcopy(self.__dict__)
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()
+ if not self.observed and self.attr_saved: 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
+ R = self.R
+ z = reshape_z(z, self.dim_z, self.x.ndim)
+ H = self.H
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"""
diff --git a/ocsort_tracker/ocsort.py b/ocsort_tracker/ocsort.py
index 398f8e1..1366d46 100644
--- a/ocsort_tracker/ocsort.py
+++ b/ocsort_tracker/ocsort.py
@@ -64,7 +64,6 @@ class KalmanBoxTracker(object):
def __init__(self, bbox, delta_t=3, orig=False):
"""
Initialises a tracker using initial bounding box.
-
"""
from .kalmanfilter import KalmanFilterNew as KalmanFilter
@@ -97,8 +96,8 @@ class KalmanBoxTracker(object):
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
+ self.velocity = np.array((0, 0))
def update(self, bbox, score=None, class_id=None):
"""
@@ -121,7 +120,7 @@ class KalmanBoxTracker(object):
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.
@@ -144,7 +143,6 @@ class KalmanBoxTracker(object):
"""
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):
@@ -170,7 +168,6 @@ class OCSort(object):
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
@@ -201,7 +198,7 @@ class OCSort(object):
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
+ inds_second = np.logical_and(inds_low, inds_high)
dets_second = dets[inds_second] # detections for second matching
class_ids_second = class_ids[inds_second]
scores_second = scores[inds_second]
@@ -212,19 +209,12 @@ class OCSort(object):
# 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])
+ velocities = np.array([trk.velocity 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])
@@ -316,6 +306,4 @@ class OCSort(object):
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
-
-
+ return out
\ No newline at end of file