其他分享
首页 > 其他分享> > 准备maskrcnn的数据集,labelme版的

准备maskrcnn的数据集,labelme版的

作者:互联网

先用labelme标注好自己的数据后,
step1:
将标注好的原始图片和json文件分别放置在不同的文件夹,例如:
在这里插入图片描述
step2:批量转换
D:\anaconda\envs\tensorflow2\Lib\site-packages\labelme\cli在这个类似的路径下找到json_to_dataset.py,可能需要稍加改动,改后代码如下:

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

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

    if args.out is None:
        out_dir = osp.basename(json_file).replace(".", "_")
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = args.out
    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    count = os.listdir(json_file)
    for i in range(0, len(count)):
        path = os.path.join(json_file, count[i])

        if os.path.isfile(path):
            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(
                label=lbl, image=imgviz.asgray(img), label_names=label_names, loc="rb"
            )

            out_dir = osp.basename(count[i]).replace('.', '_')
            out_dir = osp.join(osp.dirname(count[i]), out_dir)
            if not osp.exists(out_dir):
                os.mkdir(out_dir)
                print(out_dir)

            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.warning('info.yaml is being replaced by label_names.txt')
            info = dict(label_names=label_names)
            with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
                yaml.safe_dump(info, f, default_flow_style=False)

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


if __name__ == "__main__":
    main()

在控制台的的对于环境中进到json_to_dataset.py所在目录,我的就是上文中的D:\anaconda\envs\tensorflow2\Lib\site-packages\labelme\cli然后运行:

python json_to_dataset.py json文件夹的路径

然后就能得到很多个文件夹,每个文件夹中都有这些图片
在这里插入图片描述
step3:在train_data文件夹下新建两个文件夹,cv2_mask, labelme_json
在这里插入图片描述
将step2所得文件夹(不指定输出文件夹去,就在和json_to_dataset.py同一目录),移动到 labelme_json文件夹下。
step4:提取所有的mask到cv2_mask

import os
path='labelme_json'
files=os.listdir(path)
for file in files:
    jpath=os.listdir(os.path.join(path,file))
    new=file[:-5]
    newnames=os.path.join('cv2_mask',new)
    filename=os.path.join(path,file,jpath[2])
    print(filename)
    print(newnames)
    os.rename(filename,newnames+'.png')

在train_data文件夹下运行以上代码即可批量抽取mask文件

标签:name,maskrcnn,label,json,准备,labelme,os,dir,out
来源: https://blog.csdn.net/qq_45724346/article/details/122862327