File size: 1,245 Bytes
56a814d
 
 
 
32179f6
 
 
56a814d
32179f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158eae5
56a814d
32179f6
 
56a814d
 
bb4fab0
32179f6
1c8b166
bb4fab0
 
32179f6
bb4fab0
 
 
56a814d
32179f6
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
import gradio as gr
from rembg import remove
from PIL import Image
import io
import torch
import numpy as np
from RealESRGAN import RealESRGAN

# Initialize RealESRGAN model
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = RealESRGAN(device, scale=4)
model.load_weights('weights/RealESRGAN_x4.pth', download=True)

def background_remover_and_upscale(input_image):
    # Remove background
    output_img = remove(input_image)
    
    # Convert back to PIL Image for upscaling
    output_img_pil = Image.fromarray(output_img)
    
    # Upscale image
    sr_image = model.predict(output_img_pil)
    
    # Return the upscaled image directly
    return sr_image

description = """
This app uses the rembg library to remove the background from uploaded images and then the RealESRGAN model to upscale the image.
Simply upload your image and let the models do their work. Download the result immediately after!
"""

iface = gr.Interface(
    fn=background_remover_and_upscale,
    inputs=gr.Image(type='pil', label="Upload Image"),
    outputs="image",
    examples=["woman.jpg", "groot.jpg"],
    title="Background Remover and Image Upscaler",
    description=description,
    theme="soft"
)

iface.launch(share=True)