arxivgpt kim commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,12 +37,21 @@ def process(image, background_image=None):
|
|
| 37 |
with torch.no_grad():
|
| 38 |
result = net(im_tensor)
|
| 39 |
|
| 40 |
-
|
| 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()
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
final_image = Image.new("RGBA", orig_image.size)
|
| 47 |
final_image.paste(orig_image, mask=mask_image)
|
| 48 |
|
|
@@ -52,6 +61,31 @@ def process(image, background_image=None):
|
|
| 52 |
|
| 53 |
return final_image
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
title = "Background Removal"
|
| 56 |
description = "This is a demo for BRIA RMBG 1.4 using the BRIA RMBG-1.4 image matting model as backbone."
|
| 57 |
|
|
|
|
| 37 |
with torch.no_grad():
|
| 38 |
result = net(im_tensor)
|
| 39 |
|
| 40 |
+
# 후처리
|
| 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 |
+
# mask 배열이 예상대로 2차원인지 확인하고, 아니라면 조정합니다.
|
| 46 |
+
if mask.ndim > 2:
|
| 47 |
+
mask = mask.squeeze() # 차원 축소
|
| 48 |
+
|
| 49 |
+
# mask 배열을 명확히 uint8로 변환합니다.
|
| 50 |
+
mask = mask.astype(np.uint8)
|
| 51 |
+
|
| 52 |
+
# mask를 PIL 이미지로 변환
|
| 53 |
+
mask_image = Image.fromarray(mask, 'L') # 'L' 모드는 그레이스케일 이미지를 나타냅니다.
|
| 54 |
+
|
| 55 |
final_image = Image.new("RGBA", orig_image.size)
|
| 56 |
final_image.paste(orig_image, mask=mask_image)
|
| 57 |
|
|
|
|
| 61 |
|
| 62 |
return final_image
|
| 63 |
|
| 64 |
+
def merge_images(background_image, foreground_image):
|
| 65 |
+
"""
|
| 66 |
+
배경 이미지에 배경이 제거된 이미지를 투명하게 삽입합니다.
|
| 67 |
+
배경이 제거된 이미지는 배경 이미지 중앙에 30% 크기로 축소되어 삽입됩니다.
|
| 68 |
+
"""
|
| 69 |
+
background = background_image.convert("RGBA")
|
| 70 |
+
foreground = foreground_image.convert("RGBA")
|
| 71 |
+
|
| 72 |
+
# 전경 이미지를 배경 이미지의 30% 크기로 조정
|
| 73 |
+
scale_factor = 0.3
|
| 74 |
+
foreground_width = int(background.width * scale_factor)
|
| 75 |
+
foreground_height = int(foreground.height * foreground_width / foreground.width)
|
| 76 |
+
new_size = (foreground_width, foreground_height)
|
| 77 |
+
foreground_resized = foreground.resize(new_size, Image.Resampling.LANCZOS)
|
| 78 |
+
|
| 79 |
+
# 전경 이미지를 배경 이미지의 가운데에 위치시키기 위한 좌표 계산
|
| 80 |
+
x = (background.width - foreground_width) // 2
|
| 81 |
+
y = (background.height - foreground_height) // 2
|
| 82 |
+
|
| 83 |
+
# 배경 이미지 위에 전경 이미지를 붙임
|
| 84 |
+
background.paste(foreground_resized, (x, y), foreground_resized)
|
| 85 |
+
|
| 86 |
+
return background
|
| 87 |
+
|
| 88 |
+
|
| 89 |
title = "Background Removal"
|
| 90 |
description = "This is a demo for BRIA RMBG 1.4 using the BRIA RMBG-1.4 image matting model as backbone."
|
| 91 |
|