Spaces:
Sleeping
Sleeping
First init
Browse files- app.py +57 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
from modelscope.pipelines import pipeline
|
4 |
+
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]
|
12 |
+
|
13 |
+
# 마스크로 사용할 이미지 생성
|
14 |
+
mask_image = Image.new("L", image_pil.size, 0)
|
15 |
+
mask_image.paste(alpha_channel, alpha_channel)
|
16 |
+
|
17 |
+
# new image with background
|
18 |
+
new_image = Image.new("RGBA", image_pil.size, (207,195,181))
|
19 |
+
new_image.paste(image_pil, (0,0), image_pil)
|
20 |
+
new_image = new_image.convert("RGB")
|
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.Image()
|
44 |
+
|
45 |
+
btn_matt.click(
|
46 |
+
fn=matting,
|
47 |
+
inputs=[image],
|
48 |
+
outputs=[result_matt],
|
49 |
+
)
|
50 |
+
|
51 |
+
btn_merge.click(
|
52 |
+
fn=merge,
|
53 |
+
inputs=[org_image, add_image],
|
54 |
+
outputs=[result_merge],
|
55 |
+
)
|
56 |
+
|
57 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow
|
2 |
+
sentencepiece
|
3 |
+
modelscope
|