Skip to content

Commit

Permalink
fix: pr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisochoatri committed Aug 29, 2022
1 parent 502f93e commit 9c4a889
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions dgp/annotations/camera_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def calc_affine_transform(
Parameters
----------
theta: float
Rotation angle in degrees.
scale: float
Expand All @@ -64,7 +63,6 @@ def calc_affine_transform(
Returns
-------
A: np.ndarray
3x3 matrix that expresses the requested transformations.
"""
Expand Down Expand Up @@ -94,16 +92,16 @@ def calc_affine_transform(


def box_crop_affine_transform(
box_xyxy: Tuple[int, int, int, int],
box_ltrb: Tuple[int, int, int, int],
target_shape: Tuple[int, int],
) -> np.ndarray:
"""Generates a matrix that crops a rectangular area from an image and resizes it to target shape.
Note, this preserves the aspect ratio in target shape.
Parameters
----------
box_xyxy: list or tuple
Box corners expressed as x1,y1,x2,y2.
box_ltrb: list or tuple
Box corners expressed as left, top, right, bottom (x1,y1,x2,y2).
target_shape: tuple
Desired image shape (h,w) after cropping and resizing.
Expand All @@ -113,7 +111,7 @@ def box_crop_affine_transform(
3x3 matrix that expresses the requested transformation.
"""
# get box center
x1, y1, x2, y2 = box_xyxy
x1, y1, x2, y2 = box_ltrb
cx, cy = (x1 + x2) / 2, (y1 + y2) / 2
w, h = x2 - x1, y2 - y1

Expand Down Expand Up @@ -272,6 +270,11 @@ def transform_image(
-------
new_img: np.ndarray or PIL.Image.Image
New transformed image.
Raises
------
ValueError
If the mode is not one of cv2.INTER_LINEAR or cv2.INTER_NEAREST
"""
h, w = self.shape[:2]

Expand All @@ -284,6 +287,8 @@ def transform_image(
mode = PIL.Image.BILINEAR
elif mode == cv2.INTER_NEAREST:
mode = PIL.Image.NEAREST
else:
raise ValueError(f'{mode} not supported')

new_img = tx.transform((w, h), img, resample=mode)
else:
Expand All @@ -304,7 +309,6 @@ def transform_camera(
Returns
-------
mtxR: np.array
new camera intrinsics
new_pose: Pose
Expand All @@ -316,7 +320,8 @@ def transform_camera(
# Transform the camera matrix
h, w = self.shape[:2]

# Flipping leads to the wrong rotation below, so when there is a flip present, we unflip, do everything, and re flip
# Flipping leads to the wrong rotation below, so when there is a flip present,
# we unflip, do everything, and re flip
flip_mat = np.eye(3)
flip = False
if self.A[0, 0] < 0:
Expand Down Expand Up @@ -499,12 +504,12 @@ def transform_mask_2d(
Parameters
----------
mask: np.ndarray
mask: np.ndarray, optional
A boolean mask of same shape as the image that denotes a valid pixel
Returns
-------
new_mask: np.ndarray
new_mask: np.ndarray, optional
The transformed mask or None if the input mask was None
"""

Expand Down Expand Up @@ -630,6 +635,9 @@ def transform_datum(self, cam_datum: Dict[str, Any]) -> Dict[str, Any]: # pylin
instance_seg = self.transform_panoptic_segmentation_2d(instance_seg, )
new_datum['instance_segmentation_2d'] = instance_seg

if 'key_line_2d' in new_datum:
logger.warning('key_line_2d curently not supported')

# TODO(chrisochoatri): verify behavior when Nonetype is passed for each annotation
# TODO(chrisochoatri): line 2d/3d annotations
# TODO(chrisochoatri): polygon annotation
Expand Down

0 comments on commit 9c4a889

Please sign in to comment.