Janeka commited on
Commit
b613973
·
verified ·
1 Parent(s): c14759a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -24
app.py CHANGED
@@ -1,42 +1,50 @@
1
  import gradio as gr
 
2
  from PIL import Image
3
  from rembg import remove, new_session
4
- import io
5
 
6
- # Initialize session once (saves memory)
7
- session = new_session("u2net") # Smallest working model
 
 
 
 
 
 
8
 
9
  def remove_background(input_image):
10
  try:
11
- # Convert Gradio input to PIL Image
12
- if isinstance(input_image, dict): # Handle paste/drop
13
- img = Image.open(io.BytesIO(input_image["bytes"]))
14
- else:
15
  img = Image.fromarray(input_image)
 
 
 
 
16
 
17
- # Auto-resize to prevent OOM errors
18
- if max(img.size) > 1024:
19
- img = img.resize((1024, 1024))
20
-
21
- # Process with memory-efficient settings
22
- result = remove(
23
- img,
24
- session=session,
25
- alpha_matting=False, # Disable memory-heavy feature
26
- only_mask=False
27
- )
28
  return result
 
29
  except Exception as e:
30
  print(f"Error: {str(e)}")
31
  return input_image # Return original if fails
32
 
33
- # Updated Gradio interface (compatible with latest version)
34
- with gr.Blocks(title="Free BG Remover") as demo:
35
- gr.Markdown("## 🆓 Free Background Remover (Works on CPU)")
36
 
37
  with gr.Row():
38
- input_img = gr.Image(label="Upload", type="pil", height=400)
39
- output_img = gr.Image(label="Result", type="pil", height=400)
 
 
 
 
 
 
 
 
40
 
41
  gr.Button("Remove Background").click(
42
  remove_background,
@@ -44,4 +52,4 @@ with gr.Blocks(title="Free BG Remover") as demo:
44
  outputs=output_img
45
  )
46
 
47
- demo.launch(show_error=True)
 
1
  import gradio as gr
2
+ import numpy as np
3
  from PIL import Image
4
  from rembg import remove, new_session
 
5
 
6
+ # Initialize session with proper settings to prevent cropping
7
+ session = new_session("u2net")
8
+ bg_removal_kwargs = {
9
+ "alpha_matting": False, # Disable advanced features that cause cropping
10
+ "session": session,
11
+ "only_mask": False,
12
+ "post_process_mask": True # Clean edges without cropping
13
+ }
14
 
15
  def remove_background(input_image):
16
  try:
17
+ # Convert any input to PIL Image
18
+ if isinstance(input_image, np.ndarray):
 
 
19
  img = Image.fromarray(input_image)
20
+ elif isinstance(input_image, dict): # Handle paste/drop
21
+ img = Image.open(input_image["name"])
22
+ else:
23
+ img = input_image
24
 
25
+ # Preserve original size (disable auto-resizing)
26
+ result = remove(img, **bg_removal_kwargs)
 
 
 
 
 
 
 
 
 
27
  return result
28
+
29
  except Exception as e:
30
  print(f"Error: {str(e)}")
31
  return input_image # Return original if fails
32
 
33
+ # Gradio interface with proper image handling
34
+ with gr.Blocks() as demo:
35
+ gr.Markdown("## 🖼️ Background Remover (No Cropping)")
36
 
37
  with gr.Row():
38
+ input_img = gr.Image(
39
+ label="Original",
40
+ type="pil", # Ensures we get PIL Images
41
+ height=400
42
+ )
43
+ output_img = gr.Image(
44
+ label="Result",
45
+ type="pil",
46
+ height=400
47
+ )
48
 
49
  gr.Button("Remove Background").click(
50
  remove_background,
 
52
  outputs=output_img
53
  )
54
 
55
+ demo.launch()