用labelme标记Windows中的语义标记,下,使用,进行,标签,标注

发表时间:2020-10-15

1. 安装

# python3
conda create --name=labelme python=3.6
conda activate labelme
pip install labelme

其余环境安装详见 GitHub

2. 使用

在终端输入labelme (在conda激活下的路径) 即可打开软件

(labelme) D:\>labelme

在这里插入图片描述 Create Polygons 多线段标注(右键更多形状选择), Ctrl+Z 撤销上一个点,可使用 Edit 最后对mask进行调整, Save 得到json文件

标签名字是类别名字,如‘road’、‘tree’等(不是类别序号)

对不同label指定不同颜色(参考Cityscapes数据集)

  • 修改 json_to_dataset.py
    参考文件地址:
    Anaconda3\envs\labelme\Lib\site-packages\labelme\cli\json_to_dataset.py
if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
        with open(imagePath, "rb") as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode("utf-8")
    img = utils.img_b64_to_arr(imageData)

    #label_name_to_value = {"_background_": 0}
   
    #指定label
    label_name_to_value = {"_background_": 0,'road':1,'sidewalk':2,'parking':3,'building':4,'fence':5,'pole':6,'traffic light':7,'traffic sign':8,'tree':9,'lawn':10,'sky':11,'person':12,'rider':13,'car':14,'truck':15,'bus':16,'trailer':17,'train':18,'motorcycle':19,'bicycle':20}
    
    
    for shape in sorted(data["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value


(将原文件中的 label_name_to_value 修改成我们需要的标签和像素值,并保存,此处是Cityscapes的标签和像素值)

  • 修改 label.py 文件
    参考文件地址:
    Anaconda3\envs\labelme\Lib\site-packages\imgviz\label.py
for i in range(0, n_label):
        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
    
   
    #添加colormap
    cmap[1,:] = [128, 64,128]
    cmap[2,:] = [244, 35,232]
    cmap[3,:] = [250,170,160]
    cmap[4,:] = [70, 70, 70]
    cmap[5,:] = [190,153,153]
    cmap[6,:] = [153,153,153]
    cmap[7,:] = [250,170, 30]
    cmap[8,:] = [220,220,  0]
    cmap[9,:] = [107,142, 35]
    cmap[10,:] = [152,251,152]
    cmap[11,:] = [70,130,180]
    cmap[12,:] = [220, 20, 60]
    cmap[13,:] = [225, 0, 0]
    cmap[14,:] = [0,  0, 142]
    cmap[15,:] = [0,  0,  70]
    cmap[16,:] = [0, 60,100]
    cmap[17,:] = [0,  0, 110]
    cmap[18,:] = [0,  0, 110]
    cmap[19,:] = [0,  0,230]
    cmap[20,:] = [119, 11, 32]

    if value is not None:
        hsv = color_module.rgb2hsv(cmap.reshape(1, -1, 3))
        if isinstance(value, float):
            hsv[:, 1:, 2] = hsv[:, 1:, 2].astype(float) * value
        else:
            assert isinstance(value, int)
            hsv[:, 1:, 2] = value
        cmap = color_module.hsv2rgb(hsv).reshape(-1, 3)
    return cmap

添加colormap将像素值着色

使用labelme_json_to_dataset 解析json文件,生成label.png

在终端输入命令 labelme_json_to_dataset xxx.json

(labelme) D:\>labelme_json_to_dataset 170-194_result\000179.json

在这里插入图片描述

文章来源互联网,如有侵权,请联系管理员删除。邮箱:417803890@qq.com / QQ:417803890

微配音

Python Free

邮箱:417803890@qq.com
QQ:417803890

皖ICP备19001818号
© 2019 copyright www.pythonf.cn - All rights reserved

微信扫一扫关注公众号:

联系方式

Python Free