链接:https://pan.baidu.com/s/1AI1gpKZETjby81hGSSuzbg 
提取码:ikun 

json转txt代码如下:

import re
import os
import json


def search_file(data_dir, pattern=r'\.jpg$'):
    root_dir = os.path.abspath(data_dir)
    for root, dirs, files in os.walk(root_dir):
        for f in files:
            if re.search(pattern, f, re.I):
                abs_path = os.path.join(root, f)
                # print('new file %s' % absfn)
                yield abs_path


class Bdd2yolov5:
    def __init__(self):
        self.bdd100k_width = 1280
        self.bdd100k_height = 720
        self.select_categorys = ["person", "rider", "car", "bus", "truck", "bike", "motor"]   # 自己需要的类别
        self.cat2id = {
            "person": 0,   # 数字代表目标类别
            "rider": 0,
            "car": 1,
            "bus": 1,
            "truck": 1,
            "bike": 1,
            "motor": 1
        }

    @property
    def all_categorys(self):
        return ["person", "rider", "car", "bus", "truck", "bike",        # 人 骑手 车 公交车 货车 自行车
                "motor", "traffic light", "traffic sign", "train"]       # 摩托 交通信号灯 交通标志 火车

    def _filter_by_attr(self, attr=None):
        if attr is None:
            return False
            # 过滤掉晚上的图片
        # if attr['timeofday'] == 'night':
        #     return True
        # return False

    def _filter_by_box(self, w, h):
        # size ratio
        # 过滤到过于小的小目标
        # threshold = 0.001
        # if float(w * h) / (self.bdd100k_width * self.bdd100k_height) < threshold:
        #     return True
        return False

    def bdd2yolov5(self, path):
        lines = ""
        with open(path) as fp:
            j = json.load(fp)
            if self._filter_by_attr(j['attributes']):
                return
            for fr in j["frames"]:
                dw = 1.0 / self.bdd100k_width
                dh = 1.0 / self.bdd100k_height
                for obj in fr["objects"]:
                    if obj["category"] in self.select_categorys:
                        idx = self.cat2id[obj["category"]]
                        cx = (obj["box2d"]["x1"] + obj["box2d"]["x2"]) / 2.0
                        cy = (obj["box2d"]["y1"] + obj["box2d"]["y2"]) / 2.0
                        w = obj["box2d"]["x2"] - obj["box2d"]["x1"]
                        h = obj["box2d"]["y2"] - obj["box2d"]["y1"]
                        if w <= 0 or h <= 0:
                            continue
                        if self._filter_by_box(w, h):
                            continue
                        # 根据图片尺寸进行归一化
                        cx, cy, w, h = cx * dw, cy * dh, w * dw, h * dh
                        line = f"{idx} {cx:.6f} {cy:.6f} {w:.6f} {h:.6f}\n"
                        lines += line
                if len(lines) != 0:
                    # 转换后的以*.txt结尾的标注文件我就直接和*.json放一具目录了
                    yolo_txt = path.replace(".json", ".txt")
                    with open(yolo_txt, 'w') as fp2:
                        fp2.writelines(lines)
                # print("%s has been dealt!" % path)


if __name__ == "__main__":
    bdd_label_dir = r"D:\BaiduNetdiskDownload\BDD100K\bdd100k\labels\100k\val"
    cvt = Bdd2yolov5()
    for path in search_file(bdd_label_dir, r"\.json$"):
        cvt.bdd2yolov5(path)


 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