arxivgpt kim
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -23,7 +23,7 @@ def resize_image(image, model_input_size=(1024, 1024)):
|
|
23 |
return image
|
24 |
|
25 |
def process(image, background_image=None):
|
26 |
-
#
|
27 |
orig_image = Image.fromarray(image).convert("RGBA")
|
28 |
w, h = orig_im_size = orig_image.size
|
29 |
image = resize_image(orig_image)
|
@@ -33,54 +33,38 @@ def process(image, background_image=None):
|
|
33 |
if torch.cuda.is_available():
|
34 |
im_tensor = im_tensor.cuda()
|
35 |
|
36 |
-
#
|
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 |
-
# 마스크를 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 |
-
|
|
|
|
|
51 |
|
|
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
배경 이미지에 배경이 제거된 이미지를 투명하게 삽입합니다.
|
56 |
-
배경이 제거된 이미지는 배경 이미지 중앙에 30% 크기로 축소되어 삽입됩니다.
|
57 |
-
"""
|
58 |
-
# 이미 RGBA 모드로 변환된 이미지를 직접 사용합니다.
|
59 |
-
background = background_image.convert("RGBA")
|
60 |
-
foreground = foreground_image.convert("RGBA")
|
61 |
-
|
62 |
-
# 전경 이미지를 배경 이미지의 30% 크기로 조정
|
63 |
-
scale_factor = 0.3
|
64 |
-
new_size = (int(background.width * scale_factor), int(foreground.height * background.width / foreground.width * scale_factor))
|
65 |
-
foreground_resized = foreground.resize(new_size, Image.Resampling.LANCZOS)
|
66 |
-
|
67 |
-
# 전경 이미지를 배경 이미지의 가운데에 위치시키기 위한 좌표 계산
|
68 |
-
x = (background.width - new_size[0]) // 2
|
69 |
-
y = (background.height - new_size[1]) // 2
|
70 |
-
|
71 |
-
# 배경 이미지 위에 전경 이미지를 붙임
|
72 |
-
background.paste(foreground_resized, (x, y), foreground_resized)
|
73 |
-
|
74 |
-
return background
|
75 |
-
|
76 |
|
77 |
demo = gr.Interface(
|
78 |
fn=process,
|
79 |
inputs=[
|
80 |
-
gr.Image(type="numpy", label="Image to remove background"),
|
81 |
-
gr.Image(type="pil", label="Optional: Background Image"
|
82 |
],
|
83 |
outputs="image",
|
84 |
title=title,
|
85 |
description=description
|
86 |
)
|
|
|
|
|
|
|
|
23 |
return image
|
24 |
|
25 |
def process(image, background_image=None):
|
26 |
+
# 이미지 준비
|
27 |
orig_image = Image.fromarray(image).convert("RGBA")
|
28 |
w, h = orig_im_size = orig_image.size
|
29 |
image = resize_image(orig_image)
|
|
|
33 |
if torch.cuda.is_available():
|
34 |
im_tensor = im_tensor.cuda()
|
35 |
|
36 |
+
# 추론
|
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 |
mask_image = Image.fromarray(mask).convert("L")
|
46 |
final_image = Image.new("RGBA", orig_image.size)
|
47 |
final_image.paste(orig_image, mask=mask_image)
|
48 |
|
49 |
+
# 선택적 배경 이미지 처리
|
50 |
+
if background_image is not None:
|
51 |
+
final_image = merge_images(background_image, final_image)
|
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 |
|
58 |
demo = gr.Interface(
|
59 |
fn=process,
|
60 |
inputs=[
|
61 |
+
gr.Image(type="numpy", label="Image to remove background"),
|
62 |
+
gr.Image(type="pil", label="Optional: Background Image") # 'optional=True'는 Gradio 버전에 따라 필요 없을 수 있음
|
63 |
],
|
64 |
outputs="image",
|
65 |
title=title,
|
66 |
description=description
|
67 |
)
|
68 |
+
|
69 |
+
if __name__ == "__main__":
|
70 |
+
demo.launch()
|