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

json转成yolo用的txt(json中没有宽高,需要自设宽高的)

第一章  导包

import os
import json
import argparse

第二章 类别 

class_mapping = {"自己的类别": 0}

第三章 多边形点集转换为边界框的函数

def convert_polygon_to_bbox(points):"""将多边形点集转换为边界框"""x_coords = [point[0] for point in points]y_coords = [point[1] for point in points]xmin = min(x_coords)ymin = min(y_coords)xmax = max(x_coords)ymax = max(y_coords)return xmin, ymin, xmax, ymax

第四章 转换函数

def convert_json_to_yolo(json_path, txt_path, image_width, image_height):"""将单个JSON文件转换为YOLO格式的TXT文件"""with open(json_path, 'r') as f:data = json.load(f)with open(txt_path, 'w') as f:for shape in data.get("shapes", []):label = shape["label"]if label not in class_mapping:print(f"警告: 未定义的类别 '{label}',跳过此对象")continueclass_id = class_mapping[label]points = shape["points"]xmin, ymin, xmax, ymax = convert_polygon_to_bbox(points)# 计算归一化的边界框坐标x_center = (xmin + xmax) / (2 * image_width)y_center = (ymin + ymax) / (2 * image_height)width = (xmax - xmin) / image_widthheight = (ymax - ymin) / image_height# 写入YOLO格式的行f.write(f"{class_id} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f}\n")

第五章 批量转换函数

def batch_convert(json_dir, txt_dir, image_width, image_height):"""批量转换JSON文件夹中的所有文件"""# 确保输出目录存在os.makedirs(txt_dir, exist_ok=True)# 获取所有JSON文件json_files = [f for f in os.listdir(json_dir) if f.lower().endswith('.json')]if not json_files:print(f"错误: 在 {json_dir} 中未找到JSON文件")returntotal = len(json_files)success = 0print(f"开始批量转换: {total} 个JSON文件")for i, json_file in enumerate(json_files, 1):json_path = os.path.join(json_dir, json_file)txt_file = os.path.splitext(json_file)[0] + '.txt'txt_path = os.path.join(txt_dir, txt_file)try:convert_json_to_yolo(json_path, txt_path, image_width, image_height)success += 1print(f"[{i}/{total}] 成功转换: {json_file} -> {txt_file}")except Exception as e:print(f"[{i}/{total}] 转换失败: {json_file} - 错误: {str(e)}")print(f"转换完成: 成功 {success}/{total}")

第六章 主函数

def main():parser = argparse.ArgumentParser(description='批量将JSON标注文件转换为YOLO格式的TXT文件')parser.add_argument('--json_dir', required=True, help='JSON文件所在目录')parser.add_argument('--txt_dir', required=True, help='TXT文件输出目录')parser.add_argument('--img_width', type=int, required=True, help='图像宽度')parser.add_argument('--img_height', type=int, required=True, help='图像高度')args = parser.parse_args()batch_convert(args.json_dir, args.txt_dir, args.img_width, args.img_height)

第七章 主函数调用 

 

if __name__ == "__main__":main()

全部代码如下: 

