cityscapes数据集json转txt,用于yolov5和yolov8目标检测

最近在用cityscapes的目标检测数据集测试自己训练的模型,但是cityscapes中有些类别与我的不一样,我有的他没有,他有的我不需要,为了统一标签序号,仅保留cityscapes中我需要的类别,将 json转成txt格式,设置了如下代码,需要的朋友看懂代码后自行修改使用。

import json
import os
import glob
import os.path as osp

def cityscapes_json_txt(jsonfilePath="", resultDirPath="", classList=["road", "others"]):
    # 0.创建保存转换结果的文件夹
    if(not os.path.exists(resultDirPath)):
        os.mkdir(resultDirPath)

    # 1.获取目录下所有的labelme标注好的Json文件,存入列表中
    jsonfileList = glob.glob(osp.join(jsonfilePath, "*.json"))
    # print(jsonfileList)  # 打印文件夹下的文件名称

    # 2.遍历json文件,进行转换
    for jsonfile in jsonfileList:
        # 3. 打开json文件
        with open(jsonfile, "r") as f:
            file_in = json.load(f)
            # 4. 读取文件中记录的所有标注目标
            imgHeight = file_in["imgHeight"]
            imgWidth = file_in["imgWidth"]
            objects = file_in["objects"]
            dw = 1.0 / imgWidth
            dh = 1.0 / imgHeight

            # 5. 使用图像名称创建一个txt文件,用来保存数据
            txt_path = resultDirPath + jsonfile.split("\\")[-1][:-20] + 'leftImg8bit.txt'
            # 6. 遍历shapes中的每个目标的轮廓
            strs = ""
            for shape in objects:
                # 7.根据json中目标的类别标签,从classList中寻找类别的ID,然后写入txt文件中
                if shape["label"] in classList:
                    # 8. 遍历shape轮廓中的每个点,每个点要进行图像尺寸的缩放,即x/width, y/height
                    x_list = []
                    y_list = []
                    for point in shape['polygon']:
                        x_list.append(point[0])
                        y_list.append(point[1])
                    x_max = max(x_list)
                    x_min = min(x_list)
                    y_max = max(y_list)
                    y_min = min(y_list)
                    strs += str(classList.index(shape['label']))
                    strs += " "
                    strs += str(((x_min + x_max) / 2.0) * dw)[0:8]
                    strs += " "
                    strs += str(((y_min + y_max) / 2.0) * dh)[0:8]
                    strs += " "
                    strs += str(((x_max - x_min)) * dw)[0:8]
                    strs += " "
                    strs += str(((y_max - y_min)) * dh)[0:8]
                    strs += "\n"
            #9.判断该文件是否有标签,是否保存
            # if strs != "":
            write = open(txt_path, 'w')
            write.writelines(strs)
            write.close()
            # else:
            #     print("%s has been dealt!" % txt_path)
        # 10.所有物体都遍历完,需要关闭文件
        f.close()

if __name__ == "__main__":
	#我将cityscapes中的json文件放到一个文件夹下
    jsonfilePath = r"D:\yolo\yolov8\data\citys\json\train/"  # 要转换的json文件所在目录
    resultDirPath = r"D:\yolo\yolov8\data\citys\labels\train/"  # 要生成的txt文件夹
    classList = ['traffic light', 'traffic sign', 'car', 'person', 'bus', 'truck', 'riderss',
                 'bike', 'motor', 'cone', 'barrier', 'opendoor', 'wheel', 'tail']
    cityscapes_json_txt(jsonfilePath=jsonfilePath, resultDirPath=resultDirPath, classList=classList)

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