Spaces:
Running
Running
Commit ·
0e8f4b9
1
Parent(s): 2b8f512
feat: apply mask on top of the image
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio
|
| 2 |
import numpy
|
| 3 |
|
|
|
|
| 4 |
from pathlib import Path
|
| 5 |
from PIL import Image
|
| 6 |
|
|
@@ -14,29 +15,24 @@ TEST_IMAGES_PATH = Path('.') / 'test'
|
|
| 14 |
def preprocess_mask(file_name):
|
| 15 |
"""Ensures masks are in grayscale format and removes transparency."""
|
| 16 |
mask_path = Path(
|
| 17 |
-
'/kaggle/
|
| 18 |
mask = Image.open(mask_path)
|
| 19 |
|
| 20 |
-
# Convert palette-based images to RGBA first to ensure proper color interpretation
|
| 21 |
if mask.mode == 'P':
|
| 22 |
mask = mask.convert('RGBA')
|
| 23 |
|
| 24 |
-
# Convert any non-RGBA images to RGBA
|
| 25 |
if mask.mode != 'RGBA':
|
| 26 |
mask = mask.convert('RGBA')
|
| 27 |
|
| 28 |
mask_data = mask.getdata()
|
| 29 |
|
| 30 |
-
# Replace fully transparent pixels with black (or another valid label)
|
| 31 |
new_mask_data = [
|
| 32 |
-
# Ensure full opacity in new mask
|
| 33 |
(r, g, b, 255) if a > 0 else (0, 0, 0, 255)
|
| 34 |
for r, g, b, a in mask_data
|
| 35 |
]
|
| 36 |
|
| 37 |
mask.putdata(new_mask_data)
|
| 38 |
|
| 39 |
-
# Convert to grayscale after handling transparency
|
| 40 |
return PILMask.create(mask.convert('L'))
|
| 41 |
|
| 42 |
|
|
@@ -47,17 +43,27 @@ def segment_image(image):
|
|
| 47 |
image = PILImage.create(image)
|
| 48 |
prediction, _, _ = LEARNER.predict(image)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
| 50 |
prediction_array = numpy.array(prediction, dtype=numpy.uint8)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
return
|
| 56 |
|
| 57 |
|
| 58 |
demo = gradio.Interface(
|
| 59 |
segment_image,
|
| 60 |
-
|
| 61 |
outputs=gradio.Image(type='numpy'),
|
| 62 |
examples=[str(image) for image in TEST_IMAGES_PATH.iterdir()]
|
| 63 |
)
|
|
|
|
| 1 |
import gradio
|
| 2 |
import numpy
|
| 3 |
|
| 4 |
+
from matplotlib import _cm
|
| 5 |
from pathlib import Path
|
| 6 |
from PIL import Image
|
| 7 |
|
|
|
|
| 15 |
def preprocess_mask(file_name):
|
| 16 |
"""Ensures masks are in grayscale format and removes transparency."""
|
| 17 |
mask_path = Path(
|
| 18 |
+
'/kaggle/inumpyut/car-segmentation/car-segmentation/masks') / file_name.name
|
| 19 |
mask = Image.open(mask_path)
|
| 20 |
|
|
|
|
| 21 |
if mask.mode == 'P':
|
| 22 |
mask = mask.convert('RGBA')
|
| 23 |
|
|
|
|
| 24 |
if mask.mode != 'RGBA':
|
| 25 |
mask = mask.convert('RGBA')
|
| 26 |
|
| 27 |
mask_data = mask.getdata()
|
| 28 |
|
|
|
|
| 29 |
new_mask_data = [
|
|
|
|
| 30 |
(r, g, b, 255) if a > 0 else (0, 0, 0, 255)
|
| 31 |
for r, g, b, a in mask_data
|
| 32 |
]
|
| 33 |
|
| 34 |
mask.putdata(new_mask_data)
|
| 35 |
|
|
|
|
| 36 |
return PILMask.create(mask.convert('L'))
|
| 37 |
|
| 38 |
|
|
|
|
| 43 |
image = PILImage.create(image)
|
| 44 |
prediction, _, _ = LEARNER.predict(image)
|
| 45 |
|
| 46 |
+
print("Prediction shape:", prediction.shape)
|
| 47 |
+
print("Unique values:", numpy.unique(prediction))
|
| 48 |
+
|
| 49 |
prediction_array = numpy.array(prediction, dtype=numpy.uint8)
|
| 50 |
|
| 51 |
+
colormap = _cm.get_cmap('jet', numpy.max(
|
| 52 |
+
prediction_array) + 1)
|
| 53 |
+
colored_mask = colormap(prediction_array)[:, :, :3]
|
| 54 |
+
|
| 55 |
+
image_array = numpy.array(image).astype(numpy.float32) / 255.0
|
| 56 |
+
|
| 57 |
+
overlay = (image_array * 0.7) + (colored_mask * 0.3) # Adjust transparency
|
| 58 |
+
|
| 59 |
+
overlay = (overlay * 255).astype(numpy.uint8)
|
| 60 |
|
| 61 |
+
return overlay
|
| 62 |
|
| 63 |
|
| 64 |
demo = gradio.Interface(
|
| 65 |
segment_image,
|
| 66 |
+
inumpyuts=gradio.Image(type='pil'),
|
| 67 |
outputs=gradio.Image(type='numpy'),
|
| 68 |
examples=[str(image) for image in TEST_IMAGES_PATH.iterdir()]
|
| 69 |
)
|