bdd100k数据集中目标检测标签格式转换(json转txt)用于yolov5和yolov8
最近在用bdd100k的目标检测数据集测试自己训练的模型,但是bdd100k中有些类别与我的不一样,我有的他没有,他有的我不需要,为了统一标签序号,仅 保留bdd100k中我需要的类别,将 json转成txt格式,设置了如下代码,需要的朋友看懂代码后自行修改使用。
代码没啥技巧,如果有更简单的写法,欢迎在评论区留言,我也想学习学习。
import json
import os
def bdd100k_json_txt(categorys,jsonFile,writepath):
f = open(jsonFile)
info = json.load(f)
#print(len(info))
#print(info["name"])
for obj in info["frames"]:
#print(obj["objects"])
strs = ""
for objects in obj["objects"]:
#print(objects)
if objects["category"] in categorys:
dw = 1.0 / 1280
dh = 1.0 / 720
strs += str(categorys.index(objects["category"]))
strs += " "
strs += str(((objects["box2d"]["x1"] + objects["box2d"]["x2"]) / 2.0) * dw)[0:8]
strs += " "
strs += str(((objects["box2d"]["y1"] + objects["box2d"]["y2"]) / 2.0) * dh)[0:8]
strs += " "
strs += str(((objects["box2d"]["x2"] - objects["box2d"]["x1"])) * dw)[0:8]
strs += " "
strs += str(((objects["box2d"]["y2"] - objects["box2d"]["y1"])) * dh)[0:8]
strs += "\n"
if strs!="":
write = open(writepath + "%s.txt" % info["name"], 'w')
write.writelines(strs)
write.close()
# print("%s has been dealt!" % info["name"])
else:
print("%s has been dealt!" % info["name"])
if __name__ == "__main__":
####################args#####################
categorys = ['traffic light', 'traffic sign', 'car', 'person', 'bus', 'truck', 'riderss',
'bike', 'motor', 'cone', 'barrier', 'opendoor', 'wheel', 'tail'] # 自己需要从BDD数据集里提取的目标类别
readpath = r"D:\yolo\yolov8\data\bdd100k\det_annotations\train/" # BDD数据集标签读取路径,这里需要分两次手动去修改train、val的地址
writepath = r"D:\yolo\yolov8\data\bdd100k\labels\train/" # BDD数据集转换后的标签保存路径
fileList = os.listdir(readpath)
#print(fileList)
for file in fileList:
# print(file)
filepath = readpath + file
bdd100k_json_txt(categorys, filepath, writepath)