Labelme json文件转换为yolo标签格式文件

该脚本将LabelMe输出的JSON格式转换为YOLO系列所需的文本格式。

帮助将 LabelMe Annotation Tool JSON 格式转换为 YOLO 文本文件格式。如果您已经通过 LabelMe 标记了分割数据集,则可以轻松使用此工具来帮助转换为 YOLO 格式数据集。

最新
将数据导出为 yolo 多边形注释(用于 YOLOv5 v7.0 分割)
现在您可以选择标签文本的输出格式。两个可用的替代方案是polygon边界框 ( bbox)。

安装
pip install labelme2yolo

参数说明
--json_dir LabelMe JSON 文件文件夹路径。
--val_size (可选)验证数据集大小,例如 0.2 表示 20% 用于验证。
--test_size (可选)测试数据集大小,例如 0.2 表示测试的 20%。
--json_name (可选)转换单个 LabelMe JSON 文件。
--output_format (可选)标签的输出格式。
--label_list (可选)预先分配的类别标签。

如何使用
1. 转换JSON文件,通过--val_size和--test_size分割训练、验证和测试数据集
将所有 LabelMe JSON 文件放在labelme_json_dir下,然后运行此 python 命令。

labelme2yolo --json_dir /path/to/labelme_json_dir/ --val_size 0.15 --test_size 0.15
   
脚本会在不同的文件夹下生成YOLO格式的数据集标签和图像,例如,

/path/to/labelme_json_dir/YOLODataset/labels/train/
/path/to/labelme_json_dir/YOLODataset/labels/test/
/path/to/labelme_json_dir/YOLODataset/labels/val/
/path/to/labelme_json_dir/YOLODataset/images/train/
/path/to/labelme_json_dir/YOLODataset/images/test/
/path/to/labelme_json_dir/YOLODataset/images/val/

/path/to/labelme_json_dir/YOLODataset/dataset.yaml

2. 转换JSON文件,按文件夹拆分训练和验证数据集
如果您已经为LabelMe自行拆分了训练数据集和验证数据集,请将这些文件夹放在labelme_json_dir下,例如:

/path/to/labelme_json_dir/train/
/path/to/labelme_json_dir/val/

将所有 LabelMe JSON 文件放在labelme_json_dir下。脚本将按文件夹读取训练和验证数据集。运行这个 python 命令。

labelme2yolo --json_dir /path/to/labelme_json_dir/

脚本会在不同的文件夹下生成YOLO格式的数据集标签和图像,例如,

/path/to/labelme_json_dir/YOLODataset/labels/train/
/path/to/labelme_json_dir/YOLODataset/labels/val/
/path/to/labelme_json_dir/YOLODataset/images/train/
/path/to/labelme_json_dir/YOLODataset/images/val/

/path/to/labelme_json_dir/YOLODataset/dataset.yaml

3. 转换单个JSON文件
将 LabelMe JSON 文件放在labelme_json_dir下,然后运行这个 python 命令。

labelme2yolo --json_dir /path/to/labelme_json_dir/ --json_name 2.json
 
脚本会在labelme_json_dir下生成 YOLO 格式的文本标签和图像,例如,

/path/to/labelme_json_dir/2.text
/path/to/labelme_json_dir/2.png

来源:https://pypi.org/project/labelme2yolo/
 

要将LabelMe标注的JSON文件转换YOLO对象检测模型所需的训练数据格式,可以通过编写脚本来完成这一任务。这种方法能够自动解析LabelMe JSON文件中的标签信息,并将其换成YOLO所使用的文本文件格式。 ### 换过程 #### 1. 解析LabelMe JSON文件 LabelMe JSON文件包含了图像的标注信息,如边界框的位置、标签名称等。这些信息需要被提取出来以便进一步处理。 #### 2. 将数据换为YOLO格式 YOLO格式要求每个标注项保存在一个单独的文本文件中,每行代表一个物体,格式如下: ``` <object-class> <x_center> <y_center> <width> <height> ``` 其中`<object-class>`是一个整数,对应于类别的索引;`<x_center>`、`<y_center>`、`<width>`和`<height>`都是相对于图像宽度和高度的比例值。 #### 3. 编写换脚本 下面提供了一个简单的Python脚本示例来执行这个换过程: ```python import os import json def convert_labelme_to_yolo(json_file, output_folder): with open(json_file) as f: data = json.load(f) image_width = data['imageWidth'] image_height = data['imageHeight'] for shape in data['shapes']: if shape['shape_type'] == 'rectangle': label = shape['label'] points = shape['points'] # 计算中心坐标和宽高 x_min = min(points[0][0], points[1][0]) x_max = max(points[0][0], points[1][0]) y_min = min(points[0][1], points[1][1]) y_max = max(points[0][1], points[1][1]) x_center = (x_min + x_max) / 2.0 / image_width y_center = (y_min + y_max) / 2.0 / image_height width = abs(x_max - x_min) / float(image_width) height = abs(y_max - y_min) / float(image_height) # 获取类别ID class_id = get_class_id(label) # 创建YOLO格式的文本文件 txt_filename = os.path.splitext(os.path.basename(json_file))[0] + ".txt" txt_filepath = os.path.join(output_folder, txt_filename) with open(txt_filepath, "a") as txt_file: txt_file.write(f"{class_id} {x_center} {y_center} {width} {height}\n") def get_class_id(label): # 这里可以根据实际情况定义类别到ID的映射关系 return {"cat": 0, "dog": 1}.get(label, -1) # 使用方法 json_files_directory = "/path/to/json/files/" output_directory = "/path/to/output/yolo/txt/" if not os.path.exists(output_directory): os.makedirs(output_directory) for filename in os.listdir(json_files_directory): if filename.endswith(".json"): json_path = os.path.join(json_files_directory, filename) convert_labelme_to_yolo(json_path, output_directory) ``` 这段脚本会遍历指定目录下的所有JSON文件,并将它们换为YOLO格式的文本文件。需要注意的是,你需要根据实际的类别设置`get_class_id()`函数中的映射关系。 ### 注意事项 - 确保输入的JSON文件路径和输出目录路径正确无误。 - 对于不同的类别,可能需要调整`get_class_id()`函数中的逻辑。 - 在实际部署前,建议先对少量样本进行测试验证。 通过以上步骤,你可以有效地将LabelMe标注的JSON文件转换为适合YOLO模型训练的格式
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值