littlebird13 commited on
Commit
50ed895
1 Parent(s): 2b1b83a

Update app.py

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