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

Labelme生成的JSON文件别乱扔!手把手教你用Python脚本批量转成YOLO格式

Labelme标注数据工程化:Python脚本实现YOLO格式批量转换实战

在计算机视觉项目的实际开发流程中,数据标注往往只完成了整个工作流的20%,而剩下的80%精力都消耗在数据清洗、格式转换和验证环节。当你用Labelme精心标注了数百张图像后,那些生成的JSON文件就像未经雕琢的玉石——价值连城但需要专业加工才能发挥真正作用。

1. 理解Labelme与YOLO的数据格式差异

Labelme生成的JSON文件采用绝对坐标记录多边形顶点,而YOLO需要的却是归一化后的中心点坐标和宽高比例。这种本质差异导致直接使用原始标注会面临三个核心挑战:

  1. 坐标系转换:从图像像素坐标系到YOLO的归一化坐标系(0-1范围)
  2. 形状描述转换:从多边形顶点序列到边界框的数学表达
  3. 标签映射:从文本标签到YOLO要求的类别索引

典型的Labelme JSON结构关键字段如下:

{ "version": "5.1.1", "flags": {}, "shapes": [ { "label": "cat", "points": [[302,240],[402,240],[402,340],[302,340]], "shape_type": "polygon" } ], "imagePath": "example.jpg", "imageWidth": 800, "imageHeight": 600 }

对应的YOLO格式要求每张图片一个txt文件,每行表示一个对象:

<class_id> <x_center> <y_center> <width> <height>

2. 构建Python转换脚本的核心逻辑

2.1 基础转换函数实现

创建一个labelme2yolo.py文件,首先实现核心几何计算函数:

import json import os import numpy as np def polygon_to_yolo(polygon_points, img_width, img_height): """将多边形顶点转换为YOLO格式的边界框""" points = np.array(polygon_points) x_min, y_min = np.min(points, axis=0) x_max, y_max = np.max(points, axis=0) # 计算中心点和宽高(归一化) x_center = ((x_min + x_max) / 2) / img_width y_center = ((y_min + y_max) / 2) / img_height width = (x_max - x_min) / img_width height = (y_max - y_min) / img_height return x_center, y_center, width, height

2.2 批量处理与文件输出

添加目录遍历和文件输出逻辑:

def process_labelme_json(json_path, class_mapping, output_dir): with open(json_path, 'r') as f: data = json.load(f) txt_lines = [] for shape in data['shapes']: if shape['shape_type'] != 'polygon': continue class_name = shape['label'].lower() if class_name not in class_mapping: continue # 转换坐标 x_center, y_center, width, height = polygon_to_yolo( shape['points'], data['imageWidth'], data['imageHeight'] ) txt_lines.append(f"{class_mapping[class_name]} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f}") # 写入YOLO格式文件 base_name = os.path.splitext(os.path.basename(json_path))[0] txt_path = os.path.join(output_dir, f"{base_name}.txt") with open(txt_path, 'w') as f: f.write('\n'.join(txt_lines))

3. 工程化实践中的关键问题处理

3.1 复杂多边形的优化策略

当遇到复杂多边形时,直接取最小外接矩形可能导致标注质量下降。我们可以在转换前对多边形进行凸包处理:

from scipy.spatial import ConvexHull def optimize_polygon(points): """对复杂多边形进行凸包优化""" hull = ConvexHull(points) return [points[i] for i in hull.vertices]

3.2 多线程批量处理

对于大型数据集,添加多线程支持可以显著提升处理速度:

from concurrent.futures import ThreadPoolExecutor def batch_convert(input_dir, output_dir, class_mapping, workers=4): os.makedirs(output_dir, exist_ok=True) json_files = [f for f in os.listdir(input_dir) if f.endswith('.json')] with ThreadPoolExecutor(max_workers=workers) as executor: for json_file in json_files: executor.submit( process_labelme_json, os.path.join(input_dir, json_file), class_mapping, output_dir )

4. 数据验证与质量检查

转换完成后必须验证结果准确性,这里提供一个可视化检查脚本:

import cv2 def visualize_yolo_annotation(image_path, txt_path, class_names): image = cv2.imread(image_path) height, width = image.shape[:2] with open(txt_path, 'r') as f: for line in f: class_id, xc, yc, w, h = map(float, line.strip().split()) # 转换回像素坐标 x = int((xc - w/2) * width) y = int((yc - h/2) * height) box_w = int(w * width) box_h = int(h * height) # 绘制边界框 cv2.rectangle(image, (x,y), (x+box_w,y+box_h), (0,255,0), 2) cv2.putText(image, class_names[int(class_id)], (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0,255,0), 2) cv2.imshow('Validation', image) cv2.waitKey(0) cv2.destroyAllWindows()

5. 完整项目结构与管理

建议采用以下目录结构组织转换项目:

labelme2yolo/ ├── src/ │ ├── converter.py # 主转换脚本 │ ├── validator.py # 验证脚本 │ └── utils.py # 工具函数 ├── configs/ │ └── classes.yaml # 类别映射配置 ├── input_data/ # 原始Labelme数据 │ ├── images/ # 原图目录 │ └── annotations/ # JSON标注目录 └── output_data/ # 转换输出 ├── images/ # 图片(可符号链接) └── labels/ # YOLO格式标签

示例classes.yaml配置文件:

class_mapping: cat: 0 dog: 1 person: 2

