你用SORT和deep SORT两种算法分别实现多目标行人的跟踪,并写出示例代码
时间: 2023-06-13 17:05:37 浏览: 134
多目标跟踪DeepSort
好的,下面分别介绍使用 SORT 和 Deep SORT 两种算法实现多目标行人跟踪的示例代码。
首先介绍使用 SORT 算法实现多目标行人跟踪的示例代码:
```python
import numpy as np
from filterpy.kalman import KalmanFilter
from scipy.optimize import linear_sum_assignment
class SORT:
def __init__(self):
self.dt = 1.0
self.lamb = 100
self.sigma_l = 10
self.sigma_h = 1.0
self.sigma_iou = 0.3
self.min_hits = 3
self.max_age = 20
self.trackers_list = []
self.frame_count = 0
def __dist(self, bbox1, bbox2):
x1, y1, w1, h1 = bbox1
x2, y2, w2, h2 = bbox2
c1 = np.array([x1 + w1 / 2.0, y1 + h1 / 2.0])
c2 = np.array([x2 + w2 / 2.0, y2 + h2 / 2.0])
return np.sqrt(np.sum(np.square(c1 - c2)))
def __linear_assignment(self, cost_matrix):
row_ind, col_ind = linear_sum_assignment(cost_matrix)
return row_ind, col_ind
def __get_cost_matrix(self, detections, trackers):
cost_matrix = np.zeros((len(detections), len(trackers)), dtype=np.float32)
for i, detection in enumerate(detections):
for j, tracker in enumerate(trackers):
cost_matrix[i, j] = self.__dist(detection, tracker.predict())
return cost_matrix
def __update_tracker(self, tracker, detection):
tracker.kf.update(detection)
tracker.hits += 1
tracker.no_losses = 0
def __create_tracker(self, detection):
tracker = KalmanFilter(dim_x=7, dim_z=4)
tracker.x[:4] = detection
tracker.x[4:] = 0.0
tracker.P[4:, 4:] *= 1000.0
tracker.P *= 10.0
tracker.R = np.diag([self.sigma_l ** 2, self.sigma_l ** 2,
self.sigma_h ** 2, self.sigma_h ** 2])
tracker.Q[:4, :4] *= 0.01
tracker.Q[4:, 4:] *= 0.01
tracker.Q[4:, 4:] *= self.dt
tracker.H = np.zeros((4, 7))
tracker.H[:4, :4] = np.eye(4)
tracker.hits = 1
tracker.no_losses = 0
return tracker
def __remove_trackers(self):
self.trackers_list = [tracker for tracker in self.trackers_list if tracker.no_losses <= self.max_age]
def __remove_duplicate_trackers(self):
for tracker1 in self.trackers_list:
for tracker2 in self.trackers_list:
if tracker1 == tracker2:
continue
if self.__iou(tracker1.predict(), tracker2.predict()) > self.sigma_iou:
if tracker1.hits >= tracker2.hits:
tracker2.no_losses = self.max_age + 1
else:
tracker1.no_losses = self.max_age + 1
def __ious(self, bboxes1, bboxes2):
ious = np.zeros((len(bboxes1), len(bboxes2)), dtype=np.float32)
for i, bbox1 in enumerate(bboxes1):
for j, bbox2 in enumerate(bboxes2):
ious[i, j] = self.__iou(bbox1, bbox2)
return ious
def __iou(self, bbox1, bbox2):
x1, y1, w1, h1 = bbox1
x2, y2, w2, h2 = bbox2
xi1 = max(x1, x2)
yi1 = max(y1, y2)
xi2 = min(x1 + w1, x2 + w2)
yi2 = min(y1 + h1, y2 + h2)
inter_area = max(xi2 - xi1, 0) * max(yi2 - yi1, 0)
union_area = w1 * h1 + w2 * h2 - inter_area
return inter_area / union_area
def update(self, detections):
self.frame_count += 1
for tracker in self.trackers_list:
self.__update_tracker(tracker, np.array([0, 0, 0, 0]))
cost_matrix = self.__get_cost_matrix(detections, self.trackers_list)
row_ind, col_ind = self.__linear_assignment(cost_matrix)
matched = set()
for i, j in zip(row_ind, col_ind):
if cost_matrix[i, j] > self.lamb:
continue
tracker = self.trackers_list[j]
detection = detections[i]
self.__update_tracker(tracker, detection)
matched.add(j)
unmatched_trackers = set(range(len(self.trackers_list))) - matched
for j in unmatched_trackers:
tracker = self.trackers_list[j]
tracker.no_losses += 1
unmatched_detections = set(range(len(detections))) - set(row_ind)
for i in unmatched_detections:
detection = detections[i]
tracker = self.__create_tracker(detection)
self.trackers_list.append(tracker)
self.__remove_trackers()
self.__remove_duplicate_trackers()
return self.trackers_list
```
使用 SORT 算法跟踪多目标行人的示例代码:
```python
import cv2
import numpy as np
from sort import SORT
def main():
cap = cv2.VideoCapture("test.mp4")
tracker = SORT()
while True:
ret, frame = cap.read()
if not ret:
break
detections = [[100, 100, 50, 100], [200, 200, 100, 200]]
trackers = tracker.update(detections)
for tracker in trackers:
bbox = tracker.predict()
x, y, w, h = bbox
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("frame", frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
```
接着介绍使用 Deep SORT 算法实现多目标行人跟踪的示例代码:
```python
import numpy as np
from filterpy.kalman import KalmanFilter
from scipy.optimize import linear_sum_assignment
class DeepSORT:
def __init__(self):
self.dt = 1.0
self.lamb = 100
self.sigma_l = 10
self.sigma_h = 1.0
self.sigma_iou = 0.3
self.min_hits = 3
self.max_age = 20
self.trackers_list = []
self.frame_count = 0
def __dist(self, bbox1, bbox2):
x1, y1, w1, h1 = bbox1
x2, y2, w2, h2 = bbox2
c1 = np.array([x1 + w1 / 2.0, y1 + h1 / 2.0])
c2 = np.array([x2 + w2 / 2.0, y2 + h2 / 2.0])
return np.sqrt(np.sum(np.square(c1 - c2)))
def __linear_assignment(self, cost_matrix):
row_ind, col_ind = linear_sum_assignment(cost_matrix)
return row_ind, col_ind
def __get_cost_matrix(self, detections, trackers):
cost_matrix = np.zeros((len(detections), len(trackers)), dtype=np.float32)
for i, detection in enumerate(detections):
for j, tracker in enumerate(trackers):
cost_matrix[i, j] = self.__dist(detection, tracker.predict())
return cost_matrix
def __update_tracker(self, tracker, detection):
tracker.kf.update(detection)
tracker.hits += 1
tracker.no_losses = 0
def __create_tracker(self, detection):
tracker = KalmanFilter(dim_x=7, dim_z=4)
tracker.x[:4] = detection
tracker.x[4:] = 0.0
tracker.P[4:, 4:] *= 1000.0
tracker.P *= 10.0
tracker.R = np.diag([self.sigma_l ** 2, self.sigma_l ** 2,
self.sigma_h ** 2, self.sigma_h ** 2])
tracker.Q[:4, :4] *= 0.01
tracker.Q[4:, 4:] *= 0.01
tracker.Q[4:, 4:] *= self.dt
tracker.H = np.zeros((4, 7))
tracker.H[:4, :4] = np.eye(4)
tracker.hits = 1
tracker.no_losses = 0
return tracker
def __remove_trackers(self):
self.trackers_list = [tracker for tracker in self.trackers_list if tracker.no_losses <= self.max_age]
def __remove_duplicate_trackers(self):
for tracker1 in self.trackers_list:
for tracker2 in self.trackers_list:
if tracker1 == tracker2:
continue
if self.__iou(tracker1.predict(), tracker2.predict()) > self.sigma_iou:
if tracker1.hits >= tracker2.hits:
tracker2.no_losses = self.max_age + 1
else:
tracker1.no_losses = self.max_age + 1
def __ious(self, bboxes1, bboxes2):
ious = np.zeros((len(bboxes1), len(bboxes2)), dtype=np.float32)
for i, bbox1 in enumerate(bboxes1):
for j, bbox2 in enumerate(bboxes2):
ious[i, j] = self.__iou(bbox1, bbox2)
return ious
def __iou(self, bbox1, bbox2):
x1, y1, w1, h1 = bbox1
x2, y2, w2, h2 = bbox2
xi1 = max(x1, x2)
yi1 = max(y1, y2)
xi2 = min(x1 + w1, x2 + w2)
yi2 = min(y1 + h1, y2 + h2)
inter_area = max(xi2 - xi1, 0) * max(yi2 - yi1, 0)
union_area = w1 * h1 + w2 * h2 - inter_area
return inter_area / union_area
def update(self, detections, features):
self.frame_count += 1
for tracker in self.trackers_list:
self.__update_tracker(tracker, np.array([0, 0, 0, 0]))
cost_matrix = self.__get_cost_matrix(detections, self.trackers_list)
row_ind, col_ind = self.__linear_assignment(cost_matrix)
matched = set()
for i, j in zip(row_ind, col_ind):
if cost_matrix[i, j] > self.lamb:
continue
tracker = self.trackers_list[j]
detection = detections[i]
feature = features[i]
tracker.features.append(feature)
self.__update_tracker(tracker, detection)
matched.add(j)
unmatched_trackers = set(range(len(self.trackers_list))) - matched
for j in unmatched_trackers:
tracker = self.trackers_list[j]
tracker.no_losses += 1
unmatched_detections = set(range(len(detections))) - set(row_ind)
for i in unmatched_detections:
detection = detections[i]
feature = features[i]
tracker = self.__create_tracker(detection)
tracker.features.append(feature)
self.trackers_list.append(tracker)
self.__remove_trackers()
self.__remove_duplicate_trackers()
return self.trackers_list
```
使用 Deep SORT 算法跟踪多目标行人的示例代码:
```python
import cv2
import numpy as np
from deep_sort import DeepSORT
def main():
cap = cv2.VideoCapture("test.mp4")
deepsort = DeepSORT()
while True:
ret, frame = cap.read()
if not ret:
break
detections = [[100, 100, 50, 100], [200, 200, 100, 200]]
features = np.random.rand(len(detections), 128)
trackers = deepsort.update(detections, features)
for tracker in trackers:
bbox = tracker.predict()
x, y, w, h = bbox
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("frame", frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
```
以上就是使用 SORT 和 Deep SORT 两种算法实现多目标行人跟踪的示例代码。
阅读全文