1、安装所需的库
2、标注
3、生成标注图像
1、单文件json格式转化为png格式
(1)找到labelme安装目录中的Scripts,找到应用程序labelme_json_to_dataset.exe,将此应用程序复制到json文件所在目录
(2)进入anaconda命令行,进入labelme所在环境,cd到包含json文件和应用程序labelme_json_to_dataset.exe的文件夹所在目录,输入指令
labelme_json_to_dataset E:\jjj #包含json文件和应用程序labelme_json_to_dataset.exe的文件夹所处目录
即可得到一个文件夹,位于jjj文件夹所在目录,文件夹里有四个文件:
img.png,源文件图像label.png,标签图像label_names.txt,标签中的各个类别的名称label_viz.png,源文件与标签融合文件
2、json格式批量转换为png格式
(1)找到labelme安装目录下Lib\site-packages\labelme\cli可以看到json_to_dataset.py文件,替换其中的代码(替换代码如下)。
import argparseimport jsonimport osimport os.path as ospimport warningsimport PIL.Imageimport yamlfrom labelme import utilsimport base64def 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_fileif 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.outif 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 denselabel_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()
(2)进入labelme虚拟环境,cd到应用程序labelme_json_to_dataset.exe所处路径(Scripts目录下),输入指令
labelme_json_to_dataset.exe D:\workplace\labelme\c062~c066\json #json文件夹所处目录
即可得到转换文件,储存在Scripts目录内(labelme版本需要降低到3.16.2否则转换不成功,方法:uninstall原来版本,pipinstalllabelme==3.16.2)。