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

labelme标注的json转Yolo格式【ultralytics工具】

目录

问题描述

labelme格式

YOLO格式

示例

注意事项

代码转换

目录格式


问题描述

最近在使用ultralytics进行目标检测训练的时候,准备数据集时候,遇到了一个坑。labelme标注的json格式不能直接用来训练。

labelme格式

原始labelme标注的json格式如下:

{"version": "5.4.1","flags": {},"shapes": [{"label": "fire","points": [[943.5862068965517,1849.9310344827586],[1305.6551724137933,2160.2758620689656]],"group_id": null,"description": "","shape_type": "rectangle","flags": {},"mask": null},{"label": "fire","points": [[1764.2758620689656,1394.7586206896551],[2109.103448275862,1684.4137931034481]],"group_id": null,"description": "","shape_type": "rectangle","flags": {},"mask": null},{"label": "fire","points": [[2964.2758620689656,722.3448275862067],[3288.4137931034484,984.4137931034481]],"group_id": null,"description": "","shape_type": "rectangle","flags": {},"mask": null}],"imagePath": "No002.jpg","imageData": "/9j/4AAQSkZJR9k=","imageHeight": 3024,"imageWidth": 4032
}

要求的格式(YOLO格式)如下:

YOLO(You Only Look Once)是一种实时物体检测系统,YOLO格式指的是YOLO模型使用的标签文件格式,用于训练和推理时标注图像中的对象。YOLO格式的标签文件通常是一个纯文本文件,每个图像对应一个单独的文件,文件内容描述了图像中每个对象的类别和位置信息。

YOLO格式

  1. 文件名:每个图像对应一个同名的.txt文件。例如,如果图像名为image1.jpg,则对应的标签文件名应为image1.txt。
  2. 文件内容:每行代表一个对象,格式如下:class x_center y_center width height
    其中:
  • class:对象的类别编号,必须是零起始的整数(即从0开始)。
  • x_center:对象边界框中心点的x坐标,相对于图像宽度进行归一化(从0到1)。
  • y_center:对象边界框中心点的y坐标,相对于图像高度进行归一化(从0到1)。
  • width:对象边界框的宽度,相对于图像宽度进行归一化(从0到1)。
  • height:对象边界框的高度,相对于图像高度进行归一化(从0到1)。

示例

假设有一个图像image1.jpg,尺寸为800x600像素,其中包含两个对象。第一个对象是类别0(例如,人),位于图像的左上角,边界框中心点的坐标为(100, 200),宽度为50像素,高度为100像素。第二个对象是类别1(例如,车),位于图像的右下角,边界框中心点的坐标为(700, 500),宽度为150像素,高度为200像素。

对应的image1.txt文件内容如下:

0 0.125 0.333 0.0625 0.1667
1 0.875 0.833 0.1875 0.3333

解释:

  • 对象1(类别0):x_center = 100/800 = 0.125,y_center = 200/600 = 0.333,width = 50/800 = 0.0625,height = 100/600 = 0.1667
  • 对象2(类别1):x_center = 700/800 = 0.875,y_center = 500/600 = 0.833,width = 150/800 = 0.1875,height = 200/600 = 0.3333

注意事项

  • 如果图像中没有对象,则不需要生成对应的.txt文件。
  • 所有坐标和尺寸必须归一化,值范围为0到1。
  • 类别编号从0开始,依次递增。

代码转换

import json
import osdef convert_to_yolo_format(json_data):# 从JSON数据中读取图像宽度和高度image_width = json_data['imageWidth']image_height = json_data['imageHeight']shapes = json_data['shapes']yolo_data = []# 遍历所有标注形状for shape in shapes:# 假设标签 "fire" 对应类别ID 0class_id = 0 if shape['label'] == 'fire' else -1  # 需要时调整以支持其他标签# 如果类别ID为-1,表示该标签不处理,跳过if class_id == -1:continuepoints = shape['points']x_min = min(points[0][0], points[1][0])y_min = min(points[0][1], points[1][1])x_max = max(points[0][0], points[1][0])y_max = max(points[0][1], points[1][1])# 计算YOLO格式所需的中心坐标和宽度高度x_center = (x_min + x_max) / 2.0 / image_widthy_center = (y_min + y_max) / 2.0 / image_heightwidth = (x_max - x_min) / image_widthheight = (y_max - y_min) / image_height# 将转换后的数据添加到列表中yolo_data.append(f"{class_id} {x_center} {y_center} {width} {height}")return yolo_datadef save_yolo_format(yolo_data, output_path):# 将YOLO格式的数据保存到指定文件中with open(output_path, 'w') as file:for line in yolo_data:file.write(f"{line}\n")def process_json_files(input_directory):# 遍历输入目录中的所有文件for filename in os.listdir(input_directory):if filename.endswith(".json"):json_file_path = os.path.join(input_directory, filename)with open(json_file_path, 'r') as file:data = json.load(file)yolo_format_data = convert_to_yolo_format(data)# 生成输出文件路径image_basename = os.path.splitext(data['imagePath'])[0]output_file_path = os.path.join(input_directory, image_basename + '.txt')save_yolo_format(yolo_format_data, output_file_path)print(f"YOLO格式数据已保存到 {output_file_path}")# 示例使用
input_directory = 'path/to/your/json_files_directory'  # 修改为实际的JSON文件目录路径
process_json_files(input_directory)

目录格式

OK,大功告成!


http://www.mrgr.cn/news/52099.html

相关文章:

  • 在掌控板上搭建http服务器
  • 你的收入达到了缴纳个人所得税的标准了吗?
  • 怎么把音乐中的人声去掉?轻松搞定,音乐伴奏去人声攻略
  • 智能驾驶必备:MEB低速紧急制动功能如何保护你的车辆?
  • Fuzz工具对比及使用体验
  • DES加密初探-python实现
  • 未来哪些大学专业有可能会红灯?ChatGPT将会替代哪些岗位?还不会订阅chatgpt?可以看文末
  • 我也要!赚钱是分层的:这就是你又累又穷的原因——早读(逆天打工人爬取热门微信文章解读)
  • 《深度学习与图像处理(PaddlePaddle版)》写完这本书我解脱了
  • pyaudio出现Invalid number of channels的解决方法
  • 软件测试:快速了解其分类、方法、黑盒测试、白盒测试与灰盒测试
  • 【19楼-注册安全分析报告】
  • SpringBoot接收LocalDateTime参数
  • 白平衡之基于边缘检测的白平衡算法
  • ThreadLocal——简单使用
  • 箭头函数语法及书写规则。
  • AI产品经理:你更适合哪一种?
  • 泛微OA移动引擎中调用手机扫码后跳转另外页面(资产盘点)
  • 是时候扔掉你的密码了
  • 杭州威雅学校:中国橄榄球国家队到访助力杭州威雅校队新赛季之旅