Spaces:
Sleeping
Sleeping
Commit
·
d728d1b
1
Parent(s):
5141811
debug: print the prediction
Browse files
app.py
CHANGED
@@ -10,15 +10,17 @@ from fastai.vision.all import load_learner, PILImage, PILMask
|
|
10 |
MODEL_PATH = Path('.') / 'models'
|
11 |
TEST_IMAGES_PATH = Path('.') / 'test'
|
12 |
|
|
|
13 |
def preprocess_mask(file_name):
|
14 |
"""Ensures masks are in grayscale format and removes transparency."""
|
15 |
-
mask_path = Path(
|
|
|
16 |
mask = Image.open(mask_path)
|
17 |
|
18 |
# Convert palette-based images to RGBA first to ensure proper color interpretation
|
19 |
if mask.mode == 'P':
|
20 |
-
mask = mask.convert('RGBA')
|
21 |
-
|
22 |
# Convert any non-RGBA images to RGBA
|
23 |
if mask.mode != 'RGBA':
|
24 |
mask = mask.convert('RGBA')
|
@@ -27,23 +29,30 @@ def preprocess_mask(file_name):
|
|
27 |
|
28 |
# Replace fully transparent pixels with black (or another valid label)
|
29 |
new_mask_data = [
|
30 |
-
|
|
|
31 |
for r, g, b, a in mask_data
|
32 |
]
|
33 |
-
|
34 |
mask.putdata(new_mask_data)
|
35 |
|
36 |
# Convert to grayscale after handling transparency
|
37 |
return PILMask.create(mask.convert('L'))
|
38 |
|
|
|
39 |
LEARNER = load_learner(MODEL_PATH / 'car-segmentation_v1.pkl')
|
40 |
|
41 |
|
42 |
def segment_image(image):
|
43 |
image = PILImage.create(image)
|
44 |
prediction, _, _ = LEARNER.predict(image)
|
|
|
|
|
|
|
|
|
45 |
return numpy.array(prediction, dtype=numpy.uint8)
|
46 |
|
|
|
47 |
demo = gradio.Interface(
|
48 |
segment_image,
|
49 |
inputs=gradio.Image(type='pil'),
|
|
|
10 |
MODEL_PATH = Path('.') / 'models'
|
11 |
TEST_IMAGES_PATH = Path('.') / 'test'
|
12 |
|
13 |
+
|
14 |
def preprocess_mask(file_name):
|
15 |
"""Ensures masks are in grayscale format and removes transparency."""
|
16 |
+
mask_path = Path(
|
17 |
+
'/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')
|
|
|
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 |
+
|
43 |
LEARNER = load_learner(MODEL_PATH / 'car-segmentation_v1.pkl')
|
44 |
|
45 |
|
46 |
def segment_image(image):
|
47 |
image = PILImage.create(image)
|
48 |
prediction, _, _ = LEARNER.predict(image)
|
49 |
+
|
50 |
+
print("Prediction shape:", prediction.shape)
|
51 |
+
print("Unique values:", numpy.unique(prediction))
|
52 |
+
|
53 |
return numpy.array(prediction, dtype=numpy.uint8)
|
54 |
|
55 |
+
|
56 |
demo = gradio.Interface(
|
57 |
segment_image,
|
58 |
inputs=gradio.Image(type='pil'),
|