千千梦丶琪 发表于 2024-11-4 08:45:18

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

目次

问题形貌
labelme格式
YOLO格式
示例
注意事项
代码转换
目次格式



问题形貌

迩来在利用ultralytics举行目标检测训练的时候,预备数据集时候,碰到了一个坑。labelme标注的json格式不能直接用来训练。

https://img-blog.csdnimg.cn/img_convert/4d12bd7e1055320f72a0e20fc26f9dc8.webp?x-oss-process=image/format,png
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格式)如下:

https://img-blog.csdnimg.cn/img_convert/70f256254a9c56bbd3a8c46a93375b5f.webp?x-oss-process=image/format,png
YOLO(You Only Look Once)是一种实时物体检测系统,YOLO格式指的是YOLO模子利用的标签文件格式,用于训练和推理时标注图像中的对象。YOLO格式的标签文件通常是一个纯文本文件,每个图像对应一个单独的文件,文件内容形貌了图像中每个对象的类别和位置信息。
YOLO格式


[*]文件名:每个图像对应一个同名的.txt文件。例如,假如图像名为image1.jpg,则对应的标签文件名应为image1.txt。
[*]文件内容:每行代表一个对象,格式如下: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 os

def 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 0
      class_id = 0 if shape['label'] == 'fire' else -1# 需要时调整以支持其他标签

      # 如果类别ID为-1,表示该标签不处理,跳过
      if class_id == -1:
            continue

      points = shape['points']
      x_min = min(points, points)
      y_min = min(points, points)
      x_max = max(points, points)
      y_max = max(points, points)

      # 计算YOLO格式所需的中心坐标和宽度高度
      x_center = (x_min + x_max) / 2.0 / image_width
      y_center = (y_min + y_max) / 2.0 / image_height
      width = (x_max - x_min) / image_width
      height = (y_max - y_min) / image_height

      # 将转换后的数据添加到列表中
      yolo_data.append(f"{class_id} {x_center} {y_center} {width} {height}")

    return yolo_data

def 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'])
            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)

目次格式


https://img-blog.csdnimg.cn/img_convert/6e937033207464599bbab6c92a2cb193.webp?x-oss-process=image/format,png

https://img-blog.csdnimg.cn/img_convert/60363c1f113464d662d48328e3491ceb.webp?x-oss-process=image/format,png

https://img-blog.csdnimg.cn/img_convert/3782b4eb0388e59dc0c2f0f1562978c2.webp?x-oss-process=image/format,png
OK,大功告成!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: labelme标注的json转Yolo格式【ultralytics工具】