File size: 3,219 Bytes
a26597f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import gradio as gr
from random import randint
from pathlib import Path
from super_image import ImageLoader, EdsrModel, MsrnModel, MdsrModel, AwsrnModel, A2nModel, CarnModel, PanModel, \
    HanModel, DrlnModel, RcanModel

title = "super-image"
description = "State of the Art Image Super-Resolution Models."
article = "<p style='text-align: center'><a href='https://github.com/eugenesiow/super-image'>Github Repo</a>" \
          "| <a href='https://eugenesiow.github.io/super-image/'>Documentation</a> " \
          "| <a href='https://github.com/eugenesiow/super-image#scale-x2'>Models</a></p>"


def inference(img, scale_str, model_name):
    _id = randint(1, 1000)
    output_dir = Path('./tmp/')
    output_dir.mkdir(parents=True, exist_ok=True)
    output_file = output_dir / ('output_image' + str(_id) + '.jpg')
    scale = int(scale_str.replace('x', ''))
    if model_name == 'EDSR':
        model = EdsrModel.from_pretrained('eugenesiow/edsr', scale=scale)
    elif model_name == 'MSRN':
        model = MsrnModel.from_pretrained('eugenesiow/msrn', scale=scale)
    elif model_name == 'MDSR':
        model = MdsrModel.from_pretrained('eugenesiow/mdsr', scale=scale)
    elif model_name == 'AWSRN-BAM':
        model = AwsrnModel.from_pretrained('eugenesiow/awsrn-bam', scale=scale)
    elif model_name == 'A2N':
        model = A2nModel.from_pretrained('eugenesiow/a2n', scale=scale)
    elif model_name == 'CARN':
        model = CarnModel.from_pretrained('eugenesiow/carn', scale=scale)
    elif model_name == 'PAN':
        model = PanModel.from_pretrained('eugenesiow/pan', scale=scale)
    elif model_name == 'HAN':
        model = HanModel.from_pretrained('eugenesiow/han', scale=scale)
    elif model_name == 'DRLN':
        model = DrlnModel.from_pretrained('eugenesiow/drln', scale=scale)
    elif model_name == 'RCAN':
        model = RcanModel.from_pretrained('eugenesiow/rcan', scale=scale)
    else:
        model = EdsrModel.from_pretrained('eugenesiow/edsr-base', scale=scale)
    inputs = ImageLoader.load_image(img)
    preds = model(inputs)
    output_file_str = str(output_file.resolve())
    ImageLoader.save_image(preds, output_file_str)
    return output_file_str


torch.hub.download_url_to_file('http://people.rennes.inria.fr/Aline.Roumy/results/images_SR_BMVC12/input_groundtruth/baby_mini_d3_gaussian.bmp',
                               'baby.bmp')
torch.hub.download_url_to_file('http://people.rennes.inria.fr/Aline.Roumy/results/images_SR_BMVC12/input_groundtruth/woman_mini_d3_gaussian.bmp',
                               'woman.bmp')

gr.Interface(
    inference,
    [
        gr.inputs.Image(type="pil", label="Input"),
        gr.inputs.Radio(["x2", "x3", "x4"], label='scale'),
        gr.inputs.Dropdown(choices=['EDSR-base', 'EDSR', 'MSRN', 'MDSR', 'AWSRN-BAM', 'A2N', 'CARN', 'PAN', 'HAN',
                                    'DRLN', 'RCAN'],
                           label='Model')
    ],
    gr.outputs.Image(type="file", label="Output"),
    title=title,
    description=description,
    article=article,
    examples=[
        ['baby.bmp', 'x2', 'EDSR-base'],
        ['woman.bmp', 'x3', 'MSRN']
    ],
    enable_queue=True
    ).launch(debug=True)