YOLOV5——将 json 格式的标注数据转化为 YOLO 需要的 txt 格式
YOLO 需要的标注数据是每个图片一个 txt 文件
json 标注数据文件内容包含:
name:图片文件名
category:类别id
bbox:目标框信息xyrb格式,分别指[左上角x坐标,左上角y坐标,右下角x坐标,右下角y坐标]
score:预测的分数
如下格式
[ { "name": "235_2_t20201127123021723_CAM2.jpg", "image_height": 6000, "image_width": 8192, "category": 5, "bbox": [ 1876.06, 998.04, 1883.06, 1004.04 ] }, { "name": "235_2_t20201127123021723_CAM2.jpg", "image_height": 6000, "image_width": 8192, "category": 5, "bbox": [ 1655.06, 1094.04, 1663.06, 1102.04 ] }, { "name": "235_2_t20201127123021723_CAM2.jpg", "image_height": 6000, "image_width": 8192, "category": 5, "bbox": [ 1909.06, 1379.04, 1920.06, 1388.04 ] } ]
我们需要将 json 中的数据转化为每个文件一个 txt 文件
其中 yolo 需要的标注数据格式:
235_2_t20201127123021723_CAM2.txt 文件
数据格式:类别id 中心点x坐标 中心点y坐标 w h(相对于图片宽高)
5 0.229438 0.16684 0.000854 0.001 5 0.202522 0.183007 0.000977 0.001333
json2txt 代码
import os import json json_dir = 'train_annos.json' # json文件路径 out_dir = 'output/' # 输出的 txt 文件路径 def main(): # 读取 json 文件数据 with open(json_dir, 'r') as load_f: content = json.load(load_f) # 循环处理 for t in content: tmp = t['name'].split('.') filename = out_dir + tmp[0] + '.txt' if os.path.exists(filename): # 计算 yolo 数据格式所需要的中心点的 相对 x, y 坐标, w,h 的值 x = (t['bbox'][0] + t['bbox'][2]) / 2 / t['image_width'] y = (t['bbox'][1] + t['bbox'][3]) / 2 / t['image_height'] w = (t['bbox'][2] - t['bbox'][0]) / t['image_width'] h = (t['bbox'][3] - t['bbox'][1]) / t['image_height'] fp = open(filename, mode="r+", encoding="utf-8") file_str = str(t['category']) + ' ' + str(round(x, 6)) + ' ' + str(round(y, 6)) + ' ' + str(round(w, 6)) + \ ' ' + str(round(h, 6)) line_data = fp.readlines() if len(line_data) != 0: fp.write('\n' + file_str) else: fp.write(file_str) fp.close() # 不存在则创建文件 else: fp = open(filename, mode="w", encoding="utf-8") fp.close() if __name__ == '__main__': main()
拓展:将yolov5输出为数据改为json格式,原代码只输出标志文件
https://www.cnblogs.com/yxyun/p/14250455.html
分类:
Python常用方法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了