labelme coco

labelme coco   好久没用了

 

#!/usr/bin/env python
# coding: utf-8

# In[12]:


import argparse
import json
import os
import os.path as osp
import warnings
import numpy as np
import PIL.Image
import yaml
from labelme import utils


def main():
    json_file='G:\\20220414_json\\'
    out_dir = 'G:\\20220414_out\\'
    list = os.listdir(json_file)
    for i in range(0, len(list)):
        path = os.path.join(json_file, list[i])
        if os.path.isfile(path):
            print(path)
            data = json.load(open(path,'rb'))
            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)
            out_folder = osp.basename(list[i]).replace('.', '_')
            out_folder = osp.join(out_dir, out_folder)
            if not osp.exists(out_folder):
                os.mkdir(out_folder)
            PIL.Image.fromarray(img).save(osp.join(out_folder, 'img.png'))
            PIL.Image.fromarray(lbl).save(osp.join(out_folder, 'label.png'))
            PIL.Image.fromarray(lbl_viz).save(osp.join(out_folder, 'label_viz.png'))
            with open(osp.join(out_folder, 'label_names.txt'), 'w') as f:
                for lbl_name in lbl_names:
                    f.write(lbl_name + '\n')
            warnings.warn('info.yaml is being replaced by label_names.txt')
            info = dict(label_names=lbl_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)




# In[2]:


#utils.__file__


# In[13]:


if __name__ == '__main__':
    main()


# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:

 

 

 

 

 

 

###########################

posted @ 2022-04-15 22:06  西北逍遥  阅读(30)  评论(0编辑  收藏  举报