Spaces:
Sleeping
Sleeping
Commit
·
8fc5d37
1
Parent(s):
9ab897d
fix: add missing function
Browse files
app.py
CHANGED
@@ -2,8 +2,9 @@ import gradio
|
|
2 |
import numpy
|
3 |
|
4 |
from pathlib import Path
|
|
|
5 |
|
6 |
-
from fastai.vision.all import load_learner, PILImage
|
7 |
|
8 |
|
9 |
MODEL_PATH = Path('.') / 'models'
|
@@ -11,6 +12,33 @@ TEST_IMAGES_PATH = Path('.') / 'test'
|
|
11 |
LEARNER = load_learner(MODEL_PATH / 'car-segmentation_v1.pkl')
|
12 |
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def segment_image(image):
|
15 |
image = PILImage.create(image)
|
16 |
prediction, _, _ = LEARNER.predict(image)
|
|
|
2 |
import numpy
|
3 |
|
4 |
from pathlib import Path
|
5 |
+
from PIL import Image
|
6 |
|
7 |
+
from fastai.vision.all import load_learner, PILImage, PILMask
|
8 |
|
9 |
|
10 |
MODEL_PATH = Path('.') / 'models'
|
|
|
12 |
LEARNER = load_learner(MODEL_PATH / 'car-segmentation_v1.pkl')
|
13 |
|
14 |
|
15 |
+
def preprocess_mask(file_name):
|
16 |
+
"""Ensures masks are in grayscale format and removes transparency."""
|
17 |
+
mask_path = Path('/kaggle/input/car-segmentation/car-segmentation/masks') / file_name.name
|
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 |
+
(r, g, b, 255) if a > 0 else (0, 0, 0, 255) # Ensure full opacity in new mask
|
33 |
+
for r, g, b, a in mask_data
|
34 |
+
]
|
35 |
+
|
36 |
+
mask.putdata(new_mask_data)
|
37 |
+
|
38 |
+
# Convert to grayscale after handling transparency
|
39 |
+
return PILMask.create(mask.convert('L'))
|
40 |
+
|
41 |
+
|
42 |
def segment_image(image):
|
43 |
image = PILImage.create(image)
|
44 |
prediction, _, _ = LEARNER.predict(image)
|