change mask color according to their labels
Browse files- main.py +7 -1
- utils/helpers.py +5 -5
main.py
CHANGED
@@ -64,11 +64,17 @@ async def predict_image(file: UploadFile = File(...)):
|
|
64 |
else:
|
65 |
background_area = 0
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
# change the mask to base64 and calculate the score
|
68 |
for i in range(len(results)):
|
69 |
mask_area = calculate_mask_area(results[i]["mask"])
|
70 |
print(results[i]["mask"].size)
|
71 |
-
processed_image = process_image(results[i]["mask"])
|
72 |
results[i]["mask"] = image_to_base64(processed_image, "PNG")
|
73 |
if results[i]["label"] == "background":
|
74 |
continue
|
|
|
64 |
else:
|
65 |
background_area = 0
|
66 |
|
67 |
+
mask_dict = {
|
68 |
+
'acne': (255, 0, 0),
|
69 |
+
'dry': (0, 255, 0),
|
70 |
+
'oily': (0,0,255),
|
71 |
+
'background': (255,255,255)
|
72 |
+
}
|
73 |
# change the mask to base64 and calculate the score
|
74 |
for i in range(len(results)):
|
75 |
mask_area = calculate_mask_area(results[i]["mask"])
|
76 |
print(results[i]["mask"].size)
|
77 |
+
processed_image = process_image(results[i]["mask"], mask_dict[results[i]['label']])
|
78 |
results[i]["mask"] = image_to_base64(processed_image, "PNG")
|
79 |
if results[i]["label"] == "background":
|
80 |
continue
|
utils/helpers.py
CHANGED
@@ -14,22 +14,22 @@ def calculate_mask_area(mask: Image.Image, background=False) -> int:
|
|
14 |
non_zero_pixels = np.count_nonzero(mask_array)
|
15 |
return non_zero_pixels
|
16 |
|
17 |
-
def process_image(input_image: Image.Image) -> Image.Image:
|
18 |
|
19 |
|
20 |
data = np.array(input_image)
|
21 |
# Split the image into its component channels
|
22 |
|
23 |
-
# Create a mask where all pixels that are black (0
|
24 |
black_areas = data == 0
|
25 |
|
26 |
rgba_image = Image.new('RGBA', input_image.size)
|
27 |
rgba_data = np.array(rgba_image)
|
28 |
|
29 |
# Copy the grayscale data to all RGB channels
|
30 |
-
rgba_data[..., 0] =
|
31 |
-
rgba_data[..., 1] =
|
32 |
-
rgba_data[..., 2] =
|
33 |
|
34 |
# Set alpha channel to 255 (fully opaque) for all pixels
|
35 |
rgba_data[..., 3] = 255
|
|
|
14 |
non_zero_pixels = np.count_nonzero(mask_array)
|
15 |
return non_zero_pixels
|
16 |
|
17 |
+
def process_image(input_image: Image.Image, fill_color:tuple = (255, 255, 255)) -> Image.Image:
|
18 |
|
19 |
|
20 |
data = np.array(input_image)
|
21 |
# Split the image into its component channels
|
22 |
|
23 |
+
# Create a mask where all pixels that are black (0) will have 0 alpha
|
24 |
black_areas = data == 0
|
25 |
|
26 |
rgba_image = Image.new('RGBA', input_image.size)
|
27 |
rgba_data = np.array(rgba_image)
|
28 |
|
29 |
# Copy the grayscale data to all RGB channels
|
30 |
+
rgba_data[..., 0] = fill_color[0]
|
31 |
+
rgba_data[..., 1] = fill_color[1]
|
32 |
+
rgba_data[..., 2] = fill_color[2]
|
33 |
|
34 |
# Set alpha channel to 255 (fully opaque) for all pixels
|
35 |
rgba_data[..., 3] = 255
|