-
Notifications
You must be signed in to change notification settings - Fork 8
/
gen_characters.py
137 lines (127 loc) · 4.22 KB
/
gen_characters.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
import os
import argparse
import cv2
import torch
import numpy as np
import glob
import math
from torch.cuda import amp
from tqdm import tqdm
import sys
from anime_segmentation.train import AnimeSegmentation, net_names
def get_mask(model, input_img, use_amp=True, s=640):
input_img = (input_img / 255).astype(np.float32)
h, w = h0, w0 = input_img.shape[:-1]
h, w = (s, int(s * w / h)) if h > w else (int(s * h / w), s)
ph, pw = s - h, s - w
img_input = np.zeros([s, s, 3], dtype=np.float32)
img_input[ph // 2 : ph // 2 + h, pw // 2 : pw // 2 + w] = cv2.resize(
input_img, (w, h)
)
img_input = np.transpose(img_input, (2, 0, 1))
img_input = img_input[np.newaxis, :]
tmpImg = torch.from_numpy(img_input).type(torch.FloatTensor).to(model.device)
with torch.no_grad():
if use_amp:
with amp.autocast():
pred = model(tmpImg)
pred = pred.to(dtype=torch.float32)
else:
pred = model(tmpImg)
pred = pred.cpu().numpy()[0]
pred = np.transpose(pred, (1, 2, 0))
pred = pred[ph // 2 : ph // 2 + h, pw // 2 : pw // 2 + w]
pred = cv2.resize(pred, (w0, h0))[:, :, np.newaxis]
return pred
def is_empty(img):
img_line = cv2.adaptiveThreshold(
cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY),
255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,
blockSize=5,
C=7,
)
if np.all(img_line==255):
return True
else:
return False
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# model args
parser.add_argument(
"--net", type=str, default="isnet_is", choices=net_names, help="net name"
)
parser.add_argument(
"--ckpt",
type=str,
default="../../models/anime-seg/isnetis.ckpt",
help="model checkpoint path",
)
parser.add_argument(
"--data",
type=str,
default=".",
help="input data dir",
)
parser.add_argument("--out", type=str, default="../characters", help="output dir")
parser.add_argument(
"--img-size",
type=int,
default=1024,
help="hyperparameter, input image size of the net",
)
parser.add_argument("--device", type=str, default="cuda")
args = parser.parse_args()
print(args)
device = torch.device(args.device)
model = AnimeSegmentation.try_load(
args.net, args.ckpt, args.device, img_size=args.img_size
)
model.eval()
model.to(device)
if not os.path.exists(args.out):
os.mkdir(args.out)
image_paths = glob.glob(f"{args.data}/**/*.png", recursive=True) + glob.glob(
f"{args.data}/**/*.jpg", recursive=True
)
image_paths = [
image_path
for image_path in image_paths
if not os.path.exists(
os.path.join(
args.out,
os.path.relpath(os.path.splitext(image_path)[0] + ".png", args.data),
)
)
]
print(len(image_paths))
character_empty_list = []
for path in tqdm(image_paths):
try:
img = cv2.cvtColor(cv2.imread(path, cv2.IMREAD_COLOR), cv2.COLOR_BGR2RGB)
mask = get_mask(
model,
img,
use_amp=False,
s=1024 if abs(1024-max(img.shape[0], img.shape[1])) <= abs(512-max(img.shape[0], img.shape[1])) else 512,
)
img = np.concatenate((mask * img + 1 - mask, mask * 255), axis=2).astype(
np.uint8
)
img = cv2.cvtColor(img, cv2.COLOR_RGBA2BGRA)
if is_empty(img):
print(path + " hasn't character")
character_empty_list.append(path)
continue
file_name, file_extension = os.path.splitext(path)
save_path = os.path.join(
args.out, os.path.relpath(file_name + ".png", args.data)
)
if not os.path.exists(os.path.dirname(save_path)):
os.makedirs(os.path.dirname(save_path))
cv2.imwrite(save_path, img)
except Exception as e:
print(path + ":" + str(e))
with open("character_empty_list.txt", "w") as f:
f.writelines("\n".join(character_empty_list))