fffiloni commited on
Commit
29394a3
1 Parent(s): 79f1d11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -6
app.py CHANGED
@@ -36,7 +36,7 @@ images_examples = [
36
  if os.path.isfile(os.path.join(examples_folder, file))
37
  ]
38
 
39
- def remove_background(input_url):
40
  # Create a temporary folder for downloaded and processed images
41
  temp_dir = tempfile.mkdtemp()
42
 
@@ -49,13 +49,17 @@ def remove_background(input_url):
49
  shutil.rmtree(temp_dir)
50
  return f"Error downloading or saving the image: {str(e)}"
51
 
 
52
  # Run background removal
53
  try:
54
  unique_id = str(uuid.uuid4())
55
  removed_bg_path = os.path.join(temp_dir, f'output_image_rmbg_{unique_id}.png')
56
  img = Image.open(image_path)
57
- result = remove(img)
58
- result.save(removed_bg_path)
 
 
 
59
 
60
  # Remove the input image to keep the temp directory clean
61
  os.remove(image_path)
@@ -99,9 +103,9 @@ def run_inference(temp_dir, removed_bg_path):
99
  except subprocess.CalledProcessError as e:
100
  return f"Error during inference: {str(e)}"
101
 
102
- def process_image(input_url):
103
  # Remove background
104
- result = remove_background(input_url)
105
 
106
  if isinstance(result, str) and result.startswith("Error"):
107
  raise gr.Error(f"{result}") # Return the error message if something went wrong
@@ -150,6 +154,11 @@ def gradio_interface():
150
  type="filepath",
151
  height=240
152
  )
 
 
 
 
 
153
 
154
  submit_button = gr.Button("Process")
155
  gr.Examples(
@@ -160,7 +169,7 @@ def gradio_interface():
160
 
161
  output_video= gr.Video(label="Output Video", scale=4)
162
 
163
- submit_button.click(process_image, inputs=[input_image], outputs=[output_video])
164
 
165
  return app
166
 
 
36
  if os.path.isfile(os.path.join(examples_folder, file))
37
  ]
38
 
39
+ def remove_background(input_url, remove_bg):
40
  # Create a temporary folder for downloaded and processed images
41
  temp_dir = tempfile.mkdtemp()
42
 
 
49
  shutil.rmtree(temp_dir)
50
  return f"Error downloading or saving the image: {str(e)}"
51
 
52
+
53
  # Run background removal
54
  try:
55
  unique_id = str(uuid.uuid4())
56
  removed_bg_path = os.path.join(temp_dir, f'output_image_rmbg_{unique_id}.png')
57
  img = Image.open(image_path)
58
+ if remove_bg:
59
+ result = remove(img)
60
+ result.save(removed_bg_path)
61
+ else:
62
+ img.save(removed_bg_path)
63
 
64
  # Remove the input image to keep the temp directory clean
65
  os.remove(image_path)
 
103
  except subprocess.CalledProcessError as e:
104
  return f"Error during inference: {str(e)}"
105
 
106
+ def process_image(input_url, remove_bg):
107
  # Remove background
108
+ result = remove_background(input_url, remove_bg)
109
 
110
  if isinstance(result, str) and result.startswith("Error"):
111
  raise gr.Error(f"{result}") # Return the error message if something went wrong
 
154
  type="filepath",
155
  height=240
156
  )
157
+
158
+ remove_bg = gr.Checkbox(
159
+ label="Need to remove BG ?",
160
+ value=False
161
+ )
162
 
163
  submit_button = gr.Button("Process")
164
  gr.Examples(
 
169
 
170
  output_video= gr.Video(label="Output Video", scale=4)
171
 
172
+ submit_button.click(process_image, inputs=[input_image, remove_bg], outputs=[output_video])
173
 
174
  return app
175