labelme的安装、批量转换方法(版本号4.5.13)

1. 参考链接

labelme项目地址:https://github.com/wkentaro/labelme

参考这个链接:Labelme 批量转 dataset 使用 labelme_json_to_dataset 命令 (简明图文教程)


2. 安装labelme 4.5.13(推荐安装)

参考链接:深度学习图像数据集标注Labelme安装

2.1 创建环境 labelme

按住Win+R,输入cmd,打开命令行窗口,键入以下命令:

注意:python的版本号应该<=你已安装的python版本

conda create –name=labelme python=3.8

2.2 进入到labelme环境(然后进行以下安装步骤)

activate labelme

2.3 安装qt界面 pyqt

conda install pyqt

2.4 安装labelme 4.5.13

进入到要安装的环境,并安装指定版本labelme 4.5.13,如果不用指定版本号,则默认安装最新版本

(我是因为老师已经用了4.5.13版本,所以就也就跟随他一起用了,不然到时候我俩版本不一致,低版本的兼容不了高版本的,容易出问题)

pip install labelme==4.5.13

然后就会在该环境的配置文件夹D:\SoftWareInstallMenu\Anaconda3\envs\labelme\Lib\site-packages中添加labelme、labelme-4.5.13.dist-info两个文件夹:

labelme的安装、批量转换方法(版本号4.5.13)_第1张图片


2.2 批量转换

进入到D:\SoftWareInstallMenu\Anaconda3\envs\labelme\Lib\site-packages\labelme\cli文件夹,更改json_to_dataset.py文件,用以下代码覆盖掉原本代码:

import argparse
import base64
import json
import os
import os.path as osp

import imgviz
import PIL.Image

from labelme.logger import logger
from labelme import utils


def main():
    logger.warning(
        "This script is aimed to demonstrate how to convert the "
        "JSON file to a single image dataset."
    )
    logger.warning(
        "It won't handle multiple JSON files to generate a "
        "real-use dataset."
    )

    parser = argparse.ArgumentParser()
    parser.add_argument("json_file")
    parser.add_argument("-o", "--out", default=None)
    args = parser.parse_args()

    json_file = args.json_file

    filelist = os.listdir(json_file)  # 文件列表
    for i in range(0, len(filelist)):  # 遍历文件列表
        path = os.path.join(json_file, filelist[i])  # 文件路径
        if os.path.isdir(path):  #如果是目录则读取下一个
            continue
        my_out = osp.basename(filelist[i]).replace(".", "_")  # 文件名转目录
        if args.out is None:
            # out_dir = osp.basename(json_file).replace(".", "_")  # 注释掉
            out_dir = osp.join(osp.dirname(json_file), my_out)   # 总目录 + 文件目录
        else:
            # out_dir = args.out # 注释掉
            if not osp.exists(args.out): # 兼容目录不存在情况
                os.makedirs(args.out)
            out_dir = osp.join(args.out, my_out)  # 兼容out参数  --  总目录 + 文件目录
        if not osp.exists(out_dir):
            os.mkdir(out_dir)

        data = json.load(open(path))   # 读取目录标注文件
        imageData = data.get("imageData")

        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}
        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
        lbl, _ = utils.shapes_to_label(
            img.shape, data["shapes"], label_name_to_value
        )

        label_names = [None] * (max(label_name_to_value.values()) + 1)
        for name, value in label_name_to_value.items():
            label_names[value] = name

        lbl_viz = imgviz.label2rgb(
            lbl, imgviz.asgray(img), label_names=label_names, loc="rb"
        )

        PIL.Image.fromarray(img).save(osp.join(out_dir, "img.png"))
        utils.lblsave(osp.join(out_dir, "label.png"), lbl)
        PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, "label_viz.png"))

        with open(osp.join(out_dir, "label_names.txt"), "w") as f:
            for lbl_name in label_names:
                f.write(lbl_name + "\n")

        logger.info("Saved to: {}".format(out_dir))


if __name__ == "__main__":
    main()

遇到问题:参考此链接解决解决AttributeError: module ‘labelme.utils‘ has no attribute ‘draw_label‘


2.3 使用方法

(注意,这里为啥能直接执行 json_to_dataset.py 呢?是因为当前cmd我已经切换到json_to_dataset.py的目录下了,所以能找到该py文件并执行!没有切换的话,就直接输入该py文件的绝对路径即可)

进入到安装了labelme的环境,执行下列代码(后面是带转换的json文件路径):

使用方法1:(不指定输出目录,默认直接生成在原始待转换json文件目录的上一级目录)

python json_to_dataset.py 原始json目录

使用方法2:(指定输出目录)

pythonn json_to_dataset.py 原始json目录 --out 输出目录

3. 一些问题

Q:有遮挡的分割数据应该怎么标注?

答:分组号标注。设置group id,同一实例各部分分开标注,但是group id是相同的

参考链接:labelme实例分割标注 不连续的物体有遮挡的物体怎么标注? 那种视野很远 不清晰的物体还标注吗?

Q:是否同时使用线和多边形进行标注,这会存在哪些可能的问题?

答:最好是统一,要不然就是你的标签再代码中会处理成一致,不然你后面计算损失函数出大问题

参考链接:使用深度学习进行裂缝分割,如何标注数据?

Q:labelme标注软件的使用 || 语义分割数据标注、批量转换、多类别转换颜色错位问题-爱代码爱编程

你可能感兴趣的:(数据集操作,python,labelme)