Skip to content

Commit

Permalink
use xyxy2xysr ops
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikel Broström committed Jun 21, 2024
1 parent ed92e46 commit b4162df
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 46 deletions.
31 changes: 2 additions & 29 deletions boxmot/trackers/deepocsort/deep_ocsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from boxmot.utils.iou import get_asso_func
from boxmot.trackers.basetracker import BaseTracker
from boxmot.utils import PerClassDecorator
from boxmot.utils.ops import xyxy2xysr


def k_previous_obs(observations, cur_age, k):
Expand All @@ -24,34 +25,6 @@ def k_previous_obs(observations, cur_age, k):
return observations[max_age]


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 convert_bbox_to_z_new(bbox):
w = bbox[2] - bbox[0]
h = bbox[3] - bbox[1]
x = bbox[0] + w / 2.0
y = bbox[1] + h / 2.0
return np.array([x, y, w, h]).reshape((4, 1))


def convert_x_to_bbox_new(x):
x, y, w, h = x.reshape(-1)[:4]
return np.array([x - w / 2, y - h / 2, x + w / 2, y + h / 2]).reshape(1, 4)


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
Expand Down Expand Up @@ -118,7 +91,7 @@ def __init__(self, det, delta_t=3, emb=None, alpha=0, max_obs=50):
self.kf.P *= 10.0
self.kf.Q[-1, -1] *= 0.01
self.kf.Q[4:, 4:] *= 0.01
self.bbox_to_z_func = convert_bbox_to_z
self.bbox_to_z_func = xyxy2xysr
self.x_to_bbox_func = convert_x_to_bbox

self.kf.x[:4] = self.bbox_to_z_func(bbox)
Expand Down
20 changes: 3 additions & 17 deletions boxmot/trackers/ocsort/ocsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from boxmot.utils.iou import run_asso_func
from boxmot.trackers.basetracker import BaseTracker
from boxmot.utils import PerClassDecorator
from boxmot.utils.ops import xyxy2xysr


def k_previous_obs(observations, cur_age, k):
Expand All @@ -26,21 +27,6 @@ def k_previous_obs(observations, cur_age, k):
return observations[max_age]


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 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
Expand Down Expand Up @@ -109,7 +95,7 @@ def __init__(self, bbox, cls, det_ind, delta_t=3, max_obs=50):
self.kf.Q[-1, -1] *= 0.01
self.kf.Q[4:, 4:] *= 0.01

self.kf.x[:4] = convert_bbox_to_z(bbox)
self.kf.x[:4] = xyxy2xysr(bbox)
self.time_since_update = 0
self.id = KalmanBoxTracker.count
KalmanBoxTracker.count += 1
Expand Down Expand Up @@ -165,7 +151,7 @@ def update(self, bbox, cls, det_ind):
self.time_since_update = 0
self.hits += 1
self.hit_streak += 1
self.kf.update(convert_bbox_to_z(bbox))
self.kf.update(xyxy2xysr(bbox))
else:
self.kf.update(bbox)

Expand Down
24 changes: 24 additions & 0 deletions boxmot/utils/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,27 @@ def tlwh2xyah(x):
y[..., 2] = x[..., 2] / x[..., 3]
y[..., 3] = x[..., 3]
return y


def xyxy2xysr(x):
"""
Converts bounding box coordinates from (x1, y1, x2, y2) format to (x, y, s, r) format.
Args:
bbox (np.ndarray) or (torch.Tensor): The input bounding box coordinates in (x1, y1, x2, y2) format.
Returns:
z (np.ndarray) or (torch.Tensor): The bounding box coordinates in (x, y, s, r) format, where
x, y is the center of the box,
s is the scale (area), and
r is the aspect ratio.
"""
x = x[0:4]
y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
w = y[..., 2] - y[..., 0] # width
h = y[..., 3] - y[..., 1] # height
y[..., 0] = y[..., 0] + w / 2.0 # x center
y[..., 1] = y[..., 1] + h / 2.0 # y center
y[..., 2] = w * h # scale (area)
y[..., 3] = w / (h + 1e-6) # aspect ratio
y = y.reshape((4, 1))
return y

0 comments on commit b4162df

Please sign in to comment.