File size: 3,510 Bytes
41ad279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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