Kvikontent commited on
Commit
5187cb0
·
verified ·
1 Parent(s): 8dd6bdd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -29
app.py CHANGED
@@ -2,44 +2,27 @@ import gradio as gr
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
- import cv2
9
 
10
- # Initialize RealESRGAN model
11
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
12
- model = RealESRGAN(device, scale=4)
13
- model.load_weights('weights/RealESRGAN_x4.pth', download=True)
14
-
15
- def background_remover_and_upscale(input_image):
16
- # Remove background
17
- output_img = remove(np.array(input_image))
18
-
19
- # Convert back to PIL Image for upscaling
20
- output_img_rgb = cv2.cvtColor(output_img, cv2.COLOR_BGR2RGB)
21
- output_img_pil = Image.fromarray(output_img_rgb)
22
-
23
- # Upscale image
24
- sr_image = model.predict([output_img])
25
-
26
- # Return the upscaled image directly
27
- return sr_image[0]
28
 
29
  description = """
30
- This app uses the rembg library to remove the background from uploaded images and then the RealESRGAN model to upscale the image.
31
- Simply upload your image and let the models do their work. Download the result immediately after!
32
  """
33
 
34
  iface = gr.Interface(
35
- fn=background_remover_and_upscale,
36
- inputs=gr.Image(type='pil'),
37
  outputs="image",
38
  examples=["woman.jpg", "groot.jpg"],
39
- title="Background Remover and Image Upscaler",
40
  description=description,
41
- article="""This demo was created by KVI Kontent using Gradio.""",
42
  theme="soft"
43
  )
44
 
45
- iface.launch()
 
 
 
2
  from rembg import remove
3
  from PIL import Image
4
  import io
 
 
 
 
5
 
6
+ def background_remover(input_image):
7
+ output_img = remove(input_image) # Remove the background using the rembg library
8
+ # No need to convert to BytesIO, just return the PIL Image object
9
+ return output_img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 model do its work. Download the result immediately after!
14
  """
15
 
16
  iface = gr.Interface(
17
+ fn=background_remover,
18
+ inputs=gr.inputs.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
+ # Launch the interface
28
+ iface.launch(share=True)