话不多说 直接附代码
这个代码主要是解析一个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文件分别对应各个大类和序号,