arxivgpt kim
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -25,13 +25,15 @@ def resize_image(image, model_input_size=(1024, 1024)):
|
|
25 |
image = image.resize(model_input_size, Image.BILINEAR)
|
26 |
return image
|
27 |
|
|
|
|
|
28 |
def process(image, background_image=None):
|
29 |
# 이미지 준비
|
30 |
-
orig_image = Image.fromarray(image).convert("
|
31 |
-
w, h =
|
32 |
-
|
33 |
-
im_np = np.array(
|
34 |
-
im_tensor = torch.tensor(im_np
|
35 |
im_tensor = normalize(im_tensor, [0.5, 0.5, 0.5], [1.0, 1.0, 1.0])
|
36 |
if torch.cuda.is_available():
|
37 |
im_tensor = im_tensor.cuda()
|
@@ -39,34 +41,33 @@ def process(image, background_image=None):
|
|
39 |
# 추론
|
40 |
with torch.no_grad():
|
41 |
result = net(im_tensor)
|
42 |
-
|
43 |
# 후처리
|
44 |
result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode='bilinear', align_corners=False), 0)
|
45 |
result = torch.sigmoid(result)
|
46 |
-
mask =
|
47 |
-
|
48 |
-
#
|
|
|
49 |
if mask.ndim > 2:
|
50 |
mask = mask.squeeze()
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
#
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
final_image = Image.new("RGBA", orig_image.size, (0, 0, 0, 0))
|
58 |
-
mask_image = Image.fromarray(mask, mode='L')
|
59 |
-
foreground_image = Image.composite(orig_image, final_image, mask_image)
|
60 |
-
|
61 |
-
# 선택적 배경 이미지 처리
|
62 |
-
if background_image:
|
63 |
-
final_image = merge_images(background_image, foreground_image)
|
64 |
else:
|
65 |
-
final_image =
|
66 |
|
67 |
return final_image
|
68 |
|
69 |
|
|
|
70 |
def merge_images(background_image, foreground_image):
|
71 |
"""
|
72 |
배경 이미지에 배경이 제거된 이미지를 투명하게 삽입합니다.
|
|
|
25 |
image = image.resize(model_input_size, Image.BILINEAR)
|
26 |
return image
|
27 |
|
28 |
+
|
29 |
+
|
30 |
def process(image, background_image=None):
|
31 |
# 이미지 준비
|
32 |
+
orig_image = Image.fromarray(image).convert("RGBA")
|
33 |
+
w, h = orig_image.size
|
34 |
+
resized_image = resize_image(orig_image)
|
35 |
+
im_np = np.array(resized_image).astype(np.float32) / 255.0
|
36 |
+
im_tensor = torch.tensor(im_np).permute(2, 0, 1).unsqueeze(0)
|
37 |
im_tensor = normalize(im_tensor, [0.5, 0.5, 0.5], [1.0, 1.0, 1.0])
|
38 |
if torch.cuda.is_available():
|
39 |
im_tensor = im_tensor.cuda()
|
|
|
41 |
# 추론
|
42 |
with torch.no_grad():
|
43 |
result = net(im_tensor)
|
44 |
+
|
45 |
# 후처리
|
46 |
result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode='bilinear', align_corners=False), 0)
|
47 |
result = torch.sigmoid(result)
|
48 |
+
mask = result.cpu().numpy()
|
49 |
+
|
50 |
+
# 마스크 후처리 및 알파 채널로 변환
|
51 |
+
mask = (mask * 255).astype(np.uint8)
|
52 |
if mask.ndim > 2:
|
53 |
mask = mask.squeeze()
|
54 |
+
|
55 |
+
# 최종 이미지 생성: 마스크를 알파 채널로 사용
|
56 |
+
orig_array = np.array(orig_image)
|
57 |
+
final_array = np.concatenate((orig_array[:, :, :3], mask[:, :, None]), axis=-1)
|
58 |
|
59 |
+
# 선택적 배경 이미지와 합성
|
60 |
+
if background_image is not None:
|
61 |
+
background = background_image.convert("RGBA")
|
62 |
+
foreground = Image.fromarray(final_array, 'RGBA')
|
63 |
+
final_image = merge_images(background, foreground)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
else:
|
65 |
+
final_image = Image.fromarray(final_array, 'RGBA')
|
66 |
|
67 |
return final_image
|
68 |
|
69 |
|
70 |
+
|
71 |
def merge_images(background_image, foreground_image):
|
72 |
"""
|
73 |
배경 이미지에 배경이 제거된 이미지를 투명하게 삽입합니다.
|