Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified label masking function #3

Merged
merged 1 commit into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions labelme/cli/draw_segment_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,12 @@ def multi_convert_json_to_mask(self, json_file):
copy.deepcopy(self.class_rgb),
self.segmentation_class)

utils.lblsave(
utils.lblsave_old(
os.path.join(self.masked_image_dir, file_name),
label)
label,
names,
copy.deepcopy(self.class_rgb),
self.segmentation_class)

if self.merge_save:
PIL.Image.fromarray(image).save(os.path.join(
Expand Down
1 change: 1 addition & 0 deletions labelme/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# flake8: noqa

from ._io import lblsave
from ._io import lblsave_old

from .image import apply_exif_orientation
from .image import img_arr_to_b64
Expand Down
24 changes: 24 additions & 0 deletions labelme/utils/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,27 @@ def lblsave(filename, lbl):
"[%s] Cannot save the pixel-wise class label as PNG. "
"Please consider using the .npy format." % filename
)

def lblsave_old(filename, lbl, names, class_rgb, segmentation_class):
here = osp.dirname(osp.abspath(__file__))

if osp.splitext(filename)[1] != '.png':
filename += '.png'
if lbl.min() >= -1 and lbl.max() < 255:
lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode='P')
colormap = np.ones((255, 3), dtype=float)
colormap[0] = [0, 0, 0]
for num_classes, classes in enumerate(names):
color_val = class_rgb[segmentation_class.index(classes)]

for i in range(3):
color_val[i] = 256 - color_val[i]
colormap[num_classes+1] = color_val
lbl_pil.putpalette((colormap * 255).astype(np.uint8).flatten())
lbl_pil.save(filename)

else:
raise ValueError(
'[%s] Cannot save the pixel-wise class label as PNG. '
'Please consider using the .npy format.' % filename
)