Spaces:
Runtime error
Runtime error
File size: 1,743 Bytes
2e2b33c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import gradio as gr
from PIL import Image
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
portrait_matting = pipeline(Tasks.portrait_matting, model='damo/cv_unet_image-matting')
def matting(image):
result = portrait_matting(image)
image_pil = Image.fromarray(result['output_img'])
alpha_channel = image_pil.split()[3]
# 마스크로 사용할 이미지 생성
mask_image = Image.new("L", image_pil.size, 0)
mask_image.paste(alpha_channel, alpha_channel)
# new image with background
new_image = Image.new("RGBA", image_pil.size, (207,195,181))
new_image.paste(image_pil, (0,0), image_pil)
new_image = new_image.convert("RGB")
return [mask_image, new_image]
def merge(org_image, add_image):
add_image = add_image.resize(org_image.size)
org_image.paste(add_image, (0,0), add_image)
return org_image
with gr.Blocks() as demo:
with gr.Tab(label="Portrait Matting"):
with gr.Row():
with gr.Column():
image = gr.Image()
btn_matt= gr.Button()
result_matt = gr.Gallery()
with gr.Tab(label="Merge"):
with gr.Row():
with gr.Column():
with gr.Row():
org_image = gr.Image(label="background", type='pil', image_mode='RGBA')
add_image = gr.Image(label="foreground", type='pil', image_mode='RGBA')
btn_merge = gr.Button()
result_merge = gr.Image()
btn_matt.click(
fn=matting,
inputs=[image],
outputs=[result_matt],
)
btn_merge.click(
fn=merge,
inputs=[org_image, add_image],
outputs=[result_merge],
)
demo.launch()
|