File size: 3,650 Bytes
f78da30
 
 
 
 
 
 
2b817c1
f78da30
 
c2af834
 
 
f78da30
 
c2af834
 
 
 
f78da30
e84ed63
f78da30
 
 
e84ed63
 
f78da30
e84ed63
 
 
f78da30
 
 
e84ed63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f78da30
dad1674
f78da30
 
6cbfd74
f78da30
 
 
6cbfd74
f78da30
 
 
6cbfd74
f78da30
 
6cbfd74
f78da30
cdef45b
f78da30
 
 
 
 
e84ed63
 
 
f78da30
 
 
 
 
e84ed63
f78da30
 
367abe0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import shutil
import torch
import cv2
import gradio as gr
from PIL import Image

#os.chdir('Restormer')

# Download sample images
os.system("wget https://github.com/swz30/Restormer/releases/download/v1.0/sample_images.zip")
shutil.unpack_archive('sample_images.zip')
os.remove('sample_images.zip')


examples = [['sample_images/Real_Denoising/degraded/117355.png', 'Denoising'],
            ['sample_images/Single_Image_Defocus_Deblurring/degraded/engagement.jpg', 'Defocus Deblurring'],
            ['sample_images/Motion_Deblurring/degraded/GoPro-GOPR0854_11_00-000090-input.jpg','Motion Deblurring'],
            ['sample_images/Deraining/degraded/Rain100H-77-input.jpg','Deraining']]

inference_on = ['Full Resolution Image', 'Downsampled Image']

title = "Restormer"
description = """
Gradio demo for <b>Restormer: Efficient Transformer for High-Resolution Image Restoration</b>, CVPR 2022--ORAL. <a href='https://arxiv.org/abs/2111.09881'>[Paper]</a><a href='https://github.com/swz30/Restormer'>[Github Code]</a>\n 
<b> Note:</b> Since this demo uses CPU, by default it will run on the downsampled version of the input image (for speedup). But if you want to perform inference on the original input, then choose the option "Full Resolution Image" from the dropdown menu. 
"""
##With Restormer, you can perform: (1) Image Denoising, (2) Defocus Deblurring, (3)  Motion Deblurring, and (4) Image Deraining. 
##To use it, simply upload your own image, or click one of the examples provided below.

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>"


def inference(img, task, run_on):
    if not os.path.exists('temp'):
      os.system('mkdir temp')
      
    if run_on == 'Full Resolution Image':
      img = img
    else: # 'Downsampled Image'
    ####  Resize the longer edge of the input image
      max_res = 512
      width, height = img.size
      if max(width,height) > max_res:
        scale = max_res /max(width,height)
        width = int(scale*width)
        height = int(scale*height)
        img = img.resize((width,height), Image.ANTIALIAS)
      
    img.save("temp/image.jpg", "JPEG")

    if task == 'Motion Deblurring':
      task = 'Motion_Deblurring'
      os.system("python demo_gradio.py --task 'Motion_Deblurring' --input_path 'temp/image.jpg' --result_dir './temp/'")
  
    if task == 'Defocus Deblurring':
      task = 'Single_Image_Defocus_Deblurring'
      os.system("python demo_gradio.py --task 'Single_Image_Defocus_Deblurring' --input_path 'temp/image.jpg' --result_dir './temp/'")
  
    if task == 'Denoising':
      task = 'Real_Denoising'
      os.system("python demo_gradio.py --task 'Real_Denoising' --input_path 'temp/image.jpg' --result_dir './temp/'")
  
    if task == 'Deraining':
      os.system("python demo_gradio.py --task 'Deraining' --input_path 'temp/image.jpg' --result_dir './temp/'")
  
    return f'temp/{task}/image.jpg'
    
gr.Interface(
    inference,
    [
        gr.inputs.Image(type="pil", label="Input"),
        gr.inputs.Radio(["Denoising", "Defocus Deblurring", "Motion Deblurring", "Deraining"], default="Denoising", label='task'),
        gr.inputs.Dropdown(choices=inference_on, type="value", default='Downsampled Image', label='Inference on')

    ],
    gr.outputs.Image(type="file", label="Output"),
    title=title,
    description=description,
    article=article,
    theme ="huggingface",
    examples=examples,
    allow_flagging=False,
    ).launch(debug=False,enable_queue=True)