-
Notifications
You must be signed in to change notification settings - Fork 0
/
mask_unifier.py
45 lines (33 loc) · 1.57 KB
/
mask_unifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import cv2
import numpy as np
import os
import matplotlib.pyplot as plt
from PIL import Image
from config import CLASS_MAPPER, IMAGE_SIZE, OG_MASKS_DIR, IMAGES_DIR, MASKS_DIR
def mask_unifier():
images_names = os.listdir(IMAGES_DIR)
multi_mask_names = os.listdir(OG_MASKS_DIR)
# print(multi_mask_names)
masks_by_image = []
for image_name in images_names:
img_name = ".".join(image_name.split(".")[:-1])
#res = [i for i in multi_mask_names if img_name in i and not i.__contains__("mask")]
res = [i for i in multi_mask_names if img_name in i and i.__contains__("_z")]
# print(res)
masks_by_image.append(res)
for image_name, masks in zip(images_names, masks_by_image):
img_name = ".".join(image_name.split(".")[:-1])
combined_mask = np.zeros((IMAGE_SIZE, IMAGE_SIZE), np.uint8)
for id, mask_name in enumerate(masks):
mask_type = ".".join(mask_name.split(".")[:-1]).split('_')[-1]
mask = cv2.imread(OG_MASKS_DIR + mask_name, cv2.IMREAD_GRAYSCALE)
mask = cv2.resize(mask, (IMAGE_SIZE, IMAGE_SIZE))
combined_mask[mask != 0] = CLASS_MAPPER[mask_type]
print(np.unique(combined_mask, return_counts=True))
Image.fromarray(combined_mask).save(MASKS_DIR + img_name + '_combined.png')
print("VALUE CHECK")
# VALUE CHECK
for image_name in images_names:
img_name = ".".join(image_name.split(".")[:-1])
mask = cv2.imread(MASKS_DIR + f"{img_name}_combined.png", cv2.IMREAD_GRAYSCALE)
print(np.unique(mask, return_counts=True))