import os
import json
import argparse# 类别映射 - 根据实际情况修改
class_mapping = {"自己的类别": 0}def convert_polygon_to_bbox(points):"""将多边形点集转换为边界框"""x_coords = [point[0] for point in points]y_coords = [point[1] for point in points]xmin = min(x_coords)ymin = min(y_coords)xmax = max(x_coords)ymax = max(y_coords)return xmin, ymin, xmax, ymaxdef convert_json_to_yolo(json_path, txt_path, image_width, image_height):"""将单个JSON文件转换为YOLO格式的TXT文件"""with open(json_path, 'r') as f:data = json.load(f)with open(txt_path, 'w') as f:for shape in data.get("shapes", []):label = shape["label"]if label not in class_mapping:print(f"警告: 未定义的类别 '{label}',跳过此对象")continueclass_id = class_mapping[label]points = shape["points"]xmin, ymin, xmax, ymax = convert_polygon_to_bbox(points)# 计算归一化的边界框坐标x_center = (xmin + xmax) / (2 * image_width)y_center = (ymin + ymax) / (2 * image_height)width = (xmax - xmin) / image_widthheight = (ymax - ymin) / image_height# 写入YOLO格式的行f.write(f"{class_id} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f}\n")def batch_convert(json_dir, txt_dir, image_width, image_height):"""批量转换JSON文件夹中的所有文件"""# 确保输出目录存在os.makedirs(txt_dir, exist_ok=True)# 获取所有JSON文件json_files = [f for f in os.listdir(json_dir) if f.lower().endswith('.json')]if not json_files:print(f"错误: 在 {json_dir} 中未找到JSON文件")returntotal = len(json_files)success = 0print(f"开始批量转换: {total} 个JSON文件")for i, json_file in enumerate(json_files, 1):json_path = os.path.join(json_dir, json_file)txt_file = os.path.splitext(json_file)[0] + '.txt'txt_path = os.path.join(txt_dir, txt_file)try:convert_json_to_yolo(json_path, txt_path, image_width, image_height)success += 1print(f"[{i}/{total}] 成功转换: {json_file} -> {txt_file}")except Exception as e:print(f"[{i}/{total}] 转换失败: {json_file} - 错误: {str(e)}")print(f"转换完成: 成功 {success}/{total}")def main():parser = argparse.ArgumentParser(description='批量将JSON标注文件转换为YOLO格式的TXT文件')parser.add_argument('--json_dir', required=True, help='JSON文件所在目录')parser.add_argument('--txt_dir', required=True, help='TXT文件输出目录')parser.add_argument('--img_width', type=int, required=True, help='图像宽度')parser.add_argument('--img_height', type=int, required=True, help='图像高度')args = parser.parse_args()batch_convert(args.json_dir, args.txt_dir, args.img_width, args.img_height)if __name__ == "__main__":main()# ""是文件路径
# 执行命令 python xx.py --json_dir "" --txt_dir "" --img_width 800 --img_height 600
# 传入四个参数
# --json_dir:json的文件夹路径
# --txt_dir:输出的txt的文件夹路径
# --img_width 图像宽
# --img_height 图像高

相关文章:

  • VMware ESXi网络配置
  • Learning Discriminative Data Fitting Functions for Blind Image Deblurring论文阅读
  • dto vo类为什么要序列化?
  • 跨架构镜像打包问题及解决方案
  • Odoo 打印功能架构与工作流程深度剖析
  • ADB推送文件到指定路径解析
  • 一键提取Office内图片的工具
  • 华为OD机试真题——字母组合过滤组合字符串(2025A卷:100分)Java/python/JavaScript/C/C++/GO最佳实现
  • 大型工业控制系统中私有云计算模式的弊端剖析与反思
  • react-color-palette源码解析
  • leetcode hot100刷题日记——28.环形链表2
  • idea本地git上传gitee码云失败分析,push rejected+git手动融合
  • 设计模式-发布订阅
  • 工厂模式 vs 策略模式:设计模式中的 “创建者” 与 “决策者”
  • spark shuffle的分区支持动态调整,而hive不支持
  • Kubernetes面试题(基础向)
  • unity星空运动
  • Linux | Shell脚本的基础知识
  • ai如何绘制mg人物的睫毛
  • 求满足target的最小窗口的长度,滑动窗口法,双指针
  • 有没有做美食的网站/搜索引擎优化自然排名
  • 上海亿网站建设/app推广怎么联系一手代理
  • 南京建网站/seo站内优化技巧
  • 南宁网站建设推广优化/厦门百度开户
  • 越秀区做网站/淘宝seo 优化软件
  • 网站建设的技术难点/龙岗seo优化