fixing the remove bg
Browse files
app.py
CHANGED
|
@@ -20,7 +20,6 @@ feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
|
| 20 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 21 |
background_removal_client = Client("not-lain/background-removal")
|
| 22 |
|
| 23 |
-
|
| 24 |
def remove_background(image):
|
| 25 |
# Convert the PIL image to a byte stream
|
| 26 |
img_bytes = BytesIO()
|
|
@@ -28,10 +27,7 @@ def remove_background(image):
|
|
| 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,
|
| 33 |
-
api_name="/png"
|
| 34 |
-
)
|
| 35 |
|
| 36 |
# Step 2: Download the image returned by the API
|
| 37 |
response = requests.get(result_url)
|
|
@@ -40,7 +36,7 @@ def remove_background(image):
|
|
| 40 |
# Step 3: Apply `rmbg` for additional cleanup
|
| 41 |
cleaned_foreground = remove(foreground)
|
| 42 |
|
| 43 |
-
return
|
| 44 |
|
| 45 |
def process_image(image_path, depth_map_path=None):
|
| 46 |
image_path = Path(image_path)
|
|
@@ -57,8 +53,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 57 |
if depth_map_path:
|
| 58 |
if depth_map_path.endswith('.npy'):
|
| 59 |
depth_image = np.load(depth_map_path)
|
| 60 |
-
# Invert depth only for npy files
|
| 61 |
-
depth_image = 1 - depth_image
|
| 62 |
else:
|
| 63 |
depth_image_raw = Image.open(depth_map_path).convert("L")
|
| 64 |
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
|
|
|
| 20 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 21 |
background_removal_client = Client("not-lain/background-removal")
|
| 22 |
|
|
|
|
| 23 |
def remove_background(image):
|
| 24 |
# Convert the PIL image to a byte stream
|
| 25 |
img_bytes = BytesIO()
|
|
|
|
| 27 |
img_bytes.seek(0)
|
| 28 |
|
| 29 |
# Step 1: Remove background using the Gradio API
|
| 30 |
+
result_url = background_removal_client.predict(f=img_bytes, api_name="/png")
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Step 2: Download the image returned by the API
|
| 33 |
response = requests.get(result_url)
|
|
|
|
| 36 |
# Step 3: Apply `rmbg` for additional cleanup
|
| 37 |
cleaned_foreground = remove(foreground)
|
| 38 |
|
| 39 |
+
return cleaned_foreground.convert("RGB") # Directly return cleaned foreground
|
| 40 |
|
| 41 |
def process_image(image_path, depth_map_path=None):
|
| 42 |
image_path = Path(image_path)
|
|
|
|
| 53 |
if depth_map_path:
|
| 54 |
if depth_map_path.endswith('.npy'):
|
| 55 |
depth_image = np.load(depth_map_path)
|
| 56 |
+
depth_image = 1 - depth_image # Invert depth only for npy files
|
|
|
|
| 57 |
else:
|
| 58 |
depth_image_raw = Image.open(depth_map_path).convert("L")
|
| 59 |
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|