-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathbg_remove.py
242 lines (201 loc) · 8.24 KB
/
bg_remove.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import os
import sys
import copy
import warnings
import numpy as np
import cv2
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as transforms
from src.models.modnet import MODNet
warnings.filterwarnings("ignore")
class BGRemove():
# define hyper-parameters
ref_size = 512
# define image to tensor transform
im_transform = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
]
)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# create MODNet and load the pre-trained ckpt
modnet = MODNet(backbone_pretrained=False)
modnet = nn.DataParallel(modnet)
if device == 'cuda':
modnet = modnet.cuda()
def __init__(self, ckpt_path):
self.parameter_load(ckpt_path)
def parameter_load(self, ckpt_path):
BGRemove.modnet.load_state_dict(
torch.load(ckpt_path, map_location=BGRemove.device))
BGRemove.modnet.eval()
def file_load(self, filename):
im = cv2.imread(filename)
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
if len(im.shape) == 2:
im = im[:, :, None]
if im.shape[2] == 1:
im = np.repeat(im, 3, axis=2)
elif im.shape[2] == 4:
im = im[:, :, 0:3]
return im
def dir_check(self, path):
os.makedirs(path, exist_ok=True)
if not path.endswith('/'):
path += '/'
return path
def pre_process(self, im):
self.original_im = copy.deepcopy(im)
# convert image to PyTorch tensor
im = BGRemove.im_transform(im)
# add mini-batch dim
im = im[None, :, :, :]
# resize image for input
im_b, im_c, im_h, im_w = im.shape
self.height, self.width = im_h, im_w
if max(im_h, im_w) < BGRemove.ref_size or min(im_h, im_w) > BGRemove.ref_size:
if im_w >= im_h:
im_rh = BGRemove.ref_size
im_rw = int(im_w / im_h * BGRemove.ref_size)
elif im_w < im_h:
im_rw = BGRemove.ref_size
im_rh = int(im_h / im_w * BGRemove.ref_size)
else:
im_rh = im_h
im_rw = im_w
im_rw = im_rw - im_rw % 32
im_rh = im_rh - im_rh % 32
im = F.interpolate(im, size=(im_rh, im_rw), mode='area')
if BGRemove.device == 'cuda':
im = im.cuda()
return im
def post_process(self, mask_data, background=False, backgound_path='assets/background/background.jpg'):
matte = F.interpolate(mask_data, size=(
self.height, self.width), mode='area')
matte = matte.repeat(1, 3, 1, 1)
matte = matte[0].data.cpu().numpy().transpose(1, 2, 0)
height, width, _ = matte.shape
if background:
back_image = self.file_load(backgound_path)
back_image = cv2.resize(
back_image, (width, height), cv2.INTER_AREA)
else:
back_image = np.full(self.original_im.shape, 255.0)
self.alpha = np.uint8(matte[:, :, 0]*255)
matte = matte * self.original_im + (1 - matte) * back_image
return matte
def image(self, filename, background=False, output='output/', save=True):
output = self.dir_check(output)
self.im_name = filename.split('/')[-1]
im = self.file_load(filename)
im = self.pre_process(im)
_, _, matte = BGRemove.modnet(im, inference=False)
matte = self.post_process(matte, background)
if save:
matte = np.uint8(matte)
msg, name = self.save(matte, output, background)
return name
else:
h, w, _ = matte.shape
r_h, r_w = 720, int((w / h) * 720)
image = cv2.resize(self.original_im, (r_w, r_h), cv2.INTER_AREA)
matte = cv2.resize(matte, (r_w, r_h), cv2.INTER_AREA)
full_image = np.uint8(np.concatenate((image, matte), axis=1))
self.save(full_image, output, background)
exit_key = ord('q')
while True:
if cv2.waitKey(exit_key) & 255 == exit_key:
cv2.destroyAllWindows()
break
cv2.imshow(
'MODNet - {} [Press "Q" To Exit]'.format(self.im_name), full_image)
def video(self, filename, background=False, output='output/'):
output = self.dir_check(output)
output_name = filename.split('/')[-1]
extension = output_name.split('.')[-1]
output_name = output_name.replace(extension, 'mp4')
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
cap = cv2.VideoCapture(filename)
flag = 1
if (cap.isOpened() == False):
print("Error opening video stream or file")
while (cap.isOpened()):
ret, frame = cap.read()
if flag:
height, width, _ = frame.shape
out = cv2.VideoWriter(output+output_name,
fourcc, 20.0, (2*width, height))
flag = 0
if ret:
print('Video is processing..', end='\r')
im = self.pre_process(frame)
_, _, matte = BGRemove.modnet(im, inference=False)
matte = np.uint8(self.post_process(matte, background))
full_image = np.concatenate((frame, matte), axis=1)
full_image = np.uint8(cv2.resize(
full_image, (2*width, height), cv2.INTER_AREA))
out.write(full_image)
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
def folder(self, foldername, background=False, output='output/'):
output = self.dir_check(output)
foldername = self.dir_check(foldername)
for filename in os.listdir(foldername):
try:
self.im_name = filename
im = self.file_load(foldername+filename)
im = self.pre_process(im)
_, _, matte = BGRemove.modnet(im, inference=False)
matte = self.post_process(matte, background)
status = self.save(matte, output, background)
print(status)
except:
print('There is an error for {} file/folder'.format(foldername+filename))
def webcam(self, background=False):
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
width, height = 455, 512
exit_key = ord('q')
while(True):
_, frame_np = cap.read()
frame_np = cv2.resize(frame_np, (width, height), cv2.INTER_AREA)
im = self.pre_process(frame_np)
_, _, matte = BGRemove.modnet(im, inference=False)
processed_image = self.post_process(matte, background)
full_image = np.concatenate((frame_np, processed_image), axis=1)
full_image = np.uint8(cv2.resize(
full_image, (2*width, height), cv2.INTER_AREA))
if cv2.waitKey(exit_key) & 255 == exit_key:
cv2.destroyAllWindows()
break
cv2.imshow('MODNet - WebCam [Press "Q" To Exit]', full_image)
def save(self, matte, output_path='output/', background=False):
name = '.'.join(self.im_name.split('.')[:-1])+'.png'
path = os.path.join(output_path, name)
if background:
try:
matte = cv2.cvtColor(matte, cv2.COLOR_RGB2BGR)
cv2.imwrite(path, matte)
return "Successfully saved {}".format(path), name
except:
return "Error while saving {}".format(path), ''
else:
w, h, _ = matte.shape
png_image = np.zeros((w, h, 4))
png_image[:, :, :3] = matte
png_image[:, :, 3] = self.alpha
png_image = png_image.astype(np.uint8)
try:
png_image = cv2.cvtColor(png_image, cv2.COLOR_RGBA2BGRA)
cv2.imwrite(path, png_image, [
int(cv2.IMWRITE_PNG_COMPRESSION), 9])
return "Successfully saved {}".format(path), name
except:
return "Error while saving {}".format(path), ''