Update app.py
Browse files
app.py
CHANGED
|
@@ -27,27 +27,21 @@ def remove_background(image):
|
|
| 27 |
image.save(img_bytes, format="PNG")
|
| 28 |
img_bytes.seek(0)
|
| 29 |
|
| 30 |
-
# Step 1:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# Step 2: Remove background using the Gradio API
|
| 34 |
-
result = background_removal_client.predict(
|
| 35 |
-
base64_image, # Send base64 image
|
| 36 |
api_name="/png"
|
| 37 |
)
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
foreground = Image.open(BytesIO(base64.b64decode(result_data))).convert("RGBA")
|
| 43 |
-
else:
|
| 44 |
-
# Handle unexpected result format (you may want to raise an error or log)
|
| 45 |
-
raise ValueError("Unexpected result format from API")
|
| 46 |
|
| 47 |
# Step 3: Apply `rmbg` for additional cleanup
|
| 48 |
cleaned_foreground = remove(foreground)
|
| 49 |
|
| 50 |
-
return cleaned_foreground.convert("RGB") # Directly return cleaned
|
|
|
|
| 51 |
def process_image(image_path, depth_map_path=None):
|
| 52 |
image_path = Path(image_path)
|
| 53 |
image_raw = Image.open(image_path).convert("RGB")
|
|
|
|
| 27 |
image.save(img_bytes, format="PNG")
|
| 28 |
img_bytes.seek(0)
|
| 29 |
|
| 30 |
+
# Step 1: Remove background using the Gradio API
|
| 31 |
+
result_url = background_removal_client.predict(
|
| 32 |
+
f=img_bytes, # Pass the BytesIO object directly
|
|
|
|
|
|
|
|
|
|
| 33 |
api_name="/png"
|
| 34 |
)
|
| 35 |
|
| 36 |
+
# Step 2: Download the image returned by the API
|
| 37 |
+
response = requests.get(result_url)
|
| 38 |
+
foreground = Image.open(BytesIO(response.content)).convert("RGBA")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# Step 3: Apply `rmbg` for additional cleanup
|
| 41 |
cleaned_foreground = remove(foreground)
|
| 42 |
|
| 43 |
+
return cleaned_foreground.convert("RGB") # Directly return cleaned foregroun
|
| 44 |
+
|
| 45 |
def process_image(image_path, depth_map_path=None):
|
| 46 |
image_path = Path(image_path)
|
| 47 |
image_raw = Image.open(image_path).convert("RGB")
|