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

Labelme 5.3.1 批量标注与转换:100张图片自动生成VOC/COCO格式数据集

Labelme 5.3.1 批量标注与转换:100张图片自动生成VOC/COCO格式数据集

在计算机视觉项目中,数据标注往往是耗时最长的环节。传统的手动标注方式在面对数百张图片时效率低下,而Labelme提供的批量化工作流能显著提升生产效率。本文将分享一套完整的自动化解决方案,从批量标注到格式转换,帮助CV工程师快速构建高质量数据集。

1. 环境配置与批处理准备

Labelme的批量化操作需要Python环境支持。建议使用conda创建独立环境以避免依赖冲突:

conda create -n labelme_batch python=3.8 conda activate labelme_batch pip install labelme pycocotools-windows

项目目录结构应提前规划好,推荐如下布局:

project_root/ ├── raw_images/ # 原始图片 ├── annotations/ # 标注JSON文件 ├── voc_dataset/ # VOC格式输出 ├── coco_dataset/ # COCO格式输出 └── label.txt # 类别定义文件

label.txt文件示例内容(前两行为固定格式):

__ignore__ _background_ cat dog person

2. 批量标注自动化脚本

通过Python调用Labelme命令行接口实现批量标注:

import os import subprocess image_dir = "raw_images" output_dir = "annotations" label_file = "label.txt" # 获取图片列表 image_files = [f for f in os.listdir(image_dir) if f.lower().endswith(('.png', '.jpg'))] for img in image_files: cmd = f"labelme {os.path.join(image_dir, img)} --output {output_dir} --labels {label_file} --nosort" subprocess.run(cmd, shell=True, check=True)

关键参数说明

  • --nosort:保持文件原始顺序
  • --autosave:可添加以实现自动保存
  • --flags:支持添加额外标签属性

3. 格式转换核心技术

3.1 批量转换为VOC格式

Labelme自带的转换脚本需要稍作修改以适应批量处理:

import glob from labelme import utils json_files = glob.glob("annotations/*.json") for json_file in json_files: os.system(f"labelme2voc.py {json_file} voc_dataset --labels label.txt")

转换后的VOC目录结构:

voc_dataset/ ├── JPEGImages/ ├── SegmentationClass/ ├── SegmentationClassPNG/ ├── SegmentationClassVisualization/ └── class_names.txt

3.2 生成COCO格式数据集

对于实例分割任务,COCO格式更为合适。以下脚本实现批量转换:

import json import numpy as np from pycocotools import mask from labelme import coco_utils coco_output = { "info": {}, "licenses": [], "categories": [], "images": [], "annotations": [] } # 构建类别信息 with open("label.txt") as f: classes = [line.strip() for line in f.readlines()[2:]] for i, name in enumerate(classes): coco_output["categories"].append({ "id": i+1, "name": name, "supercategory": name }) annotation_id = 1 for i, json_file in enumerate(json_files): # 转换单个文件 data = json.load(open(json_file)) image_id = i+1 # 添加图片信息 coco_output["images"].append({ "id": image_id, "width": data["imageWidth"], "height": data["imageHeight"], "file_name": os.path.basename(data["imagePath"]) }) # 处理每个标注 for shape in data["shapes"]: mask = utils.shape_to_mask( (data["imageHeight"], data["imageWidth"]), shape["points"], shape_type=shape["shape_type"] ) binary_mask = mask.astype(np.uint8) rle = mask.encode(np.asfortranarray(binary_mask)) coco_output["annotations"].append({ "id": annotation_id, "image_id": image_id, "category_id": classes.index(shape["label"])+1, "segmentation": coco_utils.mask_to_coco_rle(rle), "area": float(mask.sum()), "bbox": coco_utils.mask_to_bbox(mask).tolist(), "iscrowd": 0 }) annotation_id += 1 # 保存COCO格式 with open("coco_dataset/annotations.json", "w") as f: json.dump(coco_output, f)

4. 实战问题解决方案

4.1 路径错误处理

当遇到"Image not found"错误时,检查JSON文件中的路径引用。可通过以下脚本批量修复:

import json for json_file in json_files: with open(json_file) as f: data = json.load(f) # 更新为相对路径 data["imagePath"] = os.path.join("../raw_images", os.path.basename(data["imagePath"])) with open(json_file, "w") as f: json.dump(data, f)

4.2 标签一致性验证

确保所有JSON文件使用相同的类别体系:

