图像分割 json文件 批量转mask

图像分割 json文件 批量转mask

在进行labelme进行标注后有大量json文件,如果做图像分割需要转为mask才能进入进行训练

1.下载labelme
去github上搜索labelme,用git clone 下载

2.修改json_to_dataset.py 文件路径:label/label/cil/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(file, outfile):
    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", default=file)
    parser.add_argument("-o", "--out", default=outfile)
    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)

    data = json.load(open(json_file))
    imageData = data.get("imageData")

    if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"].split("/")[-1])
        
        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, img=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__":
    import os
    import os.path as osp
    json_path = "../SR-line/" #json文件路径
    out_path = "../SR-line_vis/" #输出mask保存文件路径
    os.makedirs(out_path, exist_ok=True)
    for file in os.listdir(json_path):
        if ".json" in file:
            main(osp.join(json_path, file), osp.join(out_path, file.split(".")[0]))

修改完后在json_to_dataset.py路径下运行:python json_to_dataset.py (第一次运行过程中缺什么包安装什么包:pip install [缺少的包] 即可)

图像分割 json文件 批量转mask_第1张图片
保存结果:

图像分割 json文件 批量转mask_第2张图片

备注:
1.mask是8bit,原图是24bit,需要正确的图片格式输入网络才能正确训练,否则模型无法正确学习
2.文件保存格式和路径可以适当修改json_to_dataset.py里面的代码

简单版:

"""
将用labelme分割打标工具打标获取的json文件批量转换为mask(annotation)图及对应图片

"""

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

import imgviz
import PIL.Image
from labelme import utils

if __name__ == "__main__":

    base_dir = osp.dirname(osp.abspath(__file__))

    out_dir_name = "out_data"
    out_dir = osp.join(base_dir, out_dir_name)
    if not osp.exists(out_dir):
        os.mkdir(out_dir)
    label_names = []
    label_file = "./label_names.txt"
    label_value_dict = {}
    with open(label_file) as f:
        labels = f.readlines()
        if len(labels) > 0:
            for i, item in enumerate(labels):
                item = item.strip()
                label_value_dict[item] = i

    json_file_name = "jsons"  #'cityspaces'
    json_file_path = osp.join(base_dir, json_file_name)
    new_filename = 1
    for file_name in os.listdir(json_file_path):
        print(file_name)
        if file_name.endswith(".json"):
            filePath = osp.join(json_file_path, file_name)
            data = json.load(open(filePath))
            imageData = data.get("imageData")
            if not imageData:
                imagePath = os.path.join(os.path.dirname(filePath),
                                         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)

            for shape in sorted(data["shapes"], key=lambda x: x["label"]):
                label_name = shape["label"]
                if label_name not in label_value_dict:
                    label_value = len(label_value_dict)
                    label_value_dict[label_name] = label_value

            lbl, _ = utils.shapes_to_label(img.shape, data["shapes"],
                                           label_value_dict)

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

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

            #new_filename = uuid.uuid4().hex

            PIL.Image.fromarray(img).save(
                osp.join(out_dir,
                         str(new_filename) + "_img.png"))
            utils.lblsave(
                osp.join(out_dir,
                         str(new_filename) + "_label" + ".png"), lbl)
            new_filename = new_filename + 1
            #PIL.Image.fromarray(lbl_viz).save(
            #    osp.join(out_dir, new_filename + "_label_viz" + ".png"))

    for name, value in label_value_dict.items():
        label_names[value] = name

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

#

你可能感兴趣的:(深度学习,神经网络,python)