labelme根据一个标注数据生成多个相同的标注文件

labelme根据一个标注数据生成多个相同的标注文件

import os
import json

filelist = os.listdir("D:\Test_TF\Test")
filelist.sort()  # 文件名列表排序
base_path = "D:\Test_TF\Test\\"

# dic = {"labels": [{"name": "A类"}]}  # 每类图像都是一类固定的标签
# label_value = json.dumps(dic)  # 字典转换字符串
json_path = r"D:\Test_TF\Test\0531-shijihuanjing2_ExternDisk_005.json"
with open(json_path,"r") as json_file:
    dic = json.load(json_file)
    # label_value = json.dumps(dic)
    with open (json_path,"w") as f:
        label_value = json.dumps(dic)
        for name in filelist:
            # file_realname = name.translate(str.maketrans('','','.jpg'))
            file_realname = os.path.splitext(name)
            if file_realname[1]==".jpg":
                print(file_realname)
                filename = file_realname[0] + '.json'
                full_path = base_path + filename
                file = open(full_path,'w')
                file.write(label_value)
                file.close()
                # 修改 json文件 imagePath的value
                with open(full_path, 'r') as fr:
                    json_all = json.load(fr)
                    # print(type(json_all))
                    json_all['imagePath'] = file_realname[0]+'.jpg'
                with open(full_path, 'w+') as fw:
                    json.dump(json_all, fw, ensure_ascii=False, indent=4)
                file.close()


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值