Spaces:
Sleeping
Sleeping
File size: 1,897 Bytes
5b600b5 f830061 |
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 |
import gradio as gr
from PIL import Image
import torch
import numpy as np
import sys
import os
# Add the `scripts/` folder to the system path
sys.path.append(os.path.join(os.path.dirname(__file__), 'scripts'))
import RRDBNet_arch as arch
# Path to the pretrained model
model_path = os.path.join(os.path.dirname(__file__), 'models', 'RRDB_ESRGAN_x4.pth')
# Load ESRGAN model
device = torch.device('cpu') #'cuda' if using GPU
model = arch.RRDBNet(3, 3, 64, 23, gc=32)
model.load_state_dict(torch.load(model_path, map_location=device), strict=True)
model.eval()
model = model.to(device)
def upscale_image(image):
img = np.array(image) / 255.0
img = torch.from_numpy(np.transpose(img[:, :, [2, 1, 0]], (2, 0, 1))).float()
img_LR = img.unsqueeze(0).to(device)
with torch.no_grad():
output = model(img_LR).data.squeeze().float().cpu().clamp_(0, 1).numpy()
output = np.transpose(output[[2, 1, 0], :, :], (1, 2, 0))
output = (output * 255.0).round().astype(np.uint8)
return Image.fromarray(output)
# Gradio interface
def gradio_interface(image):
try:
if image is None:
raise ValueError("No image uploaded. Please upload an image to upscale.")
upscaled_image = upscale_image(image)
original_size = image.size
upscaled_size = upscaled_image.size
return image, upscaled_image, f"Original Size: {original_size[0]}x{original_size[1]}", f"Upscaled Size: {upscaled_size[0]}x{upscaled_size[1]}"
except Exception as e:
return None, None, "Error", str(e)
gr_interface = gr.Interface(
fn=gradio_interface,
inputs=gr.Image(type="pil"),
outputs=[gr.Image(), gr.Image(), gr.Text(), gr.Text()],
title="ESRGAN Image Upscaler",
description="Upload an image to upscale it using ESRGAN."
)
if __name__ == '__main__':
gr_interface.launch(share=True)
|