arxivgpt kim
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -26,10 +26,9 @@ def resize_image(image, model_input_size=(1024, 1024)):
|
|
26 |
return image
|
27 |
|
28 |
|
29 |
-
|
30 |
def process(image, background_image=None):
|
31 |
# 이미지 준비
|
32 |
-
orig_image = Image.fromarray(image).convert("
|
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
|
@@ -45,24 +44,25 @@ def process(image, background_image=None):
|
|
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 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
64 |
else:
|
65 |
-
|
|
|
66 |
|
67 |
return final_image
|
68 |
|
|
|
26 |
return image
|
27 |
|
28 |
|
|
|
29 |
def process(image, background_image=None):
|
30 |
# 이미지 준비
|
31 |
+
orig_image = Image.fromarray(image).convert("RGB")
|
32 |
w, h = orig_image.size
|
33 |
resized_image = resize_image(orig_image)
|
34 |
im_np = np.array(resized_image).astype(np.float32) / 255.0
|
|
|
44 |
# 후처리
|
45 |
result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode='bilinear', align_corners=False), 0)
|
46 |
result = torch.sigmoid(result)
|
47 |
+
mask = (result * 255).byte().cpu().numpy()
|
48 |
+
|
|
|
|
|
49 |
if mask.ndim > 2:
|
50 |
mask = mask.squeeze()
|
51 |
+
|
52 |
+
mask = mask.astype(np.uint8)
|
53 |
+
|
54 |
+
# 마스크를 알파 채널로 사용하여 최종 이미지 생성
|
55 |
+
final_image = Image.new("RGBA", orig_image.size)
|
56 |
+
orig_image.putalpha(Image.fromarray(mask, 'L'))
|
57 |
+
|
58 |
+
if background_image:
|
59 |
+
# 배경 이미지가 제공된 경우, 배경 이미지 크기 조정
|
60 |
+
background = background_image.convert("RGBA").resize(orig_image.size)
|
61 |
+
# 배경과 전경(알파 적용된 원본 이미지) 합성
|
62 |
+
final_image = Image.alpha_composite(background, orig_image)
|
63 |
else:
|
64 |
+
# 배경 이미지가 없는 경우, 투명도가 적용된 원본 이미지를 최종 이미지로 사용
|
65 |
+
final_image = orig_image
|
66 |
|
67 |
return final_image
|
68 |
|