Update app.py
Browse files
app.py
CHANGED
@@ -2,26 +2,41 @@ import gradio as gr
|
|
2 |
from rembg import remove
|
3 |
from PIL import Image
|
4 |
import io
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
description = """
|
12 |
-
This app uses the rembg library to remove the background from uploaded images.
|
13 |
-
Simply upload your image and let the
|
14 |
"""
|
15 |
|
16 |
iface = gr.Interface(
|
17 |
-
fn=
|
18 |
inputs=gr.Image(type='pil', label="Upload Image"),
|
19 |
outputs="image",
|
20 |
examples=["woman.jpg", "groot.jpg"],
|
21 |
-
title="Background Remover",
|
22 |
description=description,
|
23 |
theme="soft"
|
24 |
)
|
25 |
|
26 |
-
|
27 |
-
iface.launch(share=True)
|
|
|
2 |
from rembg import remove
|
3 |
from PIL import Image
|
4 |
import io
|
5 |
+
import torch
|
6 |
+
import numpy as np
|
7 |
+
from RealESRGAN import RealESRGAN
|
8 |
|
9 |
+
# Initialize RealESRGAN model
|
10 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
11 |
+
model = RealESRGAN(device, scale=4)
|
12 |
+
model.load_weights('weights/RealESRGAN_x4.pth', download=True)
|
13 |
+
|
14 |
+
def background_remover_and_upscale(input_image):
|
15 |
+
# Remove background
|
16 |
+
output_img = remove(input_image)
|
17 |
+
|
18 |
+
# Convert back to PIL Image for upscaling
|
19 |
+
output_img_pil = Image.fromarray(output_img)
|
20 |
+
|
21 |
+
# Upscale image
|
22 |
+
sr_image = model.predict(output_img_pil)
|
23 |
+
|
24 |
+
# Return the upscaled image directly
|
25 |
+
return sr_image
|
26 |
|
27 |
description = """
|
28 |
+
This app uses the rembg library to remove the background from uploaded images and then the RealESRGAN model to upscale the image.
|
29 |
+
Simply upload your image and let the models do their work. Download the result immediately after!
|
30 |
"""
|
31 |
|
32 |
iface = gr.Interface(
|
33 |
+
fn=background_remover_and_upscale,
|
34 |
inputs=gr.Image(type='pil', label="Upload Image"),
|
35 |
outputs="image",
|
36 |
examples=["woman.jpg", "groot.jpg"],
|
37 |
+
title="Background Remover and Image Upscaler",
|
38 |
description=description,
|
39 |
theme="soft"
|
40 |
)
|
41 |
|
42 |
+
iface.launch(share=True)
|
|