File size: 2,096 Bytes
74a11a6
 
 
7fc0cca
74a11a6
7fc0cca
74a11a6
7fc0cca
 
988617d
74a11a6
7fc0cca
 
74a11a6
988617d
7fc0cca
74a11a6
 
 
 
7fc0cca
 
 
 
 
 
 
 
 
 
 
 
 
 
74a11a6
 
7fc0cca
33ec714
 
c2d9412
 
 
 
 
 
 
 
 
 
 
7fc0cca
 
c2d9412
7fc0cca
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
import gradio as gr
from transformers import AutoImageProcessor, Swin2SRForImageSuperResolution
from PIL import Image
import numpy as np
import torch
import requests

# Load the model and processor
processor = AutoImageProcessor.from_pretrained("caidas/swin2SR-realworld-sr-x4-64-bsrgan-psnr")
model = Swin2SRForImageSuperResolution.from_pretrained("caidas/swin2SR-realworld-sr-x4-64-bsrgan-psnr").to('cpu')

# Define the function for super-resolution
def super_resolve(image):
    # Preprocess the input image
    inputs = processor(images=image, return_tensors="pt").to('cpu')

    # Perform super-resolution
    with torch.no_grad():
        outputs = model(**inputs)
    
    # Get the reconstructed tensor from the outputs
    reconstructed_tensor = outputs.reconstruction

    # Move the tensor to the CPU and convert it to a NumPy array
    image_tensor = reconstructed_tensor.squeeze().cpu()
    image_np = image_tensor.permute(1, 2, 0).numpy()  # Permute to make it HxWxC

    # Rescale the values from [0, 1] to [0, 255]
    image_np = np.clip(image_np, 0, 1)
    image_np = (image_np * 255).astype(np.uint8)

    # Convert the NumPy array back to an image
    output_image = Image.fromarray(image_np)

    return output_image

# Create the Gradio interface
inputs = gr.Image(type="pil", label="Upload an Image")
outputs = gr.Image(type="pil", label="Super-Resolved Image")
dsc = """
Upload an image to generate a high-resolution version using the SeemoRe model.

**Citations:**

1. Marcos V. Conde, Ui-Jin Choi, Maxime Burchi, Radu Timofte, *Swin2SR: SwinV2 Transformer for Compressed Image Super-Resolution and Restoration*, Proceedings of the European Conference on Computer Vision (ECCV) Workshops, 2022.

2. Jingyun Liang, Jiezhang Cao, Guolei Sun, Kai Zhang, Luc Van Gool, Radu Timofte, *SwinIR: Image Restoration Using Swin Transformer*, arXiv preprint arXiv:2108.10257, 2021.

For more details, refer to the original papers.
"""

gr.Interface(fn=super_resolve, inputs=inputs, outputs=outputs, title="Image Super-Resolution with Swin2SR",
             description=dsc
).launch()