『BDD100K的labels文件json转YOLOV5要求的txt文件』【包括识别交通灯】(代码编写)

『BDD100K的labels文件json转YOLOV5要求的txt文件』【包括识别交通灯】(代码编写)

import os

import json





class BDD_to_YOLOv5:

    def __init__(self):

        self.writepath = "BDD100K/labels/trains/"

        self.bdd100k_width_ratio = 1.0/1280

        self.bdd100k_height_ratio = 1.0/720

        self.select_categorys = ["person", "rider", "car", "bus", "truck", "bike","motor", "traffic light", "traffic sign", "train"]

        self.categorys_nums = {

            "person": 0,

            "rider": 1,

            "car": 2,

            "bus": 3,

            "truck": 4,

            "bike": 5,

            "motor": 6,

            "tl_green": 7,

            "tl_red": 8,

            "tl_yellow": 9,

            "tl_none": 10,

            "traffic sign": 11,

            "train": 12

        }



    def bdd_to_yolov5(self, path):

        lines = ""

        with open(path) as fp:

            j = json.load(fp)

            write = open(self.writepath + "%s.txt" % j["name"], 'w')

            for fr in j["frames"]:

                for objs in fr["objects"]:

                    if objs["category"] in self.select_categorys:

                        temp_category=objs["category"]

                        if (temp_category == "traffic light"):

                            color = objs["attributes"]["trafficLightColor"]

                            temp_category="tl_"+color

                        idx = self.categorys_nums[temp_category]

                        cx = (objs["box2d"]["x1"] + objs["box2d"]["x2"]) / 2.0

                        cy = (objs["box2d"]["y1"] + objs["box2d"]["y2"]) / 2.0

                        w = objs["box2d"]["x2"] - objs["box2d"]["x1"]

                        h = objs["box2d"]["y2"] - objs["box2d"]["y1"]

                        if w <= 0 or h <= 0:

                            continue

                        # 根据图片尺寸进行归一化

                        cx, cy, w, h = cx * self.bdd100k_width_ratio, cy * self.bdd100k_height_ratio, w * self.bdd100k_width_ratio, h * self.bdd100k_height_ratio

                        line = f"{idx} {cx:.6f} {cy:.6f} {w:.6f} {h:.6f}\n"

                        lines += line

                if len(lines) != 0:

                    write.writelines(lines)

                    write.close()

                    print("%s has been dealt!" % j["name"])







if __name__ == "__main__":

    bdd_labels_dir = "BDD100K/labels/train/"

    fileList = os.listdir(bdd_labels_dir)

    obj = BDD_to_YOLOv5()

    for path in fileList:

        filepath = bdd_labels_dir+path

        print(path)

        obj.bdd_to_yolov5(filepath) 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

粥粥粥少女的拧发条鸟

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值