labelme标记数据后,批量处理json文件,生成标签

1.安装labelme的过程省略,可参考别人

2.打开anaconda prompt

3.激活安装有labelme的虚拟环境

4.运用labelme命令打开labelme开始标记数据

5.处理json文件

  1. 首先找到labelme中的json_to_dataset.py文件

labelme标记数据后,批量处理json文件,生成标签_第1张图片

   2.更改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

    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, img=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.info("Saved to: {}".format(out_dir))


if __name__ == "__main__":
    main()

        3.在激活的labelme环境中,输入你批处理后文件保存的路径

labelme标记数据后,批量处理json文件,生成标签_第2张图片

        4. 输入:

C:\ProgramData\anaconda3\envs\labelme\Scripts\labelme_json_to_dataset.exe F:\DATA\Resize_ship_data_hjr\hqc\png

其中前面 C:\ProgramData\anaconda3\envs\labelme\Scripts\labelme_json_to_dataset.exe是你对应的labelme_json_to_dataset.exe的路径,

后面的F:\DATA\Resize_ship_data_hjr\hqc\png是labelme生成的json文件路径

labelme标记数据后,批量处理json文件,生成标签_第3张图片

然后就开始批量处理

labelme标记数据后,批量处理json文件,生成标签_第4张图片

最后在指定的文件夹下找到生成的标签文件

labelme标记数据后,批量处理json文件,生成标签_第5张图片

labelme标记数据后,批量处理json文件,生成标签_第6张图片

你可能感兴趣的:(labelme,python)