Update app.py
Browse files
app.py
CHANGED
|
@@ -23,34 +23,35 @@ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
|
| 23 |
background_removal_client = Client("not-lain/background-removal")
|
| 24 |
|
| 25 |
|
|
|
|
| 26 |
def remove_background(image):
|
| 27 |
-
# Create a temporary
|
| 28 |
-
with tempfile.
|
|
|
|
|
|
|
| 29 |
# Convert the PIL image to a byte stream and save it to the temp file
|
| 30 |
-
image.save(
|
| 31 |
-
temp_file_path = temp_file.name # Get the path of the temp file
|
| 32 |
|
| 33 |
-
try:
|
| 34 |
# Step 1: Remove background using the Gradio API
|
| 35 |
-
|
| 36 |
f=temp_file_path, # Pass the path of the temporary file
|
| 37 |
api_name="/png"
|
| 38 |
)
|
| 39 |
|
| 40 |
-
# Step 2:
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Step 3: Apply `rmbg` for additional cleanup
|
| 45 |
cleaned_foreground = remove(foreground)
|
| 46 |
|
| 47 |
return cleaned_foreground.convert("RGB") # Directly return cleaned foreground
|
| 48 |
|
| 49 |
-
finally:
|
| 50 |
-
# Ensure the temporary file is deleted
|
| 51 |
-
if os.path.exists(temp_file_path):
|
| 52 |
-
os.remove(temp_file_path)
|
| 53 |
-
|
| 54 |
def process_image(image_path, depth_map_path=None):
|
| 55 |
image_path = Path(image_path)
|
| 56 |
image_raw = Image.open(image_path).convert("RGB")
|
|
|
|
| 23 |
background_removal_client = Client("not-lain/background-removal")
|
| 24 |
|
| 25 |
|
| 26 |
+
|
| 27 |
def remove_background(image):
|
| 28 |
+
# Create a temporary directory to store temporary files
|
| 29 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 30 |
+
temp_file_path = os.path.join(temp_dir, "temp_image.png")
|
| 31 |
+
|
| 32 |
# Convert the PIL image to a byte stream and save it to the temp file
|
| 33 |
+
image.save(temp_file_path, format="PNG")
|
|
|
|
| 34 |
|
|
|
|
| 35 |
# Step 1: Remove background using the Gradio API
|
| 36 |
+
result_path = background_removal_client.predict(
|
| 37 |
f=temp_file_path, # Pass the path of the temporary file
|
| 38 |
api_name="/png"
|
| 39 |
)
|
| 40 |
|
| 41 |
+
# Step 2: Determine if result_path is a local file path or URL
|
| 42 |
+
if os.path.exists(result_path):
|
| 43 |
+
# If the result is a local file path, open it directly
|
| 44 |
+
foreground = Image.open(result_path).convert("RGBA")
|
| 45 |
+
else:
|
| 46 |
+
# Otherwise, handle it as a URL (assuming it returns a URL)
|
| 47 |
+
response = requests.get(result_path)
|
| 48 |
+
foreground = Image.open(BytesIO(response.content)).convert("RGBA")
|
| 49 |
|
| 50 |
# Step 3: Apply `rmbg` for additional cleanup
|
| 51 |
cleaned_foreground = remove(foreground)
|
| 52 |
|
| 53 |
return cleaned_foreground.convert("RGB") # Directly return cleaned foreground
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def process_image(image_path, depth_map_path=None):
|
| 56 |
image_path = Path(image_path)
|
| 57 |
image_raw = Image.open(image_path).convert("RGB")
|