Skip to content

Commit

Permalink
Remove dependency on scikit-image
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Nov 18, 2017
1 parent e9d7e62 commit c45f650
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
36 changes: 31 additions & 5 deletions labelme/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import io
except ImportError:
import io as io
import warnings

import matplotlib.pyplot as plt
import numpy as np
import PIL.Image
import PIL.ImageDraw
import scipy.misc
import skimage.color


def labelcolormap(N=256):
def label_colormap(N=256):

def bitget(byteval, idx):
return ((byteval & (1 << idx)) != 0)
Expand All @@ -33,6 +33,33 @@ def bitget(byteval, idx):
return cmap


def labelcolormap(N=256):
warnings.warn('labelcolormap is deprecated. Please use label_colormap.')
return label_colormap(N=N)


# similar function as skimage.color.label2rgb
def label2rgb(lbl, img=None, n_labels=None, alpha=0.3, thresh_suppress=0):
if n_labels is None:
n_labels = len(np.unique(lbl))

cmap = label_colormap(n_labels)
cmap = (cmap * 255).astype(np.uint8)

lbl_viz = cmap[lbl]
lbl_viz[lbl == -1] = (0, 0, 0) # unlabeled

if img is not None:
img_gray = PIL.Image.fromarray(img).convert('LA')
img_gray = np.asarray(img_gray.convert('RGB'))
# img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# img_gray = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2RGB)
lbl_viz = alpha * lbl_viz + (1 - alpha) * img_gray
lbl_viz = lbl_viz.astype(np.uint8)

return lbl_viz


def img_b64_to_array(img_b64):
f = io.BytesIO()
f.write(base64.b64decode(img_b64))
Expand All @@ -57,10 +84,9 @@ def draw_label(label, img, label_names, colormap=None):
plt.gca().yaxis.set_major_locator(plt.NullLocator())

if colormap is None:
colormap = labelcolormap(len(label_names))
colormap = label_colormap(len(label_names))

label_viz = skimage.color.label2rgb(
label, img, colors=colormap[1:], bg_label=0, bg_color=colormap[0])
label_viz = label2rgb(label, img, n_labels=len(label_names))
plt.imshow(label_viz)
plt.axis('off')

Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
exclude = .anaconda2/*,labelme/*
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def run(self):
'matplotlib',
'Pillow>=2.8.0',
'scipy',
'scikit-image',
'six',
'PyYAML',
],
Expand Down

0 comments on commit c45f650

Please sign in to comment.