Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -30,9 +30,6 @@ def parse_color(color_str):
|
|
30 |
raise ValueError(f"Invalid color format: {color_str}. Use hex like '#ff0000', color name like 'red', or rgba format.")
|
31 |
|
32 |
def apply_mask(image: Image.Image, prompt: str, color: str) -> Image.Image:
|
33 |
-
# Convert image to RGBA
|
34 |
-
image_np = np.array(image.convert("RGBA"))
|
35 |
-
|
36 |
# Process the input image and prompt
|
37 |
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
38 |
outputs = model(**inputs)
|
@@ -40,14 +37,14 @@ def apply_mask(image: Image.Image, prompt: str, color: str) -> Image.Image:
|
|
40 |
|
41 |
# Get the binary mask from predictions
|
42 |
mask = preds.sigmoid().detach().cpu().numpy()
|
43 |
-
mask =
|
44 |
-
mask = cv2.GaussianBlur(mask, (15, 15), 0)
|
45 |
-
mask_bin = (mask > 0.4).astype(np.uint8)
|
46 |
|
|
|
|
|
47 |
|
48 |
# Resize mask to match image size
|
49 |
-
|
50 |
-
mask_3d = np.stack([
|
51 |
|
52 |
# Convert the color string to an RGB tuple
|
53 |
color_rgb = parse_color(color)
|
@@ -56,11 +53,7 @@ def apply_mask(image: Image.Image, prompt: str, color: str) -> Image.Image:
|
|
56 |
# Create an overlay with the selected color
|
57 |
overlay = np.zeros_like(image_np, dtype=np.uint8)
|
58 |
overlay[:] = overlay_color
|
59 |
-
|
60 |
-
masked_image = image_np.copy()
|
61 |
-
# masked_image[mask_bin == 1] = (
|
62 |
-
# 0.5 * masked_image[mask_bin == 1] + 0.5 * overlay[mask_bin == 1]).astype(np.units)
|
63 |
-
|
64 |
# Apply the mask to the image
|
65 |
masked_image = np.where(mask_3d == 1, overlay, image_np)
|
66 |
return Image.fromarray(masked_image)
|
@@ -78,4 +71,4 @@ iface = gr.Interface(
|
|
78 |
description="Upload an image, input a prompt (e.g., 'person', 'sky'), and pick a mask color."
|
79 |
)
|
80 |
|
81 |
-
iface.launch()
|
|
|
30 |
raise ValueError(f"Invalid color format: {color_str}. Use hex like '#ff0000', color name like 'red', or rgba format.")
|
31 |
|
32 |
def apply_mask(image: Image.Image, prompt: str, color: str) -> Image.Image:
|
|
|
|
|
|
|
33 |
# Process the input image and prompt
|
34 |
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
35 |
outputs = model(**inputs)
|
|
|
37 |
|
38 |
# Get the binary mask from predictions
|
39 |
mask = preds.sigmoid().detach().cpu().numpy()
|
40 |
+
mask = (mask > 0.5).astype(np.uint8)
|
|
|
|
|
41 |
|
42 |
+
# Convert image to RGBA
|
43 |
+
image_np = np.array(image.convert("RGBA"))
|
44 |
|
45 |
# Resize mask to match image size
|
46 |
+
mask_resized = cv2.resize(mask, (image_np.shape[1], image_np.shape[0]))
|
47 |
+
mask_3d = np.stack([mask_resized] * 4, axis=-1) # Extend mask to 3D
|
48 |
|
49 |
# Convert the color string to an RGB tuple
|
50 |
color_rgb = parse_color(color)
|
|
|
53 |
# Create an overlay with the selected color
|
54 |
overlay = np.zeros_like(image_np, dtype=np.uint8)
|
55 |
overlay[:] = overlay_color
|
56 |
+
|
|
|
|
|
|
|
|
|
57 |
# Apply the mask to the image
|
58 |
masked_image = np.where(mask_3d == 1, overlay, image_np)
|
59 |
return Image.fromarray(masked_image)
|
|
|
71 |
description="Upload an image, input a prompt (e.g., 'person', 'sky'), and pick a mask color."
|
72 |
)
|
73 |
|
74 |
+
iface.launch()
|