Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,49 +37,44 @@ images_examples = [
|
|
| 37 |
]
|
| 38 |
|
| 39 |
def remove_background(input_url):
|
|
|
|
| 40 |
# Create a temporary folder for downloaded and processed images
|
| 41 |
temp_dir = tempfile.mkdtemp()
|
| 42 |
|
| 43 |
-
# Download the image from the URL
|
| 44 |
image_path = os.path.join(temp_dir, 'input_image.png')
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
# Ensure the image has an alpha channel
|
| 49 |
-
if image.mode != 'RGBA':
|
| 50 |
-
image = image.convert('RGBA')
|
| 51 |
-
|
| 52 |
-
# Resize the image to a max width of 512 pixels while maintaining aspect ratio
|
| 53 |
-
max_width = 512
|
| 54 |
-
if image.width > max_width:
|
| 55 |
-
aspect_ratio = image.height / image.width
|
| 56 |
-
new_height = int(max_width * aspect_ratio)
|
| 57 |
-
image = image.resize((max_width, new_height), Image.LANCZOS)
|
| 58 |
-
|
| 59 |
-
# Save the resized image
|
| 60 |
-
image.save(image_path)
|
| 61 |
-
except Exception as e:
|
| 62 |
-
shutil.rmtree(temp_dir)
|
| 63 |
-
return f"Error downloading or saving the image: {str(e)}"
|
| 64 |
-
|
| 65 |
|
| 66 |
-
# Run background removal
|
| 67 |
try:
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
# Remove the input image to keep the temp directory clean
|
| 77 |
-
os.remove(image_path)
|
| 78 |
except Exception as e:
|
| 79 |
shutil.rmtree(temp_dir)
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
def run_inference(temp_dir, removed_bg_path):
|
| 85 |
# Define the inference configuration
|
|
@@ -167,7 +162,8 @@ def gradio_interface():
|
|
| 167 |
with gr.Column(scale=2):
|
| 168 |
input_image = gr.Image(
|
| 169 |
label="Image input",
|
| 170 |
-
type="
|
|
|
|
| 171 |
height=240
|
| 172 |
)
|
| 173 |
|
|
|
|
| 37 |
]
|
| 38 |
|
| 39 |
def remove_background(input_url):
|
| 40 |
+
remove_bg = True
|
| 41 |
# Create a temporary folder for downloaded and processed images
|
| 42 |
temp_dir = tempfile.mkdtemp()
|
| 43 |
|
|
|
|
| 44 |
image_path = os.path.join(temp_dir, 'input_image.png')
|
| 45 |
+
unique_id = str(uuid.uuid4())
|
| 46 |
+
removed_bg_path = os.path.join(temp_dir, f'output_image_rmbg_{unique_id}.png')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
|
|
|
| 48 |
try:
|
| 49 |
+
# Check if input_url is already a PIL Image
|
| 50 |
+
if isinstance(input_url, Image.Image):
|
| 51 |
+
image = input_url
|
| 52 |
+
else:
|
| 53 |
+
# Otherwise, assume it's a file path and open it
|
| 54 |
+
image = Image.open(input_url)
|
| 55 |
|
| 56 |
+
# Save the resized image
|
| 57 |
+
image.save(image_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
shutil.rmtree(temp_dir)
|
| 60 |
+
raise gr.Error(f"Error downloading or saving the image: {str(e)}")
|
| 61 |
+
|
| 62 |
+
if remove_bg is True:
|
| 63 |
+
# Run background removal
|
| 64 |
+
try:
|
| 65 |
+
img = Image.open(image_path)
|
| 66 |
+
result = remove(img)
|
| 67 |
+
result.save(removed_bg_path)
|
| 68 |
+
|
| 69 |
+
# Remove the input image to keep the temp directory clean
|
| 70 |
+
os.remove(image_path)
|
| 71 |
+
except Exception as e:
|
| 72 |
+
shutil.rmtree(temp_dir)
|
| 73 |
+
raise gr.Error(f"Error removing background: {str(e)}")
|
| 74 |
+
|
| 75 |
+
return removed_bg_path, temp_dir
|
| 76 |
+
else:
|
| 77 |
+
return image_path, temp_dir
|
| 78 |
|
| 79 |
def run_inference(temp_dir, removed_bg_path):
|
| 80 |
# Define the inference configuration
|
|
|
|
| 162 |
with gr.Column(scale=2):
|
| 163 |
input_image = gr.Image(
|
| 164 |
label="Image input",
|
| 165 |
+
type="pil",
|
| 166 |
+
image_mode="RGBA",
|
| 167 |
height=240
|
| 168 |
)
|
| 169 |
|