-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
157 lines (121 loc) · 3.92 KB
/
utils.py
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
import cv2
from PIL import Image, ImageDraw, ImageFont
import numpy as np
from pathlib import Path
def load_image(img_path):
img_path = str(img_path)
img = cv2.imread(img_path, flags=cv2.IMREAD_COLOR)
img = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2RGB)
return img
def to_pil(img):
if not isinstance(img, Image.Image):
img = Image.fromarray(img)
return img
def to_array(img):
img = np.array(img)
return img
def to_3d(img):
if img.ndim == 2:
return np.dstack([img, img, img])
else:
return img
def show_image(img):
copied = img.copy()
copied = to_pil(copied)
copied.show()
def apply_jet_colormap(img):
img_jet = cv2.applyColorMap(src=(255 - img), colormap=cv2.COLORMAP_JET)
return img_jet
def preprocess_image(img):
if img.dtype == "bool":
img = img.astype("uint8") * 255
if img.ndim == 2:
if (
np.array_equal(np.unique(img), np.array([0, 255])) or
np.array_equal(np.unique(img), np.array([0])) or
np.array_equal(np.unique(img), np.array([255]))
):
img = to_3d(img)
else:
img = apply_jet_colormap(img)
return img
def blend_two_images(img1, img2, alpha=0.5):
img1 = to_pil(img1)
img2 = to_pil(img2)
img_blended = Image.blend(im1=img1, im2=img2, alpha=alpha)
return to_array(img_blended)
def save_image(img1, img2=None, alpha=0.5, path="") -> None:
copied1 = preprocess_image(
to_array(img1.copy())
)
if img2 is None:
img_arr = copied1
else:
copied2 = to_array(
preprocess_image(
to_array(img2.copy())
)
)
img_arr = to_array(
blend_two_images(img1=copied1, img2=copied2, alpha=alpha)
)
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
if img_arr.ndim == 3:
cv2.imwrite(
filename=str(path), img=img_arr[:, :, :: -1], params=[cv2.IMWRITE_JPEG_QUALITY, 100]
)
elif img_arr.ndim == 2:
cv2.imwrite(
filename=str(path), img=img_arr, params=[cv2.IMWRITE_JPEG_QUALITY, 100]
)
def resize_image(img, w, h):
resized_img = cv2.resize(src=img, dsize=(w, h))
return resized_img
def get_width_and_height(img):
if img.ndim == 2:
h, w = img.shape
else:
h, w, _ = img.shape
return w, h
def apply_jet_colormap(img):
img_jet = cv2.applyColorMap(src=(255 - img), colormap=cv2.COLORMAP_JET)
return img_jet
def reverse_jet_colormap(img):
gray_values = np.arange(256, dtype=np.uint8)
color_values = list(map(tuple, apply_jet_colormap(gray_values).reshape(256, 3)))
color_to_gray_map = dict(zip(color_values, gray_values))
out = np.apply_along_axis(lambda bgr: color_to_gray_map[tuple(bgr)], axis=2, arr=img)
return out
def draw_bboxes(img, bboxes, idx2cls):
canvas = to_pil(img)
draw = ImageDraw.Draw(canvas)
for x1, y1, x2, y2, label in bboxes.values:
draw.rectangle(
xy=(x1, y1, x2, y2),
outline=(255, 0, 0),
fill=None,
width=max(1, int(min(x2 - x1, y2 - y1) * 0.02))
)
for x1, y1, x2, y2, label in bboxes.values:
draw.text(
xy=(x1, y1 - 4),
text=" " + idx2cls[str(label)][1],
fill="white",
stroke_fill="black",
stroke_width=2,
font=ImageFont.truetype(
# font="./fonts/Pretendard-Regular.otf",
font="/Users/jongbeomkim/Downloads/Pretendard-1.3.6/public/static/Pretendard-Regular.otf",
size=max(10, int(min(40, min(x2 - x1, y2 - y1) * 0.12)))
),
anchor="la"
)
return canvas
def denormalize_array(img):
copied = img.copy()
copied -= copied.min()
copied /= copied.max()
copied *= 255.0
copied = np.clip(a=copied, a_min=0, a_max=255).astype("uint8")
return copied