如何将labelme生成的json文件转换成png图片
- 单个转换(费时)
1.先进入到你存放json的文件夹的磁盘中,输入d:
2. 激活labelme环境
3. 输入labelme_json_to_dataset并进入到存放json的文件夹
labelme_json_to_dataset D:\data
4.完成转换
- 批量转换(等我更新)
- 更新来啦!!!(步骤)
1、修改json_to_dataset.py代码
这里它的地址为D:\Anaconda3\envs\labelme\Lib\site-packages\labelme\cli\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()
2、找到json_to_dataset.exe的地址
这里我的地址是:D:\Anaconda3\envs\labelme\Scripts
3、在cmd中操作文章来源:https://www.toymoban.com/news/detail-538763.html
- 激活labelme
在cmd中输入activate labelme - 切换到d盘,然后转到json_to_dataset.exe的地址
- 输入labelme_json_to_dataset.exe和生成的json文件夹
- 最后生成的文件在D:\Anaconda3\envs\labelme\Scripts地址中
4、可能错误
如果出现 module ‘labelme.utils’ has no attribute 'draw_label’错误,应该是 labelme版本问题。在cmd中输入pip install labelme==3.16.2,下载 labelme3.16.2版本
参考链接:
https://blog.csdn.net/weixin_44715623/article/details/106807501?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-0&spm=1001.2101.3001.4242
https://blog.csdn.net/weixin_47286519/article/details/116993802文章来源地址https://www.toymoban.com/news/detail-538763.html
到了这里,关于如何将labelme生成的json文件转换成png图片(亲测有效)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!