python生成分类问题的LIST标签json文件

python生成分类问题LIST的json文件


import os
import json
import glob

def get_annotation_dict(input_folder_path):
      label_dict = {}
      image_set = []
      annonation_set = []
      # 主目录
      father_file_list = os.listdir(input_folder_path)

      # class 目录
      count = 0
      for class_file in father_file_list:
          full_class_file  = os.path.join(input_folder_path, class_file)
          class_file_list = os.listdir(full_class_file)
          # 相对路径的路径
          class_file_com = 'train_val2019/'+class_file
          # print(len([x for x in os.listdir(os.path.dirname(class_file_list)) if os.path.isfile(x)]),'当前文件数')
          for image_name in class_file_list:
              image_file_id ={}
              annonation_id_catgoly = {}
              image_file_id["filename"] = os.path.join(class_file_com, image_name)
              image_file_id["id"] = count
              annonation_id_catgoly['id'] = count
              annonation_id_catgoly['category_id'] = class_file
              image_set.append(image_file_id)
              annonation_set.append(annonation_id_catgoly)
              count = count +1
      label_dict['images'] = image_set
      print(image_set,'image')
      label_dict['annotation'] = annonation_set
      print(annonation_set,'ann')

      return label_dict

def save_json(label_dict, json_path):
    with open(json_path, 'w') as json_path:
        json.dump(label_dict, json_path)
        print('label json file has been generated successfully')


if __name__ == '__main__':
     input_folder_file = '/home/chensheng/code/DL/STEGO/train_val2019'
     label_dict = {}
     label_dict = get_annotation_dict(input_folder_file)
     json_path = '/home/chensheng/code/DL/STEGO/train2019.json'
     save_json(label_dict, json_path)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值