FECO / lib /utils /preprocessing.py
dqj5182's picture
Build ViT-H FECO Space with HMR2 foot crops
41ad279
Raw History Blame Contribute Delete
3.51 kB
import cv2
import numpy as np
from lib.core.config import cfg
def rotate_2d(point, radians):
x, y = point
sine, cosine = np.sin(radians), np.cos(radians)
return np.array([x * cosine - y * sine, x * sine + y * cosine], dtype=np.float32)
def gen_trans_from_patch_cv(
center_x,
center_y,
source_width,
source_height,
destination_width,
destination_height,
scale,
rotation,
inverse=False,
):
source_width *= scale
source_height *= scale
source_center = np.array([center_x, center_y], dtype=np.float32)
radians = np.pi * rotation / 180
source_down = rotate_2d(
np.array([0, source_height * 0.5], dtype=np.float32), radians
)
source_right = rotate_2d(
np.array([source_width * 0.5, 0], dtype=np.float32), radians
)
destination_center = np.array(
[destination_width * 0.5, destination_height * 0.5], dtype=np.float32
)
destination_down = np.array([0, destination_height * 0.5], dtype=np.float32)
destination_right = np.array(
[destination_width * 0.5, 0], dtype=np.float32
)
source = np.stack(
[source_center, source_center + source_down, source_center + source_right]
)
destination = np.stack(
[
destination_center,
destination_center + destination_down,
destination_center + destination_right,
]
)
if inverse:
return cv2.getAffineTransform(destination, source).astype(np.float32)
return cv2.getAffineTransform(source, destination).astype(np.float32)
def generate_patch_image_contact(
image,
bbox,
scale,
rotation,
flip,
output_shape,
translation_x=0.0,
translation_y=0.0,
background="black",
):
image = image.copy()
image_height, image_width = image.shape[:2]
center_x = float(bbox[0] + 0.5 * bbox[2])
center_y = float(bbox[1] + 0.5 * bbox[3])
box_width = float(bbox[2])
box_height = float(bbox[3])
border_value = (255, 255, 255) if background == "white" else (0, 0, 0)
if flip:
image = image[:, ::-1]
center_x = image_width - center_x - 1
center_x += translation_x * image_width
center_y += translation_y * image_height
transform = gen_trans_from_patch_cv(
center_x,
center_y,
box_width,
box_height,
output_shape[1],
output_shape[0],
scale,
rotation,
)
patch = cv2.warpAffine(
image,
transform,
(int(output_shape[1]), int(output_shape[0])),
flags=cv2.INTER_LINEAR,
borderMode=cv2.BORDER_CONSTANT,
borderValue=border_value,
).astype(np.float32)
inverse = gen_trans_from_patch_cv(
center_x,
center_y,
box_width,
box_height,
output_shape[1],
output_shape[0],
scale,
rotation,
inverse=True,
)
return patch, transform, inverse
def augmentation_contact(
image, bbox, data_split, enforce_flip=None, bkg_color="black"
):
if data_split != "test":
raise ValueError("This Space includes inference preprocessing only")
flip = bool(enforce_flip) if enforce_flip is not None else False
patch, transform, inverse = generate_patch_image_contact(
image,
bbox,
1.0,
0.0,
flip,
cfg.MODEL.input_img_shape,
background=bkg_color,
)
return patch, transform, inverse, 0.0, flip, np.ones(3, dtype=int), False, 0.0