使用LabelMe处理JSON文件:从标注到数据管理的全面指南
LabelMe是一款基于Python的开源图像标注工具,因其灵活性和强大的功能被广泛应用于计算机视觉项目中(如目标检测、语义分割等),其标注结果以JSON格式存储,而处理这些JSON文件(如格式转换、数据清洗、统计分析等)是项目落地的关键环节,本文将详细介绍如何使用LabelMe处理JSON文件,涵盖从标注结果导出、JSON结构解析到常用操作(如转换、合并、可视化)的完整流程。
LabelMe JSON文件基础:结构与生成
1 JSON文件的结构
Labelme标注的每个图像会生成一个同名的JSON文件(如image.json),核心字段包括:
version:标注工具版本(如5.6)。flags:自定义标注标记(如{"ignore": true}),可选。shapes:标注对象的核心列表,每个元素包含:label:标注类别(如"cat"、"dog")。points:标注点的坐标列表(如矩形框的4个角点、多边形的顶点)。group_id:用于关联多个独立形状(如分割区域的多个部分),可选。shape_type:形状类型(如"polygon"、"rectangle"、"point")。flags:单个形状的标记,可选。
imagePath:标注图像的文件路径(相对路径)。imageData:图像的Base64编码数据(可选,若未存储图像路径则需包含)。imageHeight/imageWidth:图像的高度和宽度(像素)。
示例JSON片段:
{
"version": "4.5.6",
"flags": {},
"shapes": [
{
"label": "cat",
"points": [[100, 150], [300, 150], [300, 400], [100, 400]],
"group_id": null,
"shape_type": "rectangle",
"flags": {}
},
{
"label": "dog",
"points": [[400, 200], [450, 250], [420, 300]],
"group_id": null,
"shape_type": "polygon",
"flags": {}
}
],
"imagePath": "images/cat_dog.jpg",
"imageHeight": 500,
"imageWidth": 800
}
2 导出JSON文件
Labelme标注完成后,JSON文件会自动保存在与图像相同的目录下(默认文件名与图像相同),若需手动导出或调整格式:
- 点击工具栏的
File→Save保存当前标注(自动生成JSON)。 - 若需批量导出,使用
File→Save As选择目标路径,或通过命令行工具labelme_json_to_dataset批量转换(详见后文)。
LabelMe JSON文件的常见处理操作
1 JSON格式转换:适配不同框架需求
不同深度学习框架(如YOLO、COCO、Mask R-CNN)对标注格式要求不同,需将LabelMe的JSON转换为对应格式。
(1)转COCO格式(适用于目标检测/分割)
COCO格式要求标注以.json文件存储,包含images、annotations、categories三个核心字段。
工具推荐:Labelme自带的labelme2coco.py脚本。
操作步骤:
- 安装依赖:
pip install labelme pycocotools。 - 准备标注文件列表:创建一个txt文件(如
image_list.txt),每行为图像路径(如images/001.jpg)。 - 运行转换脚本:
labelme2coco --input_list image_list.txt --output_dir coco_output
- 输出结果:
coco_output目录下生成annotations.json,可直接用于COCO数据集训练。
(2)转YOLO格式(适用于YOLOv5/v7/v8等)
YOLO格式要求标注为.txt文件,每行包含class_id x_center y_center width height(归一化坐标)。
工具推荐:第三方脚本labelme2yolo.py(需提前生成类别映射文件)。
操作步骤:
- 创建类别文件
classes.txt,每行为一个类别(如cat、dog)。 - 运行转换脚本(示例):
python labelme2yolo.py --json_dir labelme_jsons --output_dir yolo_labels --classes classes.txt
- 输出结果:每个图像对应一个
.txt文件,与图像文件名一致,可直接用于YOLO训练。
(3)转VOC格式(适用于Pascal VOC数据集)
VOC格式要求标注为XML文件,包含size(图像尺寸)、object(标注框)等信息。
工具推荐:Labelme自带的labelme2voc.py脚本。
操作步骤:
- 运行转换脚本:
labelme2voc --json_dir labelme_jsons --output_dir voc_output --voc_classes cat,dog
- 输出结果:
voc_output目录下生成Annotations(XML标注)和JPEGImages(图像),符合VOC数据集结构。
2 JSON文件合并:整合多场景标注
当标注数据分散在多个JSON文件中时(如不同人员标注、不同场景),需合并为一个统一文件。
方法:使用Python脚本遍历JSON文件,整合shapes和images字段。
示例代码:
import json
import os
from glob import glob
def merge_json_files(json_dir, output_file):
merged_data = {
"version": "4.5.6",
"flags": {},
"shapes": [],
"images": []
}
json_files = glob(os.path.join(json_dir, "*.json"))
for json_file in json_files:
with open(json_file, "r") as f:
data = json.load(f)
merged_data["shapes"].extend(data["shapes"])
# 合并图像信息(需去重)
img_info = {
"file_name": data["imagePath"],
"height": data["imageHeight"],
"width": data["imageWidth"]
}
if img_info not in merged_data["images"]:
merged_data["images"].append(img_info)
# 更新图像路径为相对路径(可选)
merged_data["imagePath"] = "merged_images/"
with open(output_file, "w") as f:
json.dump(merged_data, f, indent=2)
# 使用示例
merge_json_files("labelme_jsons", "merged_annotations.json")
注意事项:合并前需检查图像路径是否一致,避免重复或路径错误。
3 JSON文件清洗:修正标注错误
标注过程中可能存在错误(如类别错标、坐标越界),需通过清洗修正。
常见清洗操作:
- 修正类别标签:统一大小写、合并相似类别(如
"Dog"和"dog"合并为"dog")。 - 过滤无效标注:删除坐标超出图像范围的标注(如
points中存在负值或超过imageWidth/imageHeight)。 - 删除空标注:移除
shapes为空的JSON文件(无标注的图像)。
示例代码(过滤无效标注):
import json
def clean_json(json_file, output_file):
with open(json_file, "r") as f:
data = json.load(f)
valid_shapes = []
for shape in data["shapes"]:
points = shape["points"]
# 检查坐标是否在图像范围内
valid_points = [
[max(0, min(x, data["imageWidth"])), max(0, min(y, data["imageHeight"]))]
for x, y in points
]
if len(valid_points) >= 3: # 至少3个点(多边形/矩形)
shape["points"] = valid_points
valid_shapes.append(shape)
data["shapes"] = valid_shapes
with open(output_file, "w") as f:
json.dump(data, f, indent=2)
# 使用示例
clean_json("noisy_annotations.json", "cleaned_annotations.json")
4 JSON文件可视化:验证标注质量
合并或清洗后,需可视化JSON标注以验证准确性。
方法1:Labelme自带可视化
- 打开Labelme,点击
File→Open加载JSON文件,标注会直接显示在图像上。 - 支持切换标注类型(矩形、多边形等)、编辑和重新保存。
**方法



还没有评论,来说两句吧...