Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
from modelscope_studio import encode_image, decode_image, call_demo_service
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
from skimage import io
|
7 |
+
|
8 |
+
style_dict = {"日漫风":"anime", "3D风":"3d", "手绘风":"handdrawn", "素描风":"sketch", "艺术效果":"artstyle"}
|
9 |
+
|
10 |
+
def get_size(h, w, max = 720):
|
11 |
+
if min(h, w) > max:
|
12 |
+
if h > w:
|
13 |
+
h, w = int(max * h / w), max
|
14 |
+
else:
|
15 |
+
h, w = max, int(max * w / h)
|
16 |
+
return h, w
|
17 |
+
|
18 |
+
def inference(image: Image, style: str) -> Image:
|
19 |
+
|
20 |
+
style = style_dict[style]
|
21 |
+
|
22 |
+
w, h = image.size
|
23 |
+
h, w = get_size(h, w, 720)
|
24 |
+
|
25 |
+
image = image.resize((w, h))
|
26 |
+
|
27 |
+
input_url = encode_image(image)
|
28 |
+
|
29 |
+
data = {
|
30 |
+
"task": "image-portrait-stylization",
|
31 |
+
"inputs": [
|
32 |
+
input_url
|
33 |
+
],
|
34 |
+
"urlPaths": {
|
35 |
+
"inUrls": [
|
36 |
+
{
|
37 |
+
"value": input_url,
|
38 |
+
"fileType": "png",
|
39 |
+
"type": "image",
|
40 |
+
"displayType": "ImgUpload",
|
41 |
+
"displayProps": {
|
42 |
+
"label": {
|
43 |
+
"text": "原图",
|
44 |
+
"style": {
|
45 |
+
"background": "rgba(98,74,255,0.8)",
|
46 |
+
"color": "#fff"
|
47 |
+
}
|
48 |
+
}
|
49 |
+
},
|
50 |
+
"validator": {
|
51 |
+
"max_resolution": "3000*3000",
|
52 |
+
"max_size": "10M"
|
53 |
+
},
|
54 |
+
"name": "",
|
55 |
+
"title": ""
|
56 |
+
}
|
57 |
+
],
|
58 |
+
"outUrls": [
|
59 |
+
{
|
60 |
+
"outputKey": "output_img",
|
61 |
+
"fileType": "png"
|
62 |
+
}
|
63 |
+
]
|
64 |
+
}
|
65 |
+
}
|
66 |
+
if style == "anime":
|
67 |
+
style = ""
|
68 |
+
else:
|
69 |
+
style = "-"+style
|
70 |
+
model_id = 'cv_unet_person-image-cartoon'+style+'_compound-models'
|
71 |
+
result = call_demo_service(path='damo', name=model_id,
|
72 |
+
data=json.dumps(data))
|
73 |
+
print(result)
|
74 |
+
res_url = result['data']['output_img']
|
75 |
+
|
76 |
+
res_img = io.imread(res_url)
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
return res_img
|
81 |
+
|
82 |
+
|
83 |
+
css_style = "#fixed_size_img {height: 240px;} "
|
84 |
+
|
85 |
+
|
86 |
+
title = "AI人像多风格漫画"
|
87 |
+
# description = "输入一张人像照片,并指定希望的风格(如:日漫风、3D风、手绘风、素描风、艺术效果),内置多种风格模型用于生成对应的转换结果。本页面提供了在线体验的服务,欢迎使用!"
|
88 |
+
description = '''
|
89 |
+
街拍,人像,团建照片...随意上传您心仪的照片,选择对应风格(日漫风,3D风,手绘风等等),一键即可转换为不同风格的卡通化图片!多风格的人像模型已经内置好,点点鼠标就可以抢占朋友圈的C位,立刻玩起来吧
|
90 |
+
'''
|
91 |
+
examples = [[os.path.dirname(__file__) + './images/input1.png'], [os.path.dirname(__file__) + './images/input2.png'], [os.path.dirname(__file__) + './images/input3.png'], [os.path.dirname(__file__) + './images/input4.png']]
|
92 |
+
# examples = [[os.path.dirname(__file__) + '/images/input1.png'], [os.path.dirname(__file__) + '/images/input2.png'], [os.path.dirname(__file__) + '/images/input3.png']]
|
93 |
+
|
94 |
+
with gr.Blocks(title=title, css=css_style) as demo:
|
95 |
+
|
96 |
+
gr.HTML('''
|
97 |
+
<div style="text-align: center; max-width: 720px; margin: 0 auto;">
|
98 |
+
<div
|
99 |
+
style="
|
100 |
+
display: inline-flex;
|
101 |
+
align-items: center;
|
102 |
+
gap: 0.8rem;
|
103 |
+
font-size: 1.75rem;
|
104 |
+
"
|
105 |
+
>
|
106 |
+
<h1 style="font-family: PingFangSC; font-weight: 500; line-height: 1.5em; font-size: 32px; margin-bottom: 7px;">
|
107 |
+
AI人像多风格漫画
|
108 |
+
</h1>
|
109 |
+
</div>
|
110 |
+
<img id="overview" alt="overview" src="https://modelscope.oss-cn-beijing.aliyuncs.com/demo/image-cartoon/demo_sin1.gif" />
|
111 |
+
|
112 |
+
</div>
|
113 |
+
''')
|
114 |
+
|
115 |
+
|
116 |
+
gr.Markdown(description)
|
117 |
+
with gr.Row():
|
118 |
+
radio_style = gr.Radio(label="风格选择", choices=["日漫风", "3D风", "手绘风", "素描风", "艺术效果"], value="日漫风")
|
119 |
+
with gr.Row():
|
120 |
+
img_input = gr.Image(type="pil", elem_id="fixed_size_img")
|
121 |
+
img_output = gr.Image(type="pil", elem_id="fixed_size_img")
|
122 |
+
with gr.Row():
|
123 |
+
btn_submit = gr.Button(value="一键生成", elem_id="blue_btn")
|
124 |
+
# btn_clear = gr.Button(value="清除")
|
125 |
+
|
126 |
+
examples = gr.Examples(examples=examples, inputs=[img_input], outputs=img_output)
|
127 |
+
btn_submit.click(inference, inputs=[img_input, radio_style], outputs=img_output)
|
128 |
+
# btn_clear清除画布
|
129 |
+
|
130 |
+
|
131 |
+
demo.launch()
|