File size: 1,454 Bytes
56a814d 32179f6 27c3c1a 56a814d 32179f6 27c3c1a 32179f6 27c3c1a 32179f6 27c3c1a 32179f6 27c3c1a 158eae5 56a814d bfd179d 32179f6 56a814d bb4fab0 32179f6 27c3c1a bb4fab0 27c3c1a bb4fab0 27c3c1a bb4fab0 56a814d 27c3c1a |
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 |
import gradio as gr
from rembg import remove
from PIL import Image
import io
import torch
import numpy as np
from RealESRGAN import RealESRGAN
import cv2
# 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(np.array(input_image))
# Convert back to PIL Image for upscaling
output_img_rgb = cv2.cvtColor(output_img, cv2.COLOR_BGR2RGB)
output_img_pil = Image.fromarray(output_img_rgb)
# Upscale image
sr_image = model.predict([output_img])
# Return the upscaled image directly
return sr_image[0]
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'),
outputs=[gr.File("image")],
examples=["woman.jpg", "groot.jpg"],
title="Background Remover and Image Upscaler",
description=description,
article="""This demo was created by KVI Kontent using Gradio.""",
allow_flagging=False,
live=True,
layout="horizontal",
theme="soft"
)
iface.launch() |