6. 高级技巧与性能优化

6.1 内存映射加速大文件处理

对于超大JSON文件(>100MB),可以使用ijson库进行流式处理:

import ijson def process_large_json(json_path): with open(json_path, 'rb') as f: objects = ijson.items(f, 'shapes.item') for shape in objects: # 处理每个shape对象 pass

6.2 增量处理与断点续传

添加检查点机制,避免重复处理:

def batch_convert_with_checkpoint(input_dir, output_dir, checkpoint_file): processed = set() if os.path.exists(checkpoint_file): with open(checkpoint_file, 'r') as f: processed.update(f.read().splitlines()) with open(checkpoint_file, 'a') as checkpoint: for json_file in os.listdir(input_dir): if json_file in processed: continue # 处理文件... checkpoint.write(f"{json_file}\n")

6.3 并行GPU加速计算

对于超大规模数据集,可以使用CUDA加速几何计算:

import cupy as cp def gpu_polygon_to_yolo(polygon_points, img_width, img_height): points = cp.array(polygon_points) x_min, y_min = cp.min(points, axis=0) x_max, y_max = cp.max(points, axis=0) x_center = ((x_min + x_max) / 2) / img_width y_center = ((y_min + y_max) / 2) / img_height width = (x_max - x_min) / img_width height = (y_max - y_min) / img_height return x_center.get(), y_center.get(), width.get(), height.get()
http://www.jsqmd.com/news/909964/

相关文章:

  • 纳米砂磨机 vs 球磨机 vs 搅拌球磨机——三种湿法研磨设备的深度对比与场景选择 - 上海奎特机电
  • 海思Hi3559AV100 VGS画线实战:从API调用到矩形框绘制的完整代码解析
  • 2026荆门市本地人必选的公共卫生检测专业机构TOP5推荐!美容院、足疗店、酒店宾馆卫生检测、许可证办理,正规CMA资质检测公司排名推荐 (2026年5月商铺卫生办证最新深度调研方案) - 一休咨询
  • 2026晋州市本地人必选的公共卫生检测专业机构TOP5推荐!美容院、足疗店、酒店宾馆卫生检测、许可证办理,正规CMA资质检测公司排名推荐 (2026年5月商铺卫生办证最新深度调研方案) - 一休咨询
  • 从‘龟速’到‘起飞’:手把手教你用艾特肯(Δ²)方法加速你的MATLAB迭代程序
  • 知识图谱如何解决AI编程助手上下文丢失问题
  • 2026云端多人协同的国产 PCB 设计软件推荐,办公协作更高效 - 品牌2025
  • 你的公司是否“为了自动化而自动化”?极客老王深度解析Agent落地破局之道
  • Keep实战指南:5步构建企业级智能告警管理平台
  • 别再复制粘贴了!手把手教你用Angular+SpringBoot打造个性化电子病历模板(附汉密尔顿量表实战)
  • Qt多线程避坑指南:moveToThread后对象生命周期与内存管理的5个关键点
  • 纳米砂磨机厂家怎么选——从技术硬实力到服务体系的全方位评估框架 - 上海奎特机电
  • 2026晋江市本地人必选的公共卫生检测专业机构TOP5推荐!美容院、足疗店、酒店宾馆卫生检测、许可证办理,正规CMA资质检测公司排名推荐 (2026年5月商铺卫生办证最新深度调研方案) - 一休咨询
  • 朱雀AI检测工具原理详解与企业级落地实战指南
  • 2026 国产 PCB 信号仿真设计软件推荐:自主可控 EDA 工具选型参考 - 品牌2025
  • 告别底噪!用Python+Librosa复现维纳滤波语音降噪(附完整代码与音频对比)
  • 在Ubuntu 20.04上从零搭建ucore Lab 2环境:手把手解决make报错与依赖问题
  • 从玩具到利器:低成本改造特斯拉线圈,实现厘米级电弧与高效能量转换
  • Taotoken的审计日志功能为企业API安全管理提供了便利
  • 如何快速备份微博内容:Speechless工具5分钟实现PDF导出的完整指南
  • 2026年4月防水卷材企业推荐,非固化防水涂料/SBS防水卷材/橡胶沥青防水涂料/防水卷材,防水卷材厂商口碑推荐 - 品牌推荐师
  • 从Kaggle竞赛到业务上线:手把手教你用Python和Scikit-learn在实战中权衡Precision与Recall
  • 2026全国一线二线三线城市CPPM报名十大核心问题全流程答疑 - 企业推荐官【官方】
  • 2026佳木斯市本地人必选的公共卫生检测专业机构TOP5推荐!美容院、足疗店、酒店宾馆卫生检测、许可证办理,正规CMA资质检测公司排名推荐 (2026年5月商铺卫生办证最新深度调研方案) - 一休咨询
  • AI灵性伴侣:技术如何重塑精神慰藉与伦理边界
  • AI应用的部署策略:从开发到生产的完整流程
  • 基于LM2576的3A大电流太阳能充电器DIY:从开关电源原理到户外能源站实践
  • DistroAV:让OBS视频制作像搭积木一样简单的NDI插件指南 [特殊字符]
  • 哈尔滨企业搬迁必看:3步筛选靠谱服务机构 - 幸福生活序曲
  • HackerBox MCU Lab 2025:一站式嵌入式开发平台实战与四大主流MCU深度解析