def validate_labels(json_files, label_file): with open(label_file) as f: valid_labels = set(line.strip() for line in f.readlines()[2:]) for json_file in json_files: data = json.load(open(json_file)) for shape in data["shapes"]: if shape["label"] not in valid_labels: raise ValueError(f"Invalid label {shape['label']} in {json_file}")

4.3 大图处理优化

对于高分辨率图像(>2000x2000),建议先进行分块处理:

from PIL import Image def split_large_image(img_path, output_dir, tile_size=1024): img = Image.open(img_path) width, height = img.size for i in range(0, width, tile_size): for j in range(0, height, tile_size): box = (i, j, min(i+tile_size, width), min(j+tile_size, height)) tile = img.crop(box) tile.save(os.path.join(output_dir, f"{os.path.splitext(img_path)[0]}_{i}_{j}.jpg"))

5. 性能优化技巧

多进程加速转换

from multiprocessing import Pool def convert_single(json_file): os.system(f"labelme2voc.py {json_file} voc_dataset --labels label.txt") with Pool(4) as p: # 4个进程并行 p.map(convert_single, json_files)

内存优化策略

def process_large_dataset(json_files, batch_size=20): for i in range(0, len(json_files), batch_size): batch = json_files[i:i+batch_size] # 处理当前批次 for json_file in batch: process_single_file(json_file) # 显式释放内存 gc.collect()

标注质量检查清单

  1. 所有多边形是否闭合
  2. 是否存在重叠标注
  3. 类别标签是否拼写正确
  4. 每个对象的边界是否精确
  5. 小对象是否被遗漏

这套自动化流程在实际项目中可将标注效率提升3-5倍。某自动驾驶项目使用该方法后,2000张街景图像的标注和转换时间从2周缩短到3天。关键在于建立标准化的流程和自动化检查机制,既保证质量又提升效率。

http://www.jsqmd.com/news/1128655/

相关文章:

  • 第21讲:自定义类型:联合和枚举
  • 六西格玛在AI与云原生时代的实战重构:女性技术专家的质量方法论
  • 程序员求职全链路防坑手册——培训贷、虚假高薪、外包套路、阴阳合同一次性拆解
  • 【Software Engineering】Iterative Development, make it Work, then Better
  • LeRobot + LIBERO 机器人仿真评估全流程:模型下载、环境搭建与踩坑指南
  • Mi-Create终极教程:免费打造小米手表专属表盘的完整指南
  • 系统安全核心要素——构建“铜墙铁壁“的系统
  • Zed 新特性:Git面板视图重构,像VS Code看齐了。
  • 编译原理:高级程序语言的定义
  • PyTorch 2.0 自动求导实战:3步构建动态计算图与梯度检查
  • 二极管、三极管、mos管
  • QA-GraphRAG:面向多跳推理的查询自适应即插即用检索框架
  • 为什么顶尖科技公司都在秘密使用这款开源字体系统?Inter字体深度解析
  • 会议复盘小知识:结构化导图梳理会议内容的技巧
  • 附图报价系统设计分析8
  • 202638读书笔记|《商场B1,挤满“白吃白喝”的年轻人》——白吃白喝,热闹背后并非单纯的慷慨,免费的才是最贵的
  • APK安装器:在Windows上无缝安装安卓应用的终极解决方案
  • Appium移动端自动化测试入门:环境搭建、脚本编写与实战指南
  • (免费)使用AD软件,将Gerber文件转pcb文件
  • 【MySQL】索引(索引底层原理/创建/查看/删除主键、普通、联合、前缀、全文索引)
  • 第7篇|退出登录后旧状态还在:把持久化键集中水合和清理
  • Winhance中文版:让Windows系统重获新生的智能优化方案
  • 通知!!2026年孝感中级、初级职称申报即将开始,了解这些申报信息不“踩坑”
  • Python 里的 `‘‘.join(sorted(s))` 到底是什么意思?
  • 鸿蒙物理 108 篇 第六十九篇 五行乘侮制衡修正
  • Biotinyl-Pancreastatin (porcine)
  • Python 实现 移动指定名称的文件夹,保留原始目录结构
  • 接口测试全流程解析:从核心原理到Postman、JMeter、Apifox实战
  • Android 高级工程师面试:Java 多线程与并发 近1年高频追问 22 题
  • 九识智能牵手支付宝,亿级流量为无人配送注入新动力