告别手动标注!用Labelme+Bash脚本批量处理交通信号灯数据集(附一键转换脚本)
交通信号灯数据集自动化标注实战:从Labelme到YOLOv5的高效流水线
标注数据是计算机视觉项目中最耗时的环节之一。当面对数千张交通信号灯图片时,传统的手动标注方式不仅效率低下,还容易因疲劳导致标注质量下降。本文将分享一套完整的自动化标注流水线方案,结合Labelme的灵活标注能力和Bash脚本的批处理优势,实现从原始图片到YOLOv5训练数据的无缝转换。
1. 环境配置与工具选型
在开始自动化标注之前,需要搭建稳定且高效的工作环境。推荐使用Ubuntu 20.04作为基础系统,因其对深度学习工具链的支持最为完善。
核心工具栈配置:
# 创建专用虚拟环境 conda create -n auto_label python=3.8 -y conda activate auto_label # 安装标注工具核心依赖 conda install -c conda-forge pyqt=5.15.7 pip install labelme==5.1.1 opencv-python tqdm提示:建议固定工具版本以避免兼容性问题,特别是PyQt和Labelme的版本组合
对于硬件配置,虽然标注过程本身不要求高端GPU,但建议:
- 至少16GB内存以流畅处理高分辨率图片
- SSD存储加速文件读写
- 多核CPU提升批处理速度
2. 智能标注工作流设计
传统标注方式往往陷入"打开图片-绘制多边形-保存json"的重复循环。我们通过引入预处理和自动化技术,将整个流程优化为三个阶段:
2.1 预处理自动化
在正式标注前,对原始图片进行标准化处理:
#!/bin/bash # normalize_images.sh INPUT_DIR="./raw_images" OUTPUT_DIR="./normalized_images" mkdir -p $OUTPUT_DIR for img in $INPUT_DIR/*.{jpg,png}; do filename=$(basename "$img") # 统一转换为jpg格式,调整大小为1920x1080 convert "$img" -resize 1920x1080 -quality 90 "$OUTPUT_DIR/${filename%.*}.jpg" done这个预处理脚本实现了:
- 格式统一化(全部转为JPG)
- 分辨率标准化
- 质量压缩(保留90%质量)
2.2 半自动标注增强
利用Labelme的Python API实现半自动标注:
# auto_polygon.py import cv2 import numpy as np from labelme import utils def detect_traffic_light(image): """基于HSV颜色空间检测信号灯区域""" hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # 定义各颜色阈值范围 lower_red = np.array([0, 120, 70]) upper_red = np.array([10, 255, 255]) lower_green = np.array([35, 50, 50]) upper_green = np.array([90, 255, 255]) lower_yellow = np.array([15, 100, 100]) upper_yellow = np.array([35, 255, 255]) masks = { 'red': cv2.inRange(hsv, lower_red, upper_red), 'green': cv2.inRange(hsv, lower_green, upper_green), 'yellow': cv2.inRange(hsv, lower_yellow, upper_yellow) } return masks该脚本可自动检测信号灯大致区域,标注时只需微调多边形顶点,效率提升50%以上。
2.3 后处理自动化
标注完成后,通过以下脚本批量处理JSON文件:
#!/bin/bash # batch_process.sh # 1. 格式验证 python3 -c " import json, glob for f in glob.glob('annotations/*.json'): try: json.load(open(f)) except Exception as e: print(f'Invalid JSON: {f}') exit(1) " # 2. 转换为YOLO格式 python3 json2yolo.py --input-dir annotations --output-dir labels --class-map traffic_light_classes.txt # 3. 数据集划分 python3 split_dataset.py --image-dir images --label-dir labels --output-dir dataset --ratios 0.7 0.2 0.13. 高级批处理技巧
3.1 并行化处理
利用GNU Parallel加速大批量数据转换:
# 安装parallel sudo apt-get install parallel # 并行转换JSON到YOLO格式 find ./annotations -name "*.json" | parallel -j 8 python3 json2yolo.py --input {} --output-dir labels参数说明:
-j 8:使用8个并行进程{}:表示输入文件名占位符
3.2 自动质量检查
开发自动检查脚本确保标注质量:
# quality_check.py import json import os from pathlib import Path def check_annotation(json_file): with open(json_file) as f: data = json.load(f) issues = [] # 检查标签命名一致性 for shape in data['shapes']: if shape['label'].lower() not in ['red', 'green', 'yellow']: issues.append(f"Invalid label: {shape['label']}") # 检查多边形闭合 for shape in data['shapes']: points = shape['points'] if points[0] != points[-1]: issues.append("Polygon not closed") return issues3.3 增量标注管理
使用Git管理标注进度:
# 初始化Git仓库 git init git add . git commit -m "Initial annotations" # 每日工作流程 git add -u git commit -m "Day1 annotations" git diff HEAD~1 --stat # 查看当日修改4. 与YOLOv5训练流程集成
4.1 自动生成配置文件
动态创建YOLOv5所需的dataset.yaml:
# gen_config.py import yaml config = { 'train': '../dataset/train', 'val': '../dataset/valid', 'test': '../dataset/test', 'nc': 3, 'names': ['red', 'green', 'yellow'] } with open('traffic_light.yaml', 'w') as f: yaml.dump(config, f)4.2 一键训练脚本
#!/bin/bash # train_yolov5.sh # 准备环境 conda activate yolov5 cd ~/yolov5 # 开始训练 python train.py \ --img 640 \ --batch 16 \ --epochs 100 \ --data ../traffic_light.yaml \ --weights yolov5s.pt \ --cache ram # 使用内存缓存加速4.3 训练监控与优化
使用TensorBoard监控训练过程:
tensorboard --logdir runs/train关键监控指标:
- 损失函数曲线
- mAP@0.5
- 类别准确率
- 内存/GPU使用情况
5. 实战经验与避坑指南
在多个实际项目中,我们总结了以下关键经验:
- 颜色空间选择:HSV比RGB更适合交通灯颜色检测
- 标注粒度控制:不必追求像素级完美,YOLOv5对轻微偏差有容忍度
- 数据增强策略:
- 雨天/雾天模拟
- 不同光照条件
- 小角度旋转
常见问题解决方案:
| 问题现象 | 可能原因 | 解决方案 |
|---|---|---|
| 训练mAP低 | 标注不一致 | 运行quality_check.py统一标准 |
| 验证损失震荡 | 数据分布不均 | 检查数据集划分比例 |
| GPU利用率低 | 批处理大小不当 | 逐步增加batch_size直到显存占满 |
这套方案在某城市智能交通项目中,将原本需要2周的人工标注工作压缩到3天内完成,同时保持了98%以上的标注准确率。关键在于建立标准化流程和自动化质量检查机制,而非单纯追求速度。
