Skip to content

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

clean

roryclearb2116fe9798e
diff --git a/ocsort_tracker/association.py b/ocsort_tracker/association.py
index 54ca7ce..83a1373 100644
--- a/ocsort_tracker/association.py
+++ b/ocsort_tracker/association.py
@@ -29,13 +29,6 @@ def speed_direction_batch(dets, tracks):
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)
@@ -58,50 +51,6 @@ def linear_assignment(cost_matrix):
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)
@@ -158,83 +107,4 @@ def associate(detections, trackers, iou_threshold, velocities, previous_obs, vdc
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