#json_to_dataset 批量转换# 两种经过试验的实现方法 以及 报错解决方案
方法一
json_to_dataset.py参考代码
可以尝试依照https://blog.csdn.net/yql_617540298/article/details/81110685教程中的代码,对json_to_dataset.py文件进行修改(可以参考我的路径找到该py文件:/home/anaconda2/envs/labelme/lib/python3.9/site-packages/labelme/cli),很多小伙伴都通过这种修改方法成功了。
不过,修改之后,可能出现AttributeError: module ‘labelme.utils’ has no attribute 'draw_label’的报错,可以通过添加draw_label模块进行修改。
module ‘labelme.utils‘ has no attribute ‘draw_label‘的报错解决方法
这一报错是由于labelme.utils中没有draw_label这个模块,所以我们需要自己进行添加。
首先找到labelme.utils文件夹(可以参考我的路径:/home/anaconda2/envs/labelme/lib/python3.9/site-packages/labelme/utils)。
在文件夹中,可以找到draw.py文件,我们需要在该文件中添加draw_label:
def draw_label(label, img=None, label_names=None, colormap=None):
import matplotlib.pyplot as plt
backend_org = plt.rcParams['backend']
plt.switch_backend('agg')
plt.subplots_adjust(left=0, right=1, top=1, bottom=0,
wspace=0, hspace=0)
plt.margins(0, 0)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
if label_names is None:
label_names = [str(l) for l in range(label.max() + 1)]
if colormap is None:
colormap = label_colormap(len(label_names))
label_viz = label2rgb(label, img, n_labels=len(label_names))
plt.imshow(label_viz)
plt.axis('off')
plt_handlers = []
plt_titles = []
for label_value, label_name in enumerate(label_names):
if label_value not in label:
continue
if label_name.startswith('_'):
continue
fc = colormap[label_value]
p = plt.Rectangle((0, 0), 1, 1, fc=fc)
plt_handlers.append(p)
plt_titles.append('{value}: {name}'
.format(value=label_value, name=label_name))
plt.legend(plt_handlers, plt_titles, loc='lower right', framealpha=.5)
f = io.BytesIO()
plt.savefig(f, bbox_inches='tight', pad_inches=0)
plt.cla()
plt.close()
plt.switch_backend(backend_org)
out_size = (label_viz.shape[1], label_viz.shape[0])
out = PIL.Image.open(f).resize(out_size, PIL.Image.BILINEAR).convert('RGB')
out = np.asarray(out)
return out
需要注意的,完成draw.py修改之后,要记得在utils文件夹中__init__.py文件中对draw_label进行import。
from .draw import draw_label
如果utils文件夹中甚至没有draw.py文件,可以参考https://blog.csdn.net/yql_617540298/article/details/104182777中的完整draw.py代码进行尝试,看评论区有一些小伙伴成功了。不过我使用该draw.py代码还是会报错,建议没有该draw.py文件的小伙伴使用方法二。
方法二
参考了https://blog.csdn.net/szumaine/article/details/104407182文章,并在代码上做了一些修改(原博文中的代码无法生成yaml文件)。这种方法基于原始的json_to_dataset代码进行修改,则无需使用draw_label,避免了对draw.py进行修改,我个人认为这一方法比较方便。
同样,找到json_to_dataset.py文件(可以参考我的路径找到该py文件:/home/anaconda2/envs/labelme/lib/python3.9/site-packages/labelme/cli),将以下代码复制到json_to_dataset.py文件中即可:
import yaml
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
from math import *
import numpy as np
import random
def main():
list_path = os.listdir('/media/DATA/STUDY/picture/json') # 这是我的路径,替换成自己的就好
for i in range(0, len(list_path)):
logger.warning('This script is aimed to demonstrate how to convert the'
'JSON file to a single image dataset, and not to 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 = '/media/DATA/STUDY/picture/json/' + list_path[i] # 这是我的路径,替换成自己的就好
print(list_path[i])
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) # 用于以数字权限模式创建目录
data = json.load(open(json_file))
imageData = data.get('imageData')
if not imageData:
imagePath = os.path.join(os.path.dirname(json_file), data['imagePath']) # os.path.dirname返回文件路径
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'
)
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.warning('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)
logger.info('Saved to: {}'.format(out_dir))
x = out_dir+'\\label.png'
if __name__ == '__main__':
main()
需要注意的是,代码中包含了需要转换的json文件路径(所有待转换的json文件需放在该文件夹内),有两处需要依据自己保存json文件的路径进行修改,注释已经标明。
如果再次使用该函数时需要修改路径,则在这两处修改。
#第一次在CSDN写文章,主要是做批量转换的时候踩了好多坑,所以整理一下各位大佬分享的有效方法,写出来和大家分享QAQ#