python将json格式标签转换为txt格式标签(yolo格式)

该代码实现了一个Python脚本,主要功能是从指定的JSON文件中读取数据,将数据转换成多个TXT文件,并执行XYXY到XYWH的坐标转换。它首先读取JSON文件,然后遍历内容,根据图片路径创建对应的TXT文件,写入转换后的坐标信息。此外,还包含了一个方法用于在TXT文件中添加序号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

话不多说 直接附代码
这个代码主要是解析一个json文件 转换成多个txt文件使用的
并且进行了xyxy—>xywh的转换,如果不需要就不要调用xyxyToXywh方法
基本上修改的话就是json_dir out_dir 需要修改
write_txt 方法内地址需要修改
get_json方法中读取的内容需要修改,没了

import os
import json
import jsonpath
import numpy as np

json_dir = r'D:\desktop\data_set\StanfordExtra\stanfordextra_v12\StanfordExtra_v12.json'  # json文件路径
out_dir = r'D:\desktop\data_set\StanfordExtra\labels'  # 输出的 txt 文件路径


def get_json():
    # 读取 json 文件数据
    object = json.load(open(json_dir, 'r', encoding='utf-8'))
    for le in object:
        path = le["img_path"]  # 名称
        imgWidth =  le['img_width']
        imgHeight = le['img_height']
        img_bbox = le['img_bbox']
        strTxt = ''
        x,y,w,h = xyxyToXywh(img_bbox,imgWidth,imgHeight)   # 对数据进行转换
        strTxt +=  str(x) + ' ' + str(y) + ' ' + str(w) + ' ' + str(h)
        print('strtxt -----' ,strTxt)
        print(path)  # 获取名称
        dirPath = path.split('/')
          # 文件夹名称

        print_dir = out_dir + '\\' + dirPath[0]
        if os.path.exists(print_dir) != True:
            os.mkdir(print_dir)  # 创建文件夹
        jpgName = dirPath[1].split('.')  # 这个jpg就是名称去掉.jpg
        with open(print_dir + "\\" + jpgName[0] + '.txt', "w", encoding="utf-8") as txt:
            txt.write(strTxt)
            txt.close()


def xyxyToXywh(imgPathList,imgWidth,imgHeight):
    x = np.round(abs(imgPathList[0] + imgPathList[2]/ 2)  / imgWidth,6)
    y = np.round(abs(imgPathList[1] + imgPathList[3]/ 2)  / imgHeight,6)
    w = imgPathList[2]  
    h = imgPathList[3]   
    w = np.round(float(w) / imgWidth,6)
    h = np.round(float(h) / imgWidth,6)
    print(x,y,w,h)
    return x,y,w,h

# 根据地址循环文件夹数量
def add_kind(dir):
    i = -2
    for root, dirs, files in os.walk(dir):
        i = i+1
        for file in files:
            strtxt = ''
             # 打开文件 读取文件第一行
            with open(os.path.join(root, file), 'r', encoding='utf-8') as f:
               line = f.readline()
               strtxt += str(i) + ' ' + str(line)
            with open(os.path.join(root, file), 'w', encoding='utf-8') as f:
                f.write(strtxt)

# 生成种类txt对应文件
def write_txt(dir):
    # dir是目标文件夹
    i = -1
    with open(r'D:\desktop\data_set\StanfordExtra\class\classify.txt', 'w+', encoding='utf-8') as f:
        pass
    with open(r'D:\desktop\data_set\StanfordExtra\class\classify.json', 'w+', encoding='utf-8') as f:
        f.write('[')
    for root, dirs, files in os.walk(dir):
        for dirr in dirs:
            i = i + 1
            with open(r'D:\desktop\data_set\StanfordExtra\class\classify.txt', 'a', encoding='utf-8') as f:
                f.write(str(i) + ' ' + dirr + '\n')
            with open(r'D:\desktop\data_set\StanfordExtra\class\classify.json', 'a', encoding='utf-8') as f:
                f.write('\''+ dirr+'\'' +',')
    with open(r'D:\desktop\data_set\StanfordExtra\class\classify.json', 'a', encoding='utf-8') as f:
        f.write(']')

def main():
    get_json()      # 解释json文件,转成txt文件
    add_kind(out_dir)
    # write_txt(out_dir)   # 生成种类txt对应文件
    print('结束')

if __name__ == '__main__':
    main()



三个方法中get_json是读取json文件,并按照json语法来读取json对象
add_kind方法是用于将json对象内容写入txt文件
write_txt 是生成一个txt文件分别对应各个大类和序号,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值