-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathremove_backgrounds.py
145 lines (114 loc) · 4.6 KB
/
remove_backgrounds.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
import glob
from torchvision import utils
from torch.utils.data import DataLoader, Dataset
from shutil import copyfile
from tqdm import tqdm
import os
import cv2
from skimage import io
import multiprocessing
import traceback
import numpy as np
from skimage import io
import torch
import webp
SKIP_ITEM = 0
SIZE_BLOCK = 128
MIN_SIZE = 128
class RawImageDataset(Dataset):
def __init__(self):
file_names = file_names = glob.glob('data/raw/**.jpg', recursive=True)
self.hashes = [f.split('/')[-1][:-4] for f in file_names]
self.skip_existing_files = True
def __len__(self):
return len(self.hashes)
def __getitem__(self, index):
hash = self.hashes[index]
image_file_name = 'data/raw/{:s}.jpg'.format(hash)
result_file_name = 'data/images_alpha/{:s}.webp'.format(hash)
if self.skip_existing_files and os.path.exists(result_file_name):
return SKIP_ITEM
try:
image = io.imread(image_file_name)
image = image.transpose((2, 0, 1)).astype(np.float32) / 255
input_width = image.shape[2]
input_height = image.shape[1]
width = SIZE_BLOCK * (input_width // SIZE_BLOCK + 1)
height = SIZE_BLOCK * (input_height // SIZE_BLOCK + 1)
result = np.ones((3, height, width), dtype=np.float32)
result[:, :image.shape[1], :image.shape[2]] = image
image = torch.from_numpy(result)
except:
print("Could not open {:s}.".format(image_file_name))
return SKIP_ITEM
return image, result_file_name, input_width, input_height
def remove_smaller_components(mask):
_, labels, stats, _ = cv2.connectedComponentsWithStats(mask.astype(np.uint8), connectivity=4)
if stats.shape[0] < 2:
return
max_label = np.argmax(stats[1:, 4]) + 1
mask[labels != max_label] = 0
def save_image(image, mask, file_name):
image = image.squeeze(0).numpy()
mask = mask.squeeze(0).numpy()
mask -= 0.5
mask /= 0.35
mask += 0.5
mask = np.clip(mask, 0, 1)
mask_binary = mask > 0.001
remove_smaller_components(mask_binary)
mask *= mask_binary # remove unconnected components
coords = np.stack(mask_binary.nonzero())
if coords.size == 0:
return
top_left = np.min(coords, axis=1)
bottom_right = np.max(coords, axis=1)
mask = mask[top_left[0]:bottom_right[0], top_left[1]:bottom_right[1]]
image = image[:, top_left[0]:bottom_right[0], top_left[1]:bottom_right[1]]
if image.shape[1] < MIN_SIZE or image.shape[2] < MIN_SIZE:
return
new_size = int(max(image.shape[1], image.shape[2]))
result = np.ones((4, new_size, new_size))
result[3, :, :] = 0
y, x = (new_size - image.shape[1]) // 2, (new_size - image.shape[2]) // 2
result[:3, y:y+image.shape[1], x:x+image.shape[2]] = image
result[3, y:y+image.shape[1], x:x+image.shape[2]] = mask
webp.imwrite(file_name, (result.transpose((1, 2, 0)) * 255).astype(np.uint8).copy(order='C'), quality=95)
if __name__ == '__main__':
import torch
from classifier import Classifier
from torch.utils.data import DataLoader, Dataset
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
CLASSIFIER_FILENAME = 'trained_models/classifier.to'
classifier = Classifier()
classifier.cuda()
classifier.load_state_dict(torch.load(CLASSIFIER_FILENAME))
classifier.eval()
dataset = RawImageDataset()
data_loader = DataLoader(dataset, batch_size=1, shuffle=True, num_workers=8)
worker_count = os.cpu_count()
print("Using {:d} processes.".format(worker_count))
context = multiprocessing.get_context('spawn')
pool = context.Pool(worker_count)
progress = tqdm(total=len(dataset))
def on_complete(*_):
progress.update()
for item in data_loader:
if item == SKIP_ITEM:
progress.update()
continue
image, result_file_name, width, height = item
width, height = width[0].item(), height[0].item()
try:
with torch.no_grad():
mask = classifier(image.to(device)).squeeze(0).squeeze(0).cpu()
image = image[0, :, :height, :width]
mask = mask[:height, :width]
pool.apply_async(save_image, args=(image, mask, result_file_name[0]), callback=on_complete)
except Exception as exception:
if isinstance(exception, KeyboardInterrupt):
raise exception
print(("Error while handling {:s}".format(result_file_name[0])))
traceback.print_exc()
pool.close()
pool.join()