Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import shutil
|
3 |
+
import torch
|
4 |
+
from natsort import natsorted
|
5 |
+
from glob import glob
|
6 |
+
import cv2
|
7 |
+
import numpy as np
|
8 |
+
import gradio as gr
|
9 |
+
from PIL import Image
|
10 |
+
|
11 |
+
os.system("pip install einops")
|
12 |
+
os.system("git clone https://github.com/swz30/Restormer.git")
|
13 |
+
os.chdir('Restormer')
|
14 |
+
|
15 |
+
# Download pretrained models
|
16 |
+
os.system("wget https://github.com/swz30/Restormer/releases/download/v1.0/real_denoising.pth -P Denoising/pretrained_models")
|
17 |
+
os.system("wget https://github.com/swz30/Restormer/releases/download/v1.0/single_image_defocus_deblurring.pth -P Defocus_Deblurring/pretrained_models")
|
18 |
+
os.system("wget https://github.com/swz30/Restormer/releases/download/v1.0/motion_deblurring.pth -P Motion_Deblurring/pretrained_models")
|
19 |
+
os.system("wget https://github.com/swz30/Restormer/releases/download/v1.0/deraining.pth -P Deraining/pretrained_models")
|
20 |
+
|
21 |
+
# Download sample images
|
22 |
+
os.system("wget https://github.com/swz30/Restormer/releases/download/v1.0/sample_images.zip -P demo")
|
23 |
+
shutil.unpack_archive('demo/sample_images.zip', 'demo/')
|
24 |
+
os.remove('demo/sample_images.zip')
|
25 |
+
|
26 |
+
|
27 |
+
examples = [['demo/sample_images/Real_Denoising/degraded/117355.png', 'Denoising'],
|
28 |
+
['demo/sample_images/Single_Image_Defocus_Deblurring/degraded/engagement.jpg', 'Defocus Deblurring'],
|
29 |
+
['demo/sample_images/Motion_Deblurring/degraded/GoPro-GOPR0854_11_00-000090-input.jpg','Motion Deblurring'],
|
30 |
+
['demo/sample_images/Deraining/degraded/Rain100H-77-input.jpg','Deraining']]
|
31 |
+
|
32 |
+
|
33 |
+
title = "Restormer"
|
34 |
+
description = """
|
35 |
+
Gradio demo for Restormer: Efficient Transformer for High-Resolution Image Restoration, CVPR 2022--ORAL. <a href='https://arxiv.org/abs/2111.09881'>[Paper]</a><a href='https://github.com/swz30/Restormer'>[Github Code]</a>\n
|
36 |
+
With Restormer, you can perform: (1) Image Denoising, (2) Defocus Deblurring, (3) Motion Deblurring, and (4) Image Deraining.
|
37 |
+
To use it, simply upload your own image, or click one of the examples provided below.
|
38 |
+
"""
|
39 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2111.09881'>Restormer: Efficient Transformer for High-Resolution Image Restoration </a> | <a href='https://github.com/swz30/Restormer'>Github Repo</a></p>"
|
40 |
+
|
41 |
+
|
42 |
+
def inference(img,task):
|
43 |
+
os.system('mkdir temp')
|
44 |
+
max_res = 904
|
45 |
+
width, height = img.size
|
46 |
+
if max(width,height) > max_res:
|
47 |
+
scale = min(width,height)/max(width,height)
|
48 |
+
if width > max_res:
|
49 |
+
width = max_res
|
50 |
+
height = int(scale*max_res)
|
51 |
+
if height > max_res:
|
52 |
+
height = max_res
|
53 |
+
width = int(scale*max_res)
|
54 |
+
img = img.resize((width,height), Image.ANTIALIAS)
|
55 |
+
|
56 |
+
img.save("temp/image.jpg", "JPEG")
|
57 |
+
|
58 |
+
if task == 'Motion Deblurring':
|
59 |
+
task = 'Motion_Deblurring'
|
60 |
+
os.system("python demo.py --task 'Motion_Deblurring' --input_dir './temp/image.jpg' --result_dir './temp/'")
|
61 |
+
|
62 |
+
if task == 'Defocus Deblurring':
|
63 |
+
task = 'Single_Image_Defocus_Deblurring'
|
64 |
+
os.system("python demo.py --task 'Single_Image_Defocus_Deblurring' --input_dir './temp/image.jpg' --result_dir './temp/'")
|
65 |
+
|
66 |
+
if task == 'Denoising':
|
67 |
+
task = 'Real_Denoising'
|
68 |
+
os.system("python demo.py --task 'Real_Denoising' --input_dir './temp/image.jpg' --result_dir './temp/'")
|
69 |
+
|
70 |
+
if task == 'Deraining':
|
71 |
+
os.system("python demo.py --task 'Deraining' --input_dir './temp/image.jpg' --result_dir './temp/'")
|
72 |
+
|
73 |
+
|
74 |
+
restored = f'temp/{task}/image.png'
|
75 |
+
return restored
|
76 |
+
|
77 |
+
gr.Interface(
|
78 |
+
inference,
|
79 |
+
[
|
80 |
+
gr.inputs.Image(type="pil", label="Input"),
|
81 |
+
gr.inputs.Radio(["Denoising", "Defocus Deblurring", "Motion Deblurring", "Deraining"], default="Denoising", label='task type')
|
82 |
+
],
|
83 |
+
gr.outputs.Image(type="file", label="Output"),
|
84 |
+
title=title,
|
85 |
+
description=description,
|
86 |
+
article=article,
|
87 |
+
examples=examples,
|
88 |
+
allow_flagging=False,
|
89 |
+
).launch(debug=True,enable_queue=True)
|