Skip to content

Commit

Permalink
Merge pull request #136 from Visual-Behavior/fixe-pts2d-boxes2d-crop
Browse files Browse the repository at this point in the history
Fix crop on boxes and pts2d when using absolute position
  • Loading branch information
thibo73800 authored Dec 20, 2021
2 parents 3742ca3 + 3290694 commit 552d4b2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
5 changes: 4 additions & 1 deletion aloscene/bounding_boxes_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,10 @@ def _crop(self, H_crop: tuple, W_crop: tuple, **kwargs):

# Put back the instance into the same state as before
if absolute:
cropped_boxes = cropped_boxes.abs_pos(frame_size)
n_frame_size = ((H_crop[1] - H_crop[0]) * frame_size[0], (W_crop[1] - W_crop[0]) * frame_size[1])
cropped_boxes = cropped_boxes.abs_pos(n_frame_size)
else:
cropped_boxes.frame_size = None

cropped_boxes = cropped_boxes.get_with_format(boxes_format)

Expand Down
9 changes: 7 additions & 2 deletions aloscene/points_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,10 @@ def _crop(self, H_crop: tuple, W_crop: tuple, **kwargs):

absolute = self.absolute
frame_size = self.frame_size

points_format = self.points_format

# Get a new set of bbox
# Get a new set of points
n_points = self.abs_pos((100, 100)).xy()

# Retrieve crop coordinates
Expand All @@ -467,7 +468,11 @@ def _crop(self, H_crop: tuple, W_crop: tuple, **kwargs):

# Put back the instance into the same state as before
if absolute:
cropped_points = cropped_points.abs_pos(frame_size)
n_frame_size = ((H_crop[1] - H_crop[0]) * frame_size[0], (W_crop[1] - W_crop[0]) * frame_size[1])
cropped_points = cropped_points.abs_pos(n_frame_size)
else:
cropped_points.frame_size = None

cropped_points = cropped_points.get_with_format(points_format)

return cropped_points
Expand Down
23 changes: 21 additions & 2 deletions unittest/test_boxes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# from aloscene.renderer import View
from alodataset import WaymoDataset # , Split
import aloscene

import torch
import numpy as np

waymo_dataset = WaymoDataset(sample=True)
TEST_FRAME = 0
Expand Down Expand Up @@ -375,6 +375,24 @@ def test_boxes_slice():
assert len(n_frame.boxes2d["gt_boxes_2d"].shape) == 2


def test_crop_abs():
image = np.zeros((3, 843, 1500))
boxes = [[298, 105, 50, 50], [1250, 105, 50, 50], [298, 705, 50, 50], [1250, 705, 50, 50]]
frame = aloscene.Frame(image)
labels = aloscene.Labels([0, 0, 0, 0], labels_names=["boxes"])
boxes = aloscene.BoundingBoxes2D(
boxes, boxes_format="xcyc", frame_size=(frame.H, frame.W), absolute=True, labels=labels
)
frame.append_boxes2d(boxes)
# frame.get_view().render()
frame = frame.crop(H_crop=(0.0, 0.5), W_crop=(0.0, 0.5))
# frame.get_view().render()
# frame.get_view().render()
assert torch.allclose(frame.boxes2d[0].as_tensor(), boxes[0].as_tensor())
assert np.allclose(frame.boxes2d.frame_size[0], frame.HW[0])
assert np.allclose(frame.boxes2d.frame_size[1], frame.HW[1])


if __name__ == "__main__":
test_boxes_from_dt()
test_boxes_rel_xcyc()
Expand All @@ -383,5 +401,6 @@ def test_boxes_slice():
test_boxes_abs_xcyc()
test_boxes_abs_yxyx()
test_boxes_abs_xyxy()
# test_padded_boxes() # Outdated
# test_padded_boxes() Outdated
test_boxes_slice()
test_crop_abs()
24 changes: 24 additions & 0 deletions unittest/test_points2d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# from aloscene.renderer import View
from alodataset import WaymoDataset # , Split
import aloscene
import torch
import numpy as np


def test_crop_abs():
image = np.zeros((3, 843, 1500))
corners = [[298, 105], [1250, 105], [298, 705], [1250, 705]]
frame = aloscene.Frame(image)
labels = aloscene.Labels([0, 0, 0, 0], labels_names=["corners"])
corners = aloscene.Points2D(
corners, points_format="xy", frame_size=(frame.H, frame.W), absolute=True, labels=labels
)
frame.append_points2d(corners)
frame = frame.crop(H_crop=(0.0, 0.5), W_crop=(0.0, 0.5))
assert torch.allclose(frame.points2d[0].as_tensor(), corners[0].as_tensor())
assert np.allclose(frame.points2d.frame_size[0], frame.HW[0])
assert np.allclose(frame.points2d.frame_size[1], frame.HW[1])


if __name__ == "__main__":
test_crop_abs()

0 comments on commit 552d4b2

Please sign in to comment.