yolo txt 转 labelme json 格式

talk is cheap show me the code!

复制代码
def convert_txt_to_labelme_json(txt_path, image_path, output_dir, image_fmt='.jpg'):
    # txt 转labelme json
    # 将yolo的txt转labelme json
    txts = glob.glob(os.path.join(txt_path, "*.txt"))
    for txt in txts:
        labelme_json = {
            'version': '4.5.7',
            'flags': {},
            'shapes': [],
            'imagePath': None,
            'imageData': None,
            'imageHeight': None,
            'imageWidth': None,
        }
        txt_name = os.path.basename(txt)
        image_name = txt_name.split(".")[0] + image_fmt
        labelme_json['imagePath'] = image_name
        image_name = os.path.join(image_path, image_name)
        if not os.path.exists(image_name):
            raise Exception('txt 文件={},找不到对应的图像={}'.format(txt, image_name))
        image = cv2.imdecode(np.fromfile(image_name, dtype=np.uint8), cv2.IMREAD_COLOR)
        h, w = image.shape[:2]
        labelme_json['imageHeight'] = h
        labelme_json['imageWidth'] = w
        with open(txt, 'r') as t:
            lines = t.readlines()
            for line in lines:
                content = line.split(' ')
                label = content[0]
                object_width = float(content[3])
                object_height = float(content[4])
                top_left_x = (float(content[1]) - object_width / 2) * w
                top_left_y = (float(content[2]) - object_height / 2) * h
                bottom_right_x = (float(content[1]) + object_width / 2) * w
                bottom_right_y = (float(content[2]) + object_height / 2) * h
                shape = {
                    'label': str(label),
                    'group_id': None,
                    'shape_type': 'rectangle',
                    'flags': {},
                    'points': [
                        [float(top_left_x), float(top_left_y)],
                        [float(bottom_right_x), float(bottom_right_y)]
                    ]
                }
                labelme_json['shapes'].append(shape)
            json_name = txt_name.split('.')[0] + '.json'
            json_name_path = os.path.join(output_dir, json_name)
            fd = open(json_name_path, 'w')
            json.dump(labelme_json, fd, indent=4)
            fd.close()
            print("save json={}".format(json_name_path))

附 Yolo 坐标系格式: https://roboflow.com/formats/yolov5-pytorch-txt

相关推荐
福大大架构师每日一题3 小时前
YOLO v8.4.56 修复 QNN 导出兼容性:builtin provider wheels 也能稳定导出,Linux x86-64 更友好
linux·运维·yolo
YOLO数据集集合5 小时前
低空林业巡检数据集|生态监测树木识别|深度学习树种分类数据集
人工智能·深度学习·yolo·目标检测·分类·无人机
stsdddd7 小时前
YOLO系列目标检测数据集大全【第三期】
yolo·目标检测·目标跟踪
YOLO数据集集合7 小时前
无人机航拍人体检测数据集|低空巡检搜救智能监控|YOLO目标检测算法训练集
人工智能·深度学习·yolo·目标检测·无人机
深度学习lover8 小时前
<数据集>yolo个人防护用品识别<目标检测>
人工智能·yolo·目标检测·安全帽识别·安全背心识别·安全手套识别·防护靴识别
动物园猫8 小时前
田间杂草检测数据集分享(适用于YOLO系列深度学习分类检测任务)
深度学习·yolo·分类
吴声子夜歌8 小时前
PlantUML——显示JSON数据
json
祀爱9 小时前
ControllerBase 类将对象转换为 JSON 格式并返回前端的方法
前端·json·asp.net
毕设做完了吗?9 小时前
YOLO+paddlecor的智能车牌识别系统
人工智能·python·yolo·目标检测·计算机视觉
weelinking1 天前
【产品】11_实现后端接口——数据在背后如何流动
java·人工智能·python·sql·oracle·json·ai编程