更改json_to_dataset.py可以跑多个json

1.把下面的代码替换json_to_dataset.py里面的内容。如果装了anaconda并且没有用虚拟环境那么路径在e:\Program Files\Anaconda3\Lib\site-packages\labelme\cli\json_to_dataset.py。如果在虚拟环境里安装labelme那么路径在e:\Program Files\Anaconda3\envs\里。

#!/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()

2.根据json_to_dataset.py文件40行,按住ctrl鼠标单击utils.labelme_shapes_to_label函数,定位到shape.py 文件(在utils文件夹里面)。在shape.py        第一行加入 from skimage import img_as_ubyte

          修改shape.py文件的labelme_shapes_to_label()函数如下(可以直接复制替换整个函数。注意:修改自己的类别。

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,'qipao':1,'qita':2}  #注意:需要改成自己的类别
    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.然后在当前路径打开cmd(按住shift右击鼠标可以快速打开哦)或者打开cmd切换到当前目录。

  输入命令    python  json_to_dataset.py  d:\dataset(你自己的json文件夹的路径)然后就会看到结果了。

4.这样做出的类别图可以保证类别统一。imges_dir存放原图labels_dir就是对应的类别图。

Goodluck!

你可能感兴趣的:(数据处理)