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

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 python3.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 person2. 批量标注自动化脚本通过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 flabelme {os.path.join(image_dir, img)} --output {output_dir} --labels {label_file} --nosort subprocess.run(cmd, shellTrue, checkTrue)关键参数说明--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(flabelme2voc.py {json_file} voc_dataset --labels label.txt)转换后的VOC目录结构voc_dataset/ ├── JPEGImages/ ├── SegmentationClass/ ├── SegmentationClassPNG/ ├── SegmentationClassVisualization/ └── class_names.txt3.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: i1, name: name, supercategory: name }) annotation_id 1 for i, json_file in enumerate(json_files): # 转换单个文件 data json.load(open(json_file)) image_id i1 # 添加图片信息 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_typeshape[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(fInvalid label {shape[label]} in {json_file})4.3 大图处理优化对于高分辨率图像2000x2000建议先进行分块处理from PIL import Image def split_large_image(img_path, output_dir, tile_size1024): 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(itile_size, width), min(jtile_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(flabelme2voc.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_size20): for i in range(0, len(json_files), batch_size): batch json_files[i:ibatch_size] # 处理当前批次 for json_file in batch: process_single_file(json_file) # 显式释放内存 gc.collect()标注质量检查清单所有多边形是否闭合是否存在重叠标注类别标签是否拼写正确每个对象的边界是否精确小对象是否被遗漏这套自动化流程在实际项目中可将标注效率提升3-5倍。某自动驾驶项目使用该方法后2000张街景图像的标注和转换时间从2周缩短到3天。关键在于建立标准化的流程和自动化检查机制既保证质量又提升效率。