ShahzebKhoso commited on
Commit
7fc0cca
1 Parent(s): 1f48939

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -19
app.py CHANGED
@@ -1,32 +1,43 @@
1
  import gradio as gr
2
  from transformers import AutoImageProcessor, Swin2SRForImageSuperResolution
3
  from PIL import Image
 
4
  import torch
 
5
 
6
- # Load model and processor
7
- processor = AutoImageProcessor.from_pretrained("caidas/swin2SR-lightweight-x4-64")
8
- model = Swin2SRForImageSuperResolution.from_pretrained("caidas/swin2SR-lightweight-x4-64")
9
 
10
- def upscale_image(image):
 
11
  # Preprocess the input image
12
- inputs = processor(images=image, return_tensors="pt")
13
-
14
  # Perform super-resolution
15
  with torch.no_grad():
16
  outputs = model(**inputs)
17
 
18
- # Post-process the output to get a high-resolution image
19
- output_image = processor.postprocess(outputs.logits, target_sizes=[(image.size[1]*4, image.size[0]*4)])[0]
 
 
 
 
 
 
 
 
 
 
 
 
20
  return output_image
21
 
22
- # Gradio interface
23
- iface = gr.Interface(
24
- fn=upscale_image,
25
- inputs=gr.Image(type="pil"),
26
- outputs=gr.Image(type="pil"),
27
- title="Image Super Resolution with Swin2SR",
28
- description="Upload an image and enhance its resolution using the Swin2SR model (4x resolution)."
29
- )
30
-
31
- if __name__ == "__main__":
32
- iface.launch()
 
1
  import gradio as gr
2
  from transformers import AutoImageProcessor, Swin2SRForImageSuperResolution
3
  from PIL import Image
4
+ import numpy as np
5
  import torch
6
+ import requests
7
 
8
+ # Load the model and processor
9
+ processor = AutoImageProcessor.from_pretrained("caidas/swin2SR-realworld-sr-x4-64-bsrgan-psnr")
10
+ model = Swin2SRForImageSuperResolution.from_pretrained("caidas/swin2SR-realworld-sr-x4-64-bsrgan-psnr").to('cuda')
11
 
12
+ # Define the function for super-resolution
13
+ def super_resolve(image):
14
  # Preprocess the input image
15
+ inputs = processor(images=image, return_tensors="pt").to('cuda')
16
+
17
  # Perform super-resolution
18
  with torch.no_grad():
19
  outputs = model(**inputs)
20
 
21
+ # Get the reconstructed tensor from the outputs
22
+ reconstructed_tensor = outputs.reconstruction
23
+
24
+ # Move the tensor to the CPU and convert it to a NumPy array
25
+ image_tensor = reconstructed_tensor.squeeze().cpu()
26
+ image_np = image_tensor.permute(1, 2, 0).numpy() # Permute to make it HxWxC
27
+
28
+ # Rescale the values from [0, 1] to [0, 255]
29
+ image_np = np.clip(image_np, 0, 1)
30
+ image_np = (image_np * 255).astype(np.uint8)
31
+
32
+ # Convert the NumPy array back to an image
33
+ output_image = Image.fromarray(image_np)
34
+
35
  return output_image
36
 
37
+ # Create the Gradio interface
38
+ inputs = gr.inputs.Image(type="pil", label="Upload an Image")
39
+ outputs = gr.outputs.Image(type="pil", label="Super-Resolved Image")
40
+
41
+ gr.Interface(fn=super_resolve, inputs=inputs, outputs=outputs, title="Image Super-Resolution with Swin2SR",
42
+ description="Upload an image to upscale it using the Swin2SR model for real-world super-resolution."
43
+ ).launch()