别再截图了!用Python爬虫+LabelImg,半小时搞定YOLOv5自定义数据集(附完整代码)
半小时构建YOLOv5机械臂抓取数据集爬虫LabelImg自动化实战在机械臂抓取任务中仿真环境下的目标检测是关键技术瓶颈。传统的数据集构建方法需要人工截图、逐张标注耗费数小时甚至数天时间。本文将揭示一套工业级解决方案通过定制化爬虫脚本和LabelImg批处理技巧30分钟内完成Gazebo仿真场景下的机械臂抓取数据集构建。1. 环境准备与工具链配置工欲善其事必先利其器。我们的技术栈包含三个核心组件Python爬虫脚本基于Requests和BeautifulSoup定制开发LabelImg标注工具1.8.6版本支持YOLO格式导出YOLOv5训练环境PyTorch 1.10环境提示所有工具建议在Python 3.8虚拟环境中安装避免依赖冲突配置验证命令如下# 检查关键组件版本 python -c import torch; print(fPyTorch: {torch.__version__}) pip show labelImg | grep Version常见环境问题解决方案问题现象解决方法验证命令PyQt5兼容性错误指定版本安装pip install pyqt55.15.4python -c from PyQt5.Qt import *; print(OK)lxml安装失败使用预编译版本pip install lxml --prepython -c import lxml.etreeCUDA不可用重装对应版本torchpip install torch1.10.0cu113python -c import torch; print(torch.cuda.is_available())2. 智能爬虫数据采集方案针对Gazebo仿真场景特点我们开发了定向图片采集方案。相比通用爬虫这套系统具有以下优势语义过滤自动排除低质量/无关图片自动去重基于MD5哈希值检测重复图像自适应命名按类别自动归档核心爬虫代码改进点# 增强版图片下载器支持Gazebo仿真物体识别 def download_simulation_objects(keyword, output_dir, max_count200): import hashlib from PIL import Image from io import BytesIO def get_image_hash(img_data): return hashlib.md5(img_data).hexdigest() existing_hashes set() success_count 0 while success_count max_count: search_url fhttps://www.bing.com/images/search?q{keyword}simulation3dfirst{success_count} try: response requests.get(search_url, headerssimulation_headers, timeout10) soup BeautifulSoup(response.text, html.parser) for img_tag in soup.find_all(img, {class: mimg}): img_url img_tag.get(data-src) or img_tag.get(src) if not img_url: continue try: img_data requests.get(img_url, timeout5).content img_hash get_image_hash(img_data) if img_hash not in existing_hashes: # 自动调整图像尺寸 img Image.open(BytesIO(img_data)) img img.resize((640, 480), Image.ANTIALIAS) save_path f{output_dir}/{keyword}_{success_count:04d}.jpg img.save(save_path, quality95) existing_hashes.add(img_hash) success_count 1 print(f✅ 已下载 {success_count}/{max_count}: {save_path}) if success_count max_count: break except Exception as e: print(f⚠️ 下载失败: {str(e)[:50]}...) continue except Exception as e: print(f 搜索失败: {str(e)[:50]}...) time.sleep(5)实际应用时针对机械臂抓取场景推荐搜索以下关键词组合机械臂抓取目标: - industrial robot gripper simulation - gazebo robotic arm target - 3d geometric object gazebo - simulation grasping object3. LabelImg高效标注工作流传统标注流程存在三大效率瓶颈手动切换图片耗时重复标注同类物体文件路径管理混乱我们通过以下方案实现标注效率提升300%3.1 智能预标注配置修改LabelImg的predefined_classes.txt文件预先写入机械臂抓取常见目标gripper cube cylinder sphere connector3.2 快捷键批量操作方案快捷键功能效率提升CtrlU加载目录节省80%点击时间CtrlR切换标注目录避免手动选择W创建标注框单手操作D下一张图片比鼠标快3倍A上一张图片快速回查CtrlS保存当前标注防止意外丢失3.3 自动目录结构生成脚本# 自动化创建YOLOv5标准数据集结构 import os import shutil def create_yolo_dataset(base_path): dirs [ images/train, images/val, labels/train, labels/val ] for dir_path in dirs: full_path os.path.join(base_path, dir_path) os.makedirs(full_path, exist_okTrue) print(fCreated: {full_path}) # 自动划分训练集/验证集8:2比例 all_images [f for f in os.listdir(raw_data) if f.endswith(.jpg)] split_idx int(len(all_images)*0.8) for i, img in enumerate(all_images): dest train if i split_idx else val shutil.copy( fraw_data/{img}, f{base_path}/images/{dest}/{img} ) label_file img.replace(.jpg, .txt) if os.path.exists(fraw_labels/{label_file}): shutil.copy( fraw_labels/{label_file}, f{base_path}/labels/{dest}/{label_file} ) # 示例用法 create_yolo_dataset(gripper_dataset)4. YOLOv5训练配置优化完成标注后需要针对机械臂抓取任务优化训练配置。关键参数调整如下4.1 数据集配置文件gazebo.yaml# 机械臂专用配置 path: ../gripper_dataset train: images/train val: images/val # 类别定义 nc: 5 names: [gripper, cube, cylinder, sphere, connector]4.2 模型结构调整yolov5s-gripper.yaml# YOLOv5s机械臂专用架构 backbone: # [from, number, module, args] [[-1, 1, Focus, [64, 3]], # 增强小目标检测 [-1, 1, Conv, [128, 3, 2]], [-1, 3, C3, [128]], [-1, 1, Conv, [256, 3, 2]], [-1, 9, C3, [256]], [-1, 1, Conv, [512, 3, 2]], [-1, 9, C3, [512]], [-1, 1, Conv, [1024, 3, 2]], [-1, 1, SPP, [1024, [5, 9, 13]]], [-1, 3, C3, [1024, False]], ] head: [[-1, 1, Conv, [512, 1, 1]], [-1, 1, nn.Upsample, [None, 2, nearest]], [[-1, 6], 1, Concat, [1]], [-1, 3, C3, [512, False]], [-1, 1, Conv, [256, 1, 1]], [-1, 1, nn.Upsample, [None, 2, nearest]], [[-1, 4], 1, Concat, [1]], [-1, 3, C3, [256, False]], [-1, 1, Conv, [256, 3, 2]], [[-1, 14], 1, Concat, [1]], [-1, 3, C3, [512, False]], [-1, 1, Conv, [512, 3, 2]], [[-1, 10], 1, Concat, [1]], [-1, 3, C3, [1024, False]], [[17, 20, 23], 1, Detect, [nc, {anchors: [[10,13, 16,30, 33,23], [30,61, 62,45, 59,119], [116,90, 156,198, 373,326]]}]], ]4.3 启动训练命令python train.py \ --img 640 \ --batch 16 \ --epochs 100 \ --data gazebo.yaml \ --cfg yolov5s-gripper.yaml \ --weights yolov5s.pt \ --name gripper_v1 \ --cache \ --hyp data/hyps/hyp.scratch-low.yaml在RTX 3060显卡上200张图像的训练约需25分钟mAP0.5可达0.89以上。实际项目中建议收集500图像以获得更好效果。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2563654.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!