Spaces:
Runtime error
Runtime error
Add background color
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ from modelscope.utils.constant import Tasks
|
|
5 |
|
6 |
portrait_matting = pipeline(Tasks.portrait_matting, model='damo/cv_unet_image-matting')
|
7 |
|
8 |
-
def matting(image):
|
9 |
result = portrait_matting(image)
|
10 |
image_pil = Image.fromarray(result['output_img'])
|
11 |
alpha_channel = image_pil.split()[3]
|
@@ -15,36 +15,42 @@ def matting(image):
|
|
15 |
mask_image.paste(alpha_channel, alpha_channel)
|
16 |
|
17 |
# new image with background
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
21 |
|
22 |
-
return [mask_image, new_image]
|
23 |
|
24 |
def merge(org_image, add_image):
|
25 |
add_image = add_image.resize(org_image.size)
|
26 |
org_image.paste(add_image, (0,0), add_image)
|
27 |
-
return org_image
|
28 |
|
29 |
with gr.Blocks() as demo:
|
30 |
with gr.Tab(label="Portrait Matting"):
|
31 |
with gr.Row():
|
32 |
with gr.Column():
|
33 |
-
image = gr.Image()
|
|
|
|
|
|
|
34 |
btn_matt= gr.Button()
|
35 |
-
result_matt = gr.Gallery()
|
36 |
with gr.Tab(label="Merge"):
|
37 |
with gr.Row():
|
38 |
with gr.Column():
|
39 |
with gr.Row():
|
40 |
-
org_image = gr.Image(label="background", type='pil', image_mode='RGBA')
|
41 |
-
add_image = gr.Image(label="foreground", type='pil', image_mode='RGBA')
|
42 |
btn_merge = gr.Button()
|
43 |
-
result_merge = gr.
|
44 |
-
|
45 |
btn_matt.click(
|
46 |
fn=matting,
|
47 |
-
inputs=[image],
|
48 |
outputs=[result_matt],
|
49 |
)
|
50 |
|
@@ -54,4 +60,4 @@ with gr.Blocks() as demo:
|
|
54 |
outputs=[result_merge],
|
55 |
)
|
56 |
|
57 |
-
demo.launch()
|
|
|
5 |
|
6 |
portrait_matting = pipeline(Tasks.portrait_matting, model='damo/cv_unet_image-matting')
|
7 |
|
8 |
+
def matting(image, use_color, color):
|
9 |
result = portrait_matting(image)
|
10 |
image_pil = Image.fromarray(result['output_img'])
|
11 |
alpha_channel = image_pil.split()[3]
|
|
|
15 |
mask_image.paste(alpha_channel, alpha_channel)
|
16 |
|
17 |
# new image with background
|
18 |
+
if use_color:
|
19 |
+
new_image = Image.new("RGBA", image_pil.size, color)
|
20 |
+
new_image.paste(image_pil, (0,0), image_pil)
|
21 |
+
new_image = new_image.convert("RGB")
|
22 |
+
else:
|
23 |
+
new_image = image_pil
|
24 |
|
25 |
+
return [mask_image, new_image]
|
26 |
|
27 |
def merge(org_image, add_image):
|
28 |
add_image = add_image.resize(org_image.size)
|
29 |
org_image.paste(add_image, (0,0), add_image)
|
30 |
+
return [org_image]
|
31 |
|
32 |
with gr.Blocks() as demo:
|
33 |
with gr.Tab(label="Portrait Matting"):
|
34 |
with gr.Row():
|
35 |
with gr.Column():
|
36 |
+
image = gr.Image(height="40vh")
|
37 |
+
with gr.Row():
|
38 |
+
use_color = gr.Checkbox(label="use background color", value=True, container=False)
|
39 |
+
color = gr.ColorPicker(info="background color",value="#89cff0", container=False)
|
40 |
btn_matt= gr.Button()
|
41 |
+
result_matt = gr.Gallery(height="40vh")
|
42 |
with gr.Tab(label="Merge"):
|
43 |
with gr.Row():
|
44 |
with gr.Column():
|
45 |
with gr.Row():
|
46 |
+
org_image = gr.Image(label="background", type='pil', image_mode='RGBA', height="40vh")
|
47 |
+
add_image = gr.Image(label="foreground", type='pil', image_mode='RGBA', height="40vh")
|
48 |
btn_merge = gr.Button()
|
49 |
+
result_merge = gr.Gallery(height="40vh")
|
50 |
+
|
51 |
btn_matt.click(
|
52 |
fn=matting,
|
53 |
+
inputs=[image, use_color, color],
|
54 |
outputs=[result_matt],
|
55 |
)
|
56 |
|
|
|
60 |
outputs=[result_merge],
|
61 |
)
|
62 |
|
63 |
+
demo.launch()
|