Spaces:
Runtime error
Runtime error
File size: 4,505 Bytes
bff20b3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | # Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import math
from typing import Tuple
import cv2
import numpy as np
def bbox_xyxy2xywh(bbox_xyxy: np.ndarray) -> np.ndarray:
bbox_xywh = bbox_xyxy.copy()
bbox_xywh[:, 2] = bbox_xywh[:, 2] - bbox_xywh[:, 0]
bbox_xywh[:, 3] = bbox_xywh[:, 3] - bbox_xywh[:, 1]
return bbox_xywh
def bbox_xywh2xyxy(bbox_xywh: np.ndarray) -> np.ndarray:
bbox_xyxy = bbox_xywh.copy()
bbox_xyxy[:, 2] = bbox_xyxy[:, 2] + bbox_xyxy[:, 0]
bbox_xyxy[:, 3] = bbox_xyxy[:, 3] + bbox_xyxy[:, 1]
return bbox_xyxy
def bbox_xyxy2cs(
bbox: np.ndarray, padding: float = 1.0
) -> Tuple[np.ndarray, np.ndarray]:
# convert single bbox from (4, ) to (1, 4)
dim = bbox.ndim
if dim == 1:
bbox = bbox[None, :]
x1, y1, x2, y2 = np.hsplit(bbox, [1, 2, 3])
center = np.hstack([x1 + x2, y1 + y2]) * 0.5
scale = np.hstack([x2 - x1, y2 - y1]) * padding
if dim == 1:
center = center[0]
scale = scale[0]
return center, scale
def bbox_xywh2cs(
bbox: np.ndarray, padding: float = 1.0
) -> Tuple[np.ndarray, np.ndarray]:
# convert single bbox from (4, ) to (1, 4)
dim = bbox.ndim
if dim == 1:
bbox = bbox[None, :]
x, y, w, h = np.hsplit(bbox, [1, 2, 3])
center = np.hstack([x + w * 0.5, y + h * 0.5])
scale = np.hstack([w, h]) * padding
if dim == 1:
center = center[0]
scale = scale[0]
return center, scale
def bbox_cs2xyxy(
center: np.ndarray, scale: np.ndarray, padding: float = 1.0
) -> np.ndarray:
dim = center.ndim
assert scale.ndim == dim
if dim == 1:
center = center[None, :]
scale = scale[None, :]
wh = scale / padding
xy = center - 0.5 * wh
bbox = np.hstack((xy, xy + wh))
if dim == 1:
bbox = bbox[0]
return bbox
def bbox_cs2xywh(
center: np.ndarray, scale: np.ndarray, padding: float = 1.0
) -> np.ndarray:
dim = center.ndim
assert scale.ndim == dim
if dim == 1:
center = center[None, :]
scale = scale[None, :]
wh = scale / padding
xy = center - 0.5 * wh
bbox = np.hstack((xy, wh))
if dim == 1:
bbox = bbox[0]
return bbox
def get_udp_warp_matrix(
center: np.ndarray,
scale: np.ndarray,
rot: float,
output_size: Tuple[int, int],
) -> np.ndarray:
assert len(center) == 2
assert len(scale) == 2
assert len(output_size) == 2
input_size = center * 2
rot_rad = np.deg2rad(rot)
warp_mat = np.zeros((2, 3), dtype=np.float32)
scale_x = (output_size[0] - 1) / scale[0]
scale_y = (output_size[1] - 1) / scale[1]
warp_mat[0, 0] = math.cos(rot_rad) * scale_x
warp_mat[0, 1] = -math.sin(rot_rad) * scale_x
warp_mat[0, 2] = scale_x * (
-0.5 * input_size[0] * math.cos(rot_rad)
+ 0.5 * input_size[1] * math.sin(rot_rad)
+ 0.5 * scale[0]
)
warp_mat[1, 0] = math.sin(rot_rad) * scale_y
warp_mat[1, 1] = math.cos(rot_rad) * scale_y
warp_mat[1, 2] = scale_y * (
-0.5 * input_size[0] * math.sin(rot_rad)
- 0.5 * input_size[1] * math.cos(rot_rad)
+ 0.5 * scale[1]
)
return warp_mat
def get_warp_matrix(
center: np.ndarray,
scale: np.ndarray,
rot: float,
output_size: Tuple[int, int],
shift: Tuple[float, float] = (0.0, 0.0),
inv: bool = False,
) -> np.ndarray:
assert len(center) == 2
assert len(scale) == 2
assert len(output_size) == 2
assert len(shift) == 2
shift = np.array(shift)
src_w = scale[0]
dst_w = output_size[0]
dst_h = output_size[1]
rot_rad = np.deg2rad(rot)
src_dir = _rotate_point(np.array([0.0, src_w * -0.5]), rot_rad)
dst_dir = np.array([0.0, dst_w * -0.5])
src = np.zeros((3, 2), dtype=np.float32)
src[0, :] = center + scale * shift
src[1, :] = center + src_dir + scale * shift
src[2, :] = _get_3rd_point(src[0, :], src[1, :])
dst = np.zeros((3, 2), dtype=np.float32)
dst[0, :] = [dst_w * 0.5, dst_h * 0.5]
dst[1, :] = np.array([dst_w * 0.5, dst_h * 0.5]) + dst_dir
dst[2, :] = _get_3rd_point(dst[0, :], dst[1, :])
if inv:
warp_mat = cv2.getAffineTransform(np.float32(dst), np.float32(src))
else:
warp_mat = cv2.getAffineTransform(np.float32(src), np.float32(dst))
return warp_mat
|