Detailed installation process reference: https://github.com/wkentaro/labelme
1 Installation of Labelme under Windows
Execute the following commands:
1) 打开Anaconda命令行工具
2) 执行 conda create --name=labelme python=3.6
3) 执行activate labelme
4) 执行 conda install pyyaml
5) 执行 pip install labelme
6) 执行 labelme
2 Use of Labelme
1) Click "open" to open the image to be annotated, and select to annotate the target area. Click after the annotation is completed to generate a json file.
2) Enter the labelme installation directory and execute python labelme_json_to_dataset <文件名>.json
. You can get a folder with five files, namely: *.png, info.yaml , label.png, label_viz.png,label_names.txt
. The label.png
sum info.yaml
is what we need to use, which label.png
is equivalent to the mask file.
The generated files are as follows:
Appendix I
I modified the labelme_json_to_dataset
file a bit, and it can be generated in batches, python <文件夹名称>
just :
labelme_json_to_dataset is modified as follows:
#!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()
Appendix II
labelme
The generated mask tag label.png
is stored in 16 bits, and opencv
8 bits are read by default, and 16 bits need to be converted to 8 bits. You can use the following Java code to convert:
Someone has provided a method for C++ conversion: 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);
}