labelme标注后如何生成数据集
作者:互联网
安装labelme环境
打开Anaconda Prompt, 直接输入pip install labelme即可安装
<!-- more -->
标注图片的两种方法
由于课题研究主要针对髋骨附近的股骨进行标注,只需要一种标签即可:
方法一
1)直接在Anaconda Prompt中输入labelme即可;
2)点击OpenDir选择所要标注的图片目录,标签名自己随意起就好了;
3)点击File->Change OutputDir来改变保存json文件的位置;
4)标注完成后需要生成数据集,首先找到Anaconda环境下的labelme_json_to_dataset.exe文件,在我的电脑上文件路径是D:\anaconda3\Scripts,将labelme_json_to_dataset.exe复制到你想要保存图片的文件夹,然后修改D: \anaconda3\Lib\site-packages\labelme\cl 目录下的json_to_dataset.py文件, 修改为:
import argparse import json import os import os.path as osp import warnings import PIL.Image import yaml from labelme import utils import base64 def main(): warnings.warn("This script is aimed to demonstrate how to convert the\n" "JSON file to a single image dataset, and not to handle\n" "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)) if data['imageData']: imageData = data['imageData'] else: imagePath = os.path.join(os.path.dirname(path), 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 data['shapes']: 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 # label_values must be dense label_values, label_names = [], [] for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]): label_values.append(lv) label_names.append(ln) assert label_values == list(range(len(label_values))) lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value) captions = ['{}: {}'.format(lv, ln) for ln, lv in label_name_to_value.items()] lbl_viz = utils.draw_label(lbl, img, captions) 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) PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png')) #PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.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') warnings.warn('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) print('Saved to: %s' % out_dir) if __name__ == '__main__': main()
cd 到labelme_json_to_dataset.exe所在目录,然后执行labelme_json_to_dataset json文件目录;
该方法缺点是json文件生成的都是单独的文件夹,无法将所有标签放在一个目录,在处理多个图片文件夹的时候较为不便,方法二则可以更为方便的标注数据集;
方法二
1)在所要标注的图片文件中新建labels.txt文件,并在文件内输入如下信息:
bone为自己定义的标签,如果需要多种标签则继续换行添加即可
2)在输出目录中添加labelme2voc.py文件,文件内容为:
#!/usr/bin/env python from __future__ import print_function import argparse import glob import os import os.path as osp import sys import imgviz import numpy as np import labelme def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument("input_dir", help="input annotated directory") parser.add_argument("output_dir", help="output dataset directory") parser.add_argument("--labels", help="labels file", required=True) parser.add_argument( "--noviz", help="no visualization", action="store_true" ) args = parser.parse_args() if osp.exists(args.output_dir): print("Output directory already exists:", args.output_dir) sys.exit(1) os.makedirs(args.output_dir) os.makedirs(osp.join(args.output_dir, "JPEGImages")) os.makedirs(osp.join(args.output_dir, "SegmentationClass")) os.makedirs(osp.join(args.output_dir, "SegmentationClassPNG")) if not args.noviz: os.makedirs( osp.join(args.output_dir, "SegmentationClassVisualization") ) print("Creating dataset:", args.output_dir) class_names = [] class_name_to_id = {} for i, line in enumerate(open(args.labels).readlines()): class_id = i - 1 # starts with -1 class_name = line.strip() class_name_to_id[class_name] = class_id if class_id == -1: assert class_name == "__ignore__" continue elif class_id == 0: assert class_name == "_background_" class_names.append(class_name) class_names = tuple(class_names) print("class_names:", class_names) out_class_names_file = osp.join(args.output_dir, "class_names.txt") with open(out_class_names_file, "w") as f: f.writelines("\n".join(class_names)) print("Saved class_names:", out_class_names_file) for filename in glob.glob(osp.join(args.input_dir, "*.json")): print("Generating dataset from:", filename) label_file = labelme.LabelFile(filename=filename) base = osp.splitext(osp.basename(filename))[0] out_img_file = osp.join(args.output_dir, "JPEGImages", base + ".jpg") out_lbl_file = osp.join( args.output_dir, "SegmentationClass", base + ".npy" ) out_png_file = osp.join( args.output_dir, "SegmentationClassPNG", base + ".png" ) if not args.noviz: out_viz_file = osp.join( args.output_dir, "SegmentationClassVisualization", base + ".jpg", ) with open(out_img_file, "wb") as f: f.write(label_file.imageData) img = labelme.utils.img_data_to_arr(label_file.imageData) lbl, _ = labelme.utils.shapes_to_label( img_shape=img.shape, shapes=label_file.shapes, label_name_to_value=class_name_to_id, ) labelme.utils.lblsave(out_png_file, lbl) np.save(out_lbl_file, lbl) if not args.noviz: viz = imgviz.label2rgb( label=lbl, img=imgviz.rgb2gray(img), font_size=15, label_names=class_names, loc="rb", ) imgviz.io.imsave(out_viz_file, viz) if __name__ == "__main__": main()
3)输入以下命令 labelme images --labels labels.txt --nodata --validatelabel exact --config '{shift_auto_shape_color: -2}' ,若无效,手动打开labelme进行标注;
4)标注完成后执行:python labelme2voc.py images target --labels labels.txt即可
标签:标注,name,osp,label,生成,labelme,class,dir,out 来源: https://www.cnblogs.com/Season-Chu/p/14940649.html