labelme 的安装使用与填坑

15 篇文章 0 订阅

这些博客介绍的挺好的:

数据标注软件labelme详解

labelme 中遇到的问题

AttributeError: module 'labelme.utils' has no attribute 'draw_label'

下面是笔者曾经的填坑经验,防止忘记,以作记录。

一、安装

高低版本不通用;高版本报错。所以笔者安装4.5.2版本,其他版本也可以。

如果多人协作标注,建议使用同一版本。

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple labelme==4.5.2

二、使用

在PyCharm-Terminal中输入“labelme”,即可使用。放张多年前的荔园。

三、令人头疼的错误情况

3.1 错误

解析json文件时,报错:AttributeError: module ‘labelme.utils’ has no attribute ‘draw’

3.2 原因

据说是高版本的utils文件夹缺少draw.py文件

3.3 解决方案

1)新建 draw.py 文件,内容如下

import io

import numpy as np
import PIL.Image
import PIL.ImageDraw


def label_colormap(N=256):
    def bitget(byteval, idx):
        return (byteval & (1 << idx)) != 0

    cmap = np.zeros((N, 3))
    for i in range(0, N):
        id = i
        r, g, b = 0, 0, 0
        for j in range(0, 8):
            r = np.bitwise_or(r, (bitget(id, 0) << 7 - j))
            g = np.bitwise_or(g, (bitget(id, 1) << 7 - j))
            b = np.bitwise_or(b, (bitget(id, 2) << 7 - j))
            id = (id >> 3)
        cmap[i, 0] = r
        cmap[i, 1] = g
        cmap[i, 2] = b
    cmap = cmap.astype(np.float32) / 255
    return cmap


# similar function as skimage.color.label2rgb
def label2rgb(lbl, img=None, n_labels=None, alpha=0.5, 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 draw_label(label, img=None, label_names=None, colormap=None):
    import matplotlib.pyplot as plt
    backend_org = plt.rcParams['backend']
    plt.switch_backend('agg')

    plt.subplots_adjust(left=0, right=1, top=1, bottom=0,
                        wspace=0, hspace=0)
    plt.margins(0, 0)
    plt.gca().xaxis.set_major_locator(plt.NullLocator())
    plt.gca().yaxis.set_major_locator(plt.NullLocator())

    if label_names is None:
        label_names = [str(l) for l in range(label.max() + 1)]

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

    label_viz = label2rgb(label, img, n_labels=len(label_names))
    plt.imshow(label_viz)
    plt.axis('off')

    plt_handlers = []
    plt_titles = []
    for label_value, label_name in enumerate(label_names):
        if label_value not in label:
            continue
        if label_name.startswith('_'):
            continue
        fc = colormap[label_value]
        p = plt.Rectangle((0, 0), 1, 1, fc=fc)
        plt_handlers.append(p)
        plt_titles.append('{value}: {name}'
                          .format(value=label_value, name=label_name))
    plt.legend(plt_handlers, plt_titles, loc='lower right', framealpha=.5)

    f = io.BytesIO()
    plt.savefig(f, bbox_inches='tight', pad_inches=0)
    plt.cla()
    plt.close()

    plt.switch_backend(backend_org)

    out_size = (label_viz.shape[1], label_viz.shape[0])
    out = PIL.Image.open(f).resize(out_size, PIL.Image.BILINEAR).convert('RGB')
    out = np.asarray(out)
    return out

2)更新 __init__.py 文件,内容如下

# flake8: noqa

from ._io import lblsave

from .image import apply_exif_orientation
from .image import img_arr_to_b64
from .image import img_b64_to_arr
from .image import img_data_to_arr
from .image import img_data_to_pil
from .image import img_data_to_png_data
from .image import img_pil_to_data

from .shape import labelme_shapes_to_label
from .shape import masks_to_bboxes
from .shape import polygons_to_mask
from .shape import shape_to_mask
from .shape import shapes_to_label

from .qt import newIcon
from .qt import newButton
from .qt import newAction
from .qt import addActions
from .qt import labelValidator
from .qt import struct
from .qt import distance
from .qt import distancetoline
from .qt import fmtShortcut

from .draw import label_colormap
from .draw import label2rgb
from .draw import draw_label

3)将上述两个文件拷贝或替换到 …/venv/Lib/site-packages/labelme/utils 文件夹中

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
CSDN LabelMe是一个开源的标注工具,主要用于图像和视频的标注。下面是关于CSDN LabelMe的安装以及使用方法的详细介绍。 安装CSDN LabelMe非常简单,首先需要在电脑上安装好Python环境,然后从GitHub上下载CSDN LabelMe的源代码。安装完成后,可以通过运行指定命令来启动CSDN LabelMe。 使用CSDN LabelMe的第一步是准备要标注的图像或视频数据。将这些数据放入CSDN LabelMe所在的文件夹中,然后打开CSDN LabelMe的界面。在界面的左上角可以看到"Load"按钮,点击它可以加载要标注的数据。 加载数据后,可以使用鼠标在图像上进行绘制和修改。CSDN LabelMe支持绘制矩形、多边形、点和线段等不同的标注形状。在标注过程中,可以使用键盘上的快捷键来切换不同的标注工具和操作模式。 除了简单的绘制,CSDN LabelMe还支持更复杂的功能,比如图像或视频的裁剪、缩放和旋转等操作。在标注过程中,可以使用这些功能来更精确地定位和标记感兴趣的目标。 标注完成后,可以将结果保存到指定的文件中。CSDN LabelMe支持多种不同的数据格式,比如json、xml或Matlab等。选择适合自己工作流程的格式,保存标注结果以供后续的分析和处理。 总的来说,CSDN LabelMe是一个功能强大、易于使用的标注工具,可以帮助用户高效地完成图像和视频的标注任务。通过正确的安装使用,可以快速地掌握其操作方法,并完成标注工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

累了就要打游戏

把我养胖,搞代码

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值