Spaces:
Paused
Paused
annotator/util
Browse files- annotator/util.py +48 -0
annotator/util.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import cv2
|
3 |
+
import os
|
4 |
+
|
5 |
+
|
6 |
+
annotator_ckpts_path = os.path.join(os.path.dirname(__file__), 'ckpts')
|
7 |
+
|
8 |
+
|
9 |
+
def HWC3(x):
|
10 |
+
assert x.dtype == np.uint8
|
11 |
+
if x.ndim == 2:
|
12 |
+
x = x[:, :, None]
|
13 |
+
assert x.ndim == 3
|
14 |
+
H, W, C = x.shape
|
15 |
+
assert C == 1 or C == 3 or C == 4
|
16 |
+
if C == 3:
|
17 |
+
return x
|
18 |
+
if C == 1:
|
19 |
+
return np.concatenate([x, x, x], axis=2)
|
20 |
+
if C == 4:
|
21 |
+
color = x[:, :, 0:3].astype(np.float32)
|
22 |
+
alpha = x[:, :, 3:4].astype(np.float32) / 255.0
|
23 |
+
y = color * alpha + 255.0 * (1.0 - alpha)
|
24 |
+
y = y.clip(0, 255).astype(np.uint8)
|
25 |
+
return y
|
26 |
+
|
27 |
+
|
28 |
+
def resize_image(input_image, resolution):
|
29 |
+
H, W, C = input_image.shape
|
30 |
+
# H, W 중 짧은 쪽을 resolution과 맞춤
|
31 |
+
|
32 |
+
if H > W:
|
33 |
+
k = resolution / W
|
34 |
+
W = resolution
|
35 |
+
H = int(H * k)
|
36 |
+
else:
|
37 |
+
k = resolution / H
|
38 |
+
H = resolution
|
39 |
+
W = int(W * k)
|
40 |
+
|
41 |
+
input_image = cv2.resize(input_image, (W, H), interpolation=cv2.INTER_CUBIC)
|
42 |
+
|
43 |
+
# resolution으로 center crop
|
44 |
+
start_w = (W - resolution) // 2
|
45 |
+
start_h = (H - resolution) // 2
|
46 |
+
img = input_image[start_h:start_h + resolution, start_w:start_w + resolution]
|
47 |
+
|
48 |
+
return img
|