Skip to content

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

Min speed (#117)

Rory Clear8f29c546f2d3
diff --git a/clearcam.py b/clearcam.py
index f83a757..12e0564 100644
--- a/clearcam.py
+++ b/clearcam.py
@@ -486,7 +486,7 @@ class VideoCapture:
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
+ if x.tracklet_len < 1 or x.speed < 2.5: continue # dont alert for 1 frame, too many false positives. min speed, don't detect still objects, they jitter too. # TODO what's the best min value?
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"])
diff --git a/ocsort_tracker/STrack.py b/ocsort_tracker/STrack.py
index 93198ac..f94c976 100644
--- a/ocsort_tracker/STrack.py
+++ b/ocsort_tracker/STrack.py
@@ -2,7 +2,7 @@ from collections import defaultdict
import numpy as np
class STrack:
- def __init__(self, tlwh, score, class_id, track_id=None, age=0):
+ def __init__(self, tlwh, score, class_id, track_id=None, age=0, speed=0):
self.occurrences = defaultdict(float)
# wait activate
self._tlwh = np.asarray(tlwh, dtype=np.float64)
@@ -14,6 +14,7 @@ class STrack:
self.class_id = class_id
self.track_id = track_id
self.tracklet_len = age
+ self.speed = speed
@property
# @jit(nopython=True)
diff --git a/ocsort_tracker/ocsort.py b/ocsort_tracker/ocsort.py
index 1366d46..58d2b93 100644
--- a/ocsort_tracker/ocsort.py
+++ b/ocsort_tracker/ocsort.py
@@ -52,7 +52,7 @@ def speed_direction(bbox1, bbox2):
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
+ return speed, speed / norm
class KalmanBoxTracker(object):
@@ -98,6 +98,8 @@ class KalmanBoxTracker(object):
self.history_observations = []
self.delta_t = delta_t
self.velocity = np.array((0, 0))
+ self.avg_vel = np.array((0, 0))
+ self.speed = 0
def update(self, bbox, score=None, class_id=None):
"""
@@ -119,8 +121,9 @@ class KalmanBoxTracker(object):
"""
Estimate the track speed direction with observations \Delta t steps away
"""
- self.velocity = speed_direction(previous_box, bbox)
-
+ dist, self.velocity = speed_direction(previous_box, bbox)
+ self.avg_vel = [s + d / float(self.age) for s, d in zip(self.avg_vel, dist)]
+ self.speed = abs(self.avg_vel[0]) + abs(self.avg_vel[1])
"""
Insert new observations. This is a ugly way to maintain both self.observations
and self.history_observations. Bear it for the moment.
@@ -297,13 +300,13 @@ class OCSort(object):
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))
+ ret.append(np.concatenate((d, [trk.id+1, trk.age, trk.class_id, trk.score, trk.speed])).reshape(1, -1))
i -= 1
# remove dead tracklet
if(trk.time_since_update > self.max_age):
- self.trackers.pop(i)
+ if trk.speed > 2 or trk.time_since_update > 600: 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]))
+ 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], speed=x[0][8]))
return out
\ No newline at end of file
diff --git a/test/test_mot.py b/test/test_mot.py
index 8ff1d1e..434343d 100644
--- a/test/test_mot.py
+++ b/test/test_mot.py
@@ -46,10 +46,11 @@ if __name__ == "__main__":
Path('./test_outputs').mkdir(parents=True, exist_ok=True)
trackers = [ocs_tracker]
- excepted_ppl = [241] # same as byte-track's was
+ excepted_ppl = [162]
for j,t in enumerate(trackers):
cap = cv2.VideoCapture("test/videos/MOT16-03.mp4")
+ #cap = cv2.VideoCapture("test/videos/rain_long.mp4")
w, h = int(cap.get(3)), int(cap.get(4))
out = cv2.VideoWriter(f"test_outputs/out_{size}_{j}.mp4", cv2.VideoWriter_fourcc(*"mp4v"), 30, (w, h))
@@ -76,6 +77,7 @@ if __name__ == "__main__":
online_targets = t.update(pred, [960,960], [960,960], 0.25)
preds = []
for x in online_targets:
+ if x.tracklet_len < 1 or x.speed < 2.5: continue
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