【目标检测】Windows下安装图片标注工具labelme

详细安装过程参考:https://github.com/wkentaro/labelme

1 Labelme在Windows下的安装

执行以下命令:

1) 打开Anaconda命令行工具
2) 执行 conda create --name=labelme python=3.6
3) 执行activate labelme
4) 执行 conda install pyyaml
5) 执行 pip install labelme
6) 执行 labelme

2 Labelme的使用

这里写图片描述

1)点击“open”打开需要标注的图像,选择对目标区域进行标注。标注完成后点击,产生一个json文件。
2)进入labelme安装目录,执行python labelme_json_to_dataset <文件名>.json。即可得到一个文件夹,有五个文件,分别是:*.png, info.yaml , label.png, label_viz.png,label_names.txt。 其中label.pnginfo.yaml是我们需要用到的,label.png相当于mask 文件。

生成的文件如下:

这里写图片描述


附录一

我修改了一下labelme_json_to_dataset文件,可以批量生成了,执行 python <文件夹名称>即可:

labelme_json_to_dataset 修改如下:

#!D:\conda\envs\labelme\python.exe

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

import numpy as np
import PIL.Image
import yaml

from labelme import utils


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    parser.add_argument('-o', '--out', default=None)
    args = parser.parse_args()

    json_file = args.json_file

    list = os.listdir(json_file) 
    for i in range(0, len(list)):
        path = os.path.join(json_file, list[i])
        if os.path.isfile(path):
            data = json.load(open(path))
            img = utils.img_b64_to_array(data['imageData'])
            lbl, lbl_names = utils.labelme_shapes_to_label(img.shape, data['shapes'])

            captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)]
            lbl_viz = utils.draw_label(lbl, img, captions)
            out_dir = osp.basename(list[i]).replace('.', '_')
            out_dir = osp.join(osp.dirname(list[i]), out_dir)
            if not osp.exists(out_dir):
                os.mkdir(out_dir)

            PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
            PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.png'))
            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 lbl_names:
                    f.write(lbl_name + '\n')

            warnings.warn('info.yaml is being replaced by label_names.txt')
            info = dict(label_names=lbl_names)
            with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
                yaml.safe_dump(info, f, default_flow_style=False)

            print('Saved to: %s' % out_dir)
if __name__ == '__main__':
    main()

附录二

labelme生成的掩码标签 label.png为16位存储,opencv默认读取8位,需要将16位转8位。可以使用下来的Java代码进行转化:

有人已经提供了C++转化的方法:http://blog.csdn.net/l297969586/article/details/79154150

    /**
     * 说明 : 16位 转化为 8位
     */
    public void transform(String originPath, String transformPath) {

        if(!new File(originPath).exists()) {
            System.out.println("文件不存在!");
            return;
        }

        //原图片
        Mat src = Imgcodecs.imread(originPath, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);

        //生成的图片
        Mat mat = Mat.zeros(src.rows(), src.cols(), CvType.CV_8SC1);
        for (int r = 0; r < src.rows(); r++) {
            for(int c = 0; c < src.cols(); c++){
                mat.put(r, c, src.get(r, c));
            }
        }

        Imgcodecs.imwrite(transformPath, mat);

    }

猜你喜欢

转载自blog.csdn.net/disiwei1012/article/details/79508954
今日推荐