json采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得JSON成为理想的数据交换语言。那json文件怎么转换png文件?下面来我们就来给大家讲解一下json文件转换png文件的方法。
1)定位到Anaconda的安装目录D:\Software\anaconda\Lib\site-packages
在site_pakeages下找到lableme的文件夹:
2)进入到cli文件夹,找到json_to_dataset.py文件,将里面的代码替换成如下:
#!/usr/bin/python #- * -coding: UTF - 8 - * - import argparse import json import os import os.path as osp import base64 import warnings import PIL.Image import yaml from labelme import utils import cv2 import numpy as np from skimage import img_as_ubyte# from sys import argv 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_path = os.listdir(json_file) for i in range(0, len(list_path)): if list_path[i].endswith('.json'): path = os.path.join(json_file, list_path[i]) if os.path.isfile(path): data = json.load(open(path)) img = utils.img_b64_to_arr(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) save_file_name = osp.basename(path) .replace('.', '_') out_dir1 = osp.join(osp.dirname(path), 'labelme_results') if not osp.exists(out_dir1): os.mkdir(out_dir1) out_dir1 = osp.join(out_dir1, save_file_name) if not osp.exists(out_dir1): os.mkdir(out_dir1) PIL.Image.fromarray(img) .save(out_dir1 + '\\' + save_file_name + '_img.png') PIL.Image.fromarray(lbl) .save(out_dir1 + '\\' + save_file_name + '_label.png') PIL.Image.fromarray(lbl_viz) .save(out_dir1 + '\\' + save_file_name + '_label_viz.png') images_dir = osp.join(json_file, 'images_dir') if not osp.exists(images_dir): os.mkdir(images_dir) labels_dir = osp.join(json_file, 'labels_dir') if not osp.exists(labels_dir): os.mkdir(labels_dir) PIL.Image.fromarray(img) .save(osp.join(images_dir, '{}_img.png'.format(save_file_name))) PIL.Image.fromarray(lbl) .save(osp.join(labels_dir, '{}_label.png'.format(save_file_name))) with open(osp.join(out_dir1, 'label_names.txt'), 'w') as f: for lbl_name in lbl_names: f.write(lbl_name + '\n') info = dict(label_names = lbl_names) with open(osp.join(out_dir1, 'info.yaml'), 'w') as f: yaml.safe_dump(info, f, default_flow_style = False) print('Saved to: %s' % out_dir1) if __name__ == '__main__': #base64path = argv[1] main()
3)将utils中的文件夹中的shape.py中的文件内容改成以下:
from skimage import img_as_ubyte import numpy as np import PIL.Image import PIL.ImageDraw from labelme import logger def polygons_to_mask(img_shape, polygons): mask = np.zeros(img_shape[: 2], dtype = np.uint8) mask = PIL.Image.fromarray(mask) xy = list(map(tuple, polygons)) PIL.ImageDraw.Draw(mask) .polygon(xy = xy, outline = 1, fill = 1) mask = np.array(mask, dtype = bool) return mask def shapes_to_label(img_shape, shapes, label_name_to_value, type = 'class'): assert type in ['class', 'instance'] cls = np.zeros(img_shape[: 2], dtype = np.int32) if type == 'instance': ins = np.zeros(img_shape[: 2], dtype = np.int32) instance_names = ['_background_'] for shape in shapes: polygons = shape['points'] label = shape['label'] if type == 'class': cls_name = label elif type == 'instance': cls_name = label.split('-')[0] if label not in instance_names: instance_names.append(label) ins_id = len(instance_names) - 1 cls_id = label_name_to_value[cls_name] mask = polygons_to_mask(img_shape[: 2], polygons) cls[mask] = cls_id if type == 'instance': ins[mask] = ins_id if type == 'instance': return cls, ins return cls def labelme_shapes_to_label(img_shape, shapes): logger.warn('labelme_shapes_to_label is deprecated, so please use ' 'shapes_to_label.') label_name_to_value = { '_background_': 0 }# 注意: 需要改成自己的类别 for shape in 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 lbl = shapes_to_label(img_shape, shapes, label_name_to_value) lbl = img_as_ubyte(lbl) return lbl, label_name_to_value
3)进入D:\Software\anaconda\Scripts\下找到labelme_json_to_dataset.exe
在相应的环境下输入
labelme_json_to_dataset.exe C:\Users\86152\Desktop\json\
后面的这个路径代表的是 存放json文件的路径。
已经转换完成!
按照这些方法去做之后,就可以将json文件转换png文件了,当然json文件转换也可以批量转换成img等文件,以后我们再给大家分享哦!最后大家如果想要了解更多json工具教程知识,敬请关注奇Q工具网。
推荐阅读: