arxivgpt kim
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -22,32 +22,33 @@ def resize_image(image, model_input_size=(1024, 1024)):
|
|
22 |
image = image.resize(model_input_size, Image.BILINEAR)
|
23 |
return image
|
24 |
|
25 |
-
def process(image
|
26 |
-
#
|
27 |
-
orig_image = Image.fromarray(image.
|
28 |
-
|
29 |
-
|
|
|
|
|
30 |
im_tensor = normalize(im_tensor, [0.5, 0.5, 0.5], [1.0, 1.0, 1.0])
|
31 |
if torch.cuda.is_available():
|
32 |
im_tensor = im_tensor.cuda()
|
33 |
-
|
34 |
-
#
|
35 |
with torch.no_grad():
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
45 |
|
46 |
-
|
47 |
-
if background_image is not None:
|
48 |
-
result_image = merge_images(background_image, result_image)
|
49 |
|
50 |
-
return result_image
|
51 |
|
52 |
def merge_images(background_image, foreground_image):
|
53 |
"""
|
|
|
22 |
image = image.resize(model_input_size, Image.BILINEAR)
|
23 |
return image
|
24 |
|
25 |
+
def process(image):
|
26 |
+
# prepare input
|
27 |
+
orig_image = Image.fromarray(image).convert("RGBA")
|
28 |
+
w, h = orig_im_size = orig_image.size
|
29 |
+
image = resize_image(orig_image)
|
30 |
+
im_np = np.array(image)
|
31 |
+
im_tensor = torch.tensor(im_np, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0) / 255.0
|
32 |
im_tensor = normalize(im_tensor, [0.5, 0.5, 0.5], [1.0, 1.0, 1.0])
|
33 |
if torch.cuda.is_available():
|
34 |
im_tensor = im_tensor.cuda()
|
35 |
+
|
36 |
+
# inference
|
37 |
with torch.no_grad():
|
38 |
+
result = net(im_tensor)
|
39 |
+
|
40 |
+
# post process
|
41 |
+
result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode='bilinear', align_corners=False), 0)
|
42 |
+
result = torch.sigmoid(result) # 마스크의 투명도를 결정하는 확률 값으로 변환
|
43 |
+
mask = (result * 255).byte().cpu().numpy() # 마스크를 0~255 사이의 값으로 변환
|
44 |
|
45 |
+
# 마스크를 RGBA 이미지로 변환하고, 마스크의 알파 채널을 사용
|
46 |
+
mask_image = Image.fromarray(mask).convert("L")
|
47 |
+
final_image = Image.new("RGBA", orig_image.size)
|
48 |
+
final_image.paste(orig_image, mask=mask_image)
|
49 |
|
50 |
+
return final_image
|
|
|
|
|
51 |
|
|
|
52 |
|
53 |
def merge_images(background_image, foreground_image):
|
54 |
"""
|