Spaces:
Sleeping
Sleeping
| from __future__ import print_function | |
| import numpy as np | |
| 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.0 | |
| y = bbox[1] + h / 2.0 | |
| s = w * h # scale is just area | |
| r = w / float(h + 1e-6) | |
| return np.array([x, y, s, r]).reshape((4, 1)) | |
| 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 | |
| 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 is None: | |
| return np.array([x[0] - w / 2.0, x[1] - h / 2.0, x[0] + w / 2.0, x[1] + h / 2.0]).reshape((1, 4)) | |
| else: | |
| return np.array([x[0] - w / 2.0, x[1] - h / 2.0, x[0] + w / 2.0, x[1] + h / 2.0, score]).reshape((1, 5)) | |
| 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. | |
| """ | |
| # define constant velocity model | |
| if not orig: | |
| from .kalmanfilter import KalmanFilterNew as KalmanFilter | |
| self.kf = KalmanFilter(dim_x=7, dim_z=4) | |
| else: | |
| from filterpy.kalman import 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.0 | |
| self.kf.P[4:, 4:] *= 1000.0 # give high uncertainty to the unobservable initial velocities | |
| self.kf.P *= 10.0 | |
| 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 | |
| """ | |
| 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): | |
| """ | |
| Updates the state vector with observed bbox. | |
| """ | |
| if bbox is not None: | |
| 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) | |