当前位置: 首页 > news >正文

json格式数据集转换成yolo的txt格式数据集

这个代码是参考了两个博客  我是感觉第一篇博客可能有问题,然后自己做了改进,如果我是错误的或者正确的,请各位评论区说一下,感谢

Json格式的数据集标签转化为有效的txt格式(data_coco)_train.json-CSDN博客

COCO(.json)格式 转换为 YOLO(.txt)格式训练(详细介绍,避坑贴)_json to yolo-CSDN博客

# 处理同一个数据集下多个json文件时,仅运行一次class_txt即可
import json
import osdef convert2(size, box):dw = 1.0 / size[0]dh = 1.0 / size[1]x = (box[0] + box[1]) / 2.0y = (box[2] + box[3]) / 2.0w = box[1] - box[0]h = box[3] - box[2]x = x * dww = w * dwy = y * dhh = h * dhreturn (x, y, w, h)def convert(size, box):dw = 1. / (size[0])dh = 1. / (size[1])x = box[0] + box[2] / 2.0y = box[1] + box[3] / 2.0w = box[2] - box[0]h = box[3] - box[1]# round函数确定(xmin, ymin, xmax, ymax)的小数位数x = round(x * dw, 6)w = round(w * dw, 6)y = round(y * dh, 6)h = round(h * dh, 6)return (x, y, w, h)
"存储标签与预测框到txt文件中"
def json_txt(json_path, txt_path):"json_path: 需要处理的json文件的路径""txt_path: 将json文件处理后txt文件存放的文件夹名"# 生成存放json文件的路径if not os.path.exists(txt_path):os.mkdir(txt_path)# 读取json文件with open(json_path, 'r') as f:dict = json.load(f)# 得到images和annotations信息images_value = dict.get("images")  # 得到某个键下对应的值annotations_value = dict.get("annotations")  # 得到某个键下对应的值# 使用images下的图像名的id创建txt文件'''list=[]  # 将文件名存储在list中for i in images_value:open(txt_path + str(i.get("file_name")) + '.txt', 'w')list.append(i.get("id"))for i in images_value:for j in annotations_value:if j.get("image_id") == i.get("id"):# bbox标签归一化处理num = sum(j.get('bbox'))new_list = [round(m / num, 6) for m in j.get('bbox')]  # 保留六位小数with open(txt_path + str(i.get("file_name")) + '.txt', 'a') as file1:  # 写入txt文件中print(j.get("category_id"), new_list[0], new_list[1], new_list[2], new_list[3], file=file1)print("结束")
'''# 将id对应图片的bbox写入txt文件中for i in images_value:for j in annotations_value:if j.get("image_id") == i.get("id"):# bbox标签归一化处理w = i.get("width")h = i.get("height")bbox = j.get('bbox')  # 保留六位小数xmin = bbox[0]xmax = bbox[1]ymin = bbox[2]ymax = bbox[3]b = (float(xmin), float(xmax), float(ymin), float(ymax))new_list = convert((w, h), b)with open(txt_path + str(i.get("file_name")) + '.txt', 'a') as file1:  # 写入txt文件中print(j.get("category_id"), str(new_list[0]), str(new_list[1]), str(new_list[2]), str(new_list[3]), file=file1)print("结束")"将id对应的标签存储在class.txt中"
def class_txt(json_path, class_txt_path):"json_path: 需要处理的json文件的路径""txt_path: 将json文件处理后存放所需的txt文件名"# 生成存放json文件的路径with open(json_path, 'r') as f:dict = json.load(f)# 得到categories下对应的信息categories_value = dict.get("categories")  # 得到某个键下对应的值# 将每个类别id与类别写入txt文件中with open(class_txt_path, 'a') as file0:for i in categories_value:print(i.get("id"), i.get('name'), file=file0)print("结束")json_txt("./annotations/val.json", "train_labels/")
#class_txt("./annotations/train.json", "id_categories.txt")

http://www.lryc.cn/news/491264.html

相关文章:

  • 什么是Three.js,有什么特点
  • Linux笔记--基于OCRmyPDF将扫描件PDF转换为可搜索的PDF
  • Unity 导出 Xcode 工程 修改 Podfile 文件
  • UE5 slate BlankProgram独立程序系列
  • 内存不足引发C++程序闪退崩溃问题的分析与总结
  • C++ —— 以真我之名 如飞花般绚丽 - 智能指针
  • Linux中安装InfluxDB
  • nginx服务器实现上传文件功能_使用nginx-upload-module模块
  • ORB-SLAM2源码学习:Initializer.cc:Initializer::ComputeF21地图初始化——计算基础矩阵
  • C# 读取多条数据记录导出到 Word标签模板之图片输出改造
  • NSSCTF web刷题
  • 对象排序得到方式
  • Day2 洛谷1035+1047+1085+1089+1150+1151
  • Linux:进程间通信之进程池和日志
  • 详细介绍HTTP与RPC:为什么有了HTTP,还需要RPC?
  • Paddle Inference部署推理(十二)
  • 外观模式 (Facade Pattern)
  • 人工智能-深度学习-Torch框架-手动构建回归流程
  • SpringBoot源码解析(五):准备应用环境
  • MySQL面试-1
  • nginx配置不缓存资源
  • PHP导出EXCEL含合计行,设置单元格格式
  • RabbitMQ 之 死信队列
  • 【创建型设计模式】单例模式
  • Charles抓包工具-笔记
  • Go语言使用 kafka-go 消费 Kafka 消息教程
  • 【论文笔记】Number it: Temporal Grounding Videos like Flipping Manga
  • C语言菜鸟入门·关键字·int的用法
  • 基于企业微信客户端设计一个文件下载与预览系统
  • 昇思MindSpore第七课---文本解码原理