Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,218 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
os.system('pip install modelscope')
|
3 |
+
os.system('pip install "modelscope[cv]" -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html')
|
4 |
+
import json
|
5 |
+
from PIL import Image
|
6 |
+
from skimage import io
|
7 |
+
import gradio as gr
|
8 |
+
from modelscope_studio import encode_image, decode_image, call_demo_service
|
9 |
+
|
10 |
+
|
11 |
+
yes, no = "是", "否"
|
12 |
+
|
13 |
+
def get_size(h, w, max_size=720):
|
14 |
+
if min(h, w) > max_size:
|
15 |
+
if h > w:
|
16 |
+
h, w = int(max_size * h / w), max_size
|
17 |
+
else:
|
18 |
+
h, w = max_size, int(max_size * w / h)
|
19 |
+
return h, w
|
20 |
+
|
21 |
+
|
22 |
+
def inference(img: Image, colorization_option: str, image_denoise_option: str, color_enhance_option: str) -> Image:
|
23 |
+
if img is None:
|
24 |
+
return None
|
25 |
+
w, h = img.size
|
26 |
+
h, w = get_size(h, w, 512)
|
27 |
+
img = img.resize((w, h))
|
28 |
+
|
29 |
+
input_url = encode_image(img)
|
30 |
+
res_url = input_url
|
31 |
+
|
32 |
+
# image-denoising (optional)
|
33 |
+
if image_denoise_option == yes:
|
34 |
+
data = {
|
35 |
+
"task": "image-denoising",
|
36 |
+
"inputs": [
|
37 |
+
res_url
|
38 |
+
],
|
39 |
+
"parameters":{},
|
40 |
+
"urlPaths": {
|
41 |
+
"inUrls": [
|
42 |
+
{
|
43 |
+
"value": res_url,
|
44 |
+
"fileType": "png",
|
45 |
+
"type": "image",
|
46 |
+
"displayType": "ImgUploader",
|
47 |
+
"validator": {
|
48 |
+
"accept": "*.jpeg,*.jpg,*.png",
|
49 |
+
"max_resolution": "5000*5000",
|
50 |
+
"max_size": "10m"
|
51 |
+
},
|
52 |
+
"name": "",
|
53 |
+
"title": ""
|
54 |
+
}
|
55 |
+
],
|
56 |
+
"outUrls": [
|
57 |
+
{
|
58 |
+
"outputKey": "output_img",
|
59 |
+
"type": "image"
|
60 |
+
}
|
61 |
+
]
|
62 |
+
}
|
63 |
+
}
|
64 |
+
result = call_demo_service(
|
65 |
+
path='damo', name='cv_nafnet_image-denoise_sidd', data=json.dumps(data))
|
66 |
+
print(f"image-denoising result: {result}")
|
67 |
+
res_url = result['data']['output_img']
|
68 |
+
|
69 |
+
# image-colorization (optional)
|
70 |
+
if colorization_option == yes:
|
71 |
+
data = {
|
72 |
+
"task": "image-colorization",
|
73 |
+
"inputs": [
|
74 |
+
res_url
|
75 |
+
],
|
76 |
+
"parameters":{},
|
77 |
+
"urlPaths": {
|
78 |
+
"inUrls": [
|
79 |
+
{
|
80 |
+
"value": res_url,
|
81 |
+
"fileType": "png",
|
82 |
+
"type": "image",
|
83 |
+
"displayType": "ImgUploader",
|
84 |
+
"validator": {
|
85 |
+
"accept": "*.jpeg,*.jpg,*.png",
|
86 |
+
"max_size": "10m",
|
87 |
+
"max_resolution": "5000*5000",
|
88 |
+
},
|
89 |
+
"name": "",
|
90 |
+
"title": ""
|
91 |
+
}
|
92 |
+
],
|
93 |
+
"outUrls": [
|
94 |
+
{
|
95 |
+
"outputKey": "output_img",
|
96 |
+
"type": "image"
|
97 |
+
}
|
98 |
+
]
|
99 |
+
}
|
100 |
+
}
|
101 |
+
result = call_demo_service(
|
102 |
+
path='damo', name='cv_ddcolor_image-colorization', data=json.dumps(data))
|
103 |
+
print(f"image-colorization result: {result}")
|
104 |
+
res_url = result['data']['output_img']
|
105 |
+
|
106 |
+
|
107 |
+
# image-portrait-enhancement
|
108 |
+
data = {
|
109 |
+
"task": "image-portrait-enhancement",
|
110 |
+
"inputs": [
|
111 |
+
res_url
|
112 |
+
],
|
113 |
+
"parameters":{},
|
114 |
+
"urlPaths": {
|
115 |
+
"inUrls": [
|
116 |
+
{
|
117 |
+
"value": res_url,
|
118 |
+
"fileType": "png",
|
119 |
+
"type": "image",
|
120 |
+
"displayType": "ImgUploader",
|
121 |
+
"validator": {
|
122 |
+
"accept": "*.jpeg,*.jpg,*.png",
|
123 |
+
"max_size": "10M",
|
124 |
+
"max_resolution": "2000*2000",
|
125 |
+
},
|
126 |
+
"name": "",
|
127 |
+
"title": ""
|
128 |
+
}
|
129 |
+
],
|
130 |
+
"outUrls": [
|
131 |
+
{
|
132 |
+
"outputKey": "output_img",
|
133 |
+
"type": "image"
|
134 |
+
}
|
135 |
+
]
|
136 |
+
}
|
137 |
+
}
|
138 |
+
result = call_demo_service(
|
139 |
+
path='damo', name='cv_gpen_image-portrait-enhancement', data=json.dumps(data))
|
140 |
+
print(f"image-portrait-enhancement result: {result}")
|
141 |
+
res_url = result['data']['output_img']
|
142 |
+
|
143 |
+
# image-color-enhancement (optional)
|
144 |
+
if color_enhance_option == yes:
|
145 |
+
data = {
|
146 |
+
"task": "image-color-enhancement",
|
147 |
+
"inputs": [
|
148 |
+
res_url
|
149 |
+
],
|
150 |
+
"parameters":{},
|
151 |
+
"urlPaths": {
|
152 |
+
"inUrls": [
|
153 |
+
{
|
154 |
+
"value": res_url,
|
155 |
+
"fileType": "png",
|
156 |
+
"type": "image",
|
157 |
+
"displayType": "ImgUploader",
|
158 |
+
"validator": {
|
159 |
+
"accept": "*.jpeg,*.jpg,*.png",
|
160 |
+
"max_size": "10m",
|
161 |
+
"max_resolution": "5000*5000",
|
162 |
+
},
|
163 |
+
"name": "",
|
164 |
+
"title": ""
|
165 |
+
}
|
166 |
+
],
|
167 |
+
"outUrls": [
|
168 |
+
{
|
169 |
+
"outputKey": "output_img",
|
170 |
+
"type": "image"
|
171 |
+
}
|
172 |
+
]
|
173 |
+
}
|
174 |
+
}
|
175 |
+
result = call_demo_service(
|
176 |
+
path='damo', name='cv_csrnet_image-color-enhance-models', data=json.dumps(data))
|
177 |
+
print(f"image-color-enhancement result: {result}")
|
178 |
+
res_url = result['data']['output_img']
|
179 |
+
|
180 |
+
|
181 |
+
res_img = decode_image(res_url)
|
182 |
+
|
183 |
+
return res_img
|
184 |
+
|
185 |
+
|
186 |
+
title = "AI老照片修复"
|
187 |
+
description = '''
|
188 |
+
输入一张老照片,点击一键修复,就能获得由AI完成画质增强、智能上色等处理后的彩色照片!还等什么呢?快让相册里的老照片坐上时光机吧~
|
189 |
+
'''
|
190 |
+
examples = [[os.path.dirname(__file__) + './images/input1.jpg'],
|
191 |
+
[os.path.dirname(__file__) + './images/input2.jpg'],
|
192 |
+
[os.path.dirname(__file__) + './images/input3.jpg'],
|
193 |
+
[os.path.dirname(__file__) + './images/input4.jpg'],
|
194 |
+
[os.path.dirname(__file__) + './images/input5.jpg']]
|
195 |
+
|
196 |
+
css_style = "#overview {margin: auto;max-width: 600px; max-height: 400px; width: 100%;}"
|
197 |
+
|
198 |
+
with gr.Blocks(title=title, css=css_style) as demo:
|
199 |
+
gr.HTML('''
|
200 |
+
<div style="text-align: center; max-width: 720px; margin: 0 auto;">
|
201 |
+
<img id="overview" alt="overview" src="https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/public/ModelScope/studio_old_photo_restoration/overview_long.gif" />
|
202 |
+
</div>
|
203 |
+
''')
|
204 |
+
gr.Markdown(description)
|
205 |
+
with gr.Row():
|
206 |
+
with gr.Column(scale=2):
|
207 |
+
img_input = gr.components.Image(label="图片", type="pil")
|
208 |
+
colorization_option = gr.components.Radio(label="重新上色", choices=[yes, no], value=yes)
|
209 |
+
image_denoise_option = gr.components.Radio(label="应用图像去噪(存在细节损失风险)", choices=[yes, no], value=no)
|
210 |
+
color_enhance_option = gr.components.Radio(label="应用色彩增强(存在罕见色调风险)", choices=[yes, no], value=no)
|
211 |
+
btn = gr.Button("一键修复")
|
212 |
+
with gr.Column(scale=3):
|
213 |
+
img_output = gr.components.Image(label="图片", type="pil").style(height=600)
|
214 |
+
inputs = [img_input, colorization_option, image_denoise_option, color_enhance_option]
|
215 |
+
btn.click(fn=inference, inputs=inputs, outputs=img_output)
|
216 |
+
gr.Examples(examples, inputs=img_input)
|
217 |
+
|
218 |
+
demo.launch()
|