YOLO和labelme
时间: 2025-02-01 17:01:05 浏览: 55
### YOLO与Labelme的关系
YOLO(You Only Look Once)是一种流行的实时目标检测算法,而Labelme是一个灵活的手动图像标注工具。两者之间的关系在于Labelme用于创建高质量的训练数据集,这些数据随后可以被转换成适合YOLO模型训练所需的格式。
#### 安装并使用Labelme进行图片数据标签制作
为了利用Labelme为YOLO准备数据集,首先需要安装该软件[^1]:
```bash
pip install labelme
```
启动Labelme可以通过命令行输入`labelme`来打开图形界面,在此界面上可以选择要标注的图片文件夹,并逐一对每张图片中的对象绘制边界框或分割区域。
#### 将JSON格式转为YOLO格式
由于Labelme默认保存的是`.json`格式的标注信息,这并不直接适用于YOLO框架下的学习过程。因此,完成标注之后还需要进一步处理以适应YOLO的要求。一种方法是手动编写Python脚本来解析Json文件并将它们转化为YOLO所接受的形式;另一种更为简便的方式则是借助专门为此目的开发的小型库——例如提到过的`Labelme2YOLO`[^2]。
对于后者来说,只需要按照官方文档说明克隆仓库、配置环境变量以及运行相应的转换指令即可实现自动化迁移工作流:
```bash
git clone https://gitcode.com/gh_mirrors/la/Labelme2YOLO.git
cd Labelme2YOLO
python setup.py install
labelme2yolo --input_dir ./labeled_images --output_dir ./yolo_dataset
```
上述命令会读取指定路径下所有的标注结果(`./labeled_images`),将其转变为YOLO兼容版本存储于另一位置(`./yolo_dataset`)内供后续操作调用。
#### 按比例划分数据集(训练、验证、测试)
最后一步是对整理好的YOLO格式的数据集做合理的切分,通常建议的比例大约为70%作为训练集(train),15%-20%留作验证(val), 剩余部分则构成测试(test)集合。这一环节同样能够依靠简单的随机抽样函数轻松达成,比如采用如下方式快速分配样本至各个子集中去:
```python
import os
from sklearn.model_selection import train_test_split
def split_data(image_folder, output_path, test_size=0.15, val_size=0.15):
images = [f for f in os.listdir(image_folder) if not f.startswith('.')]
# Split into training and testing sets first.
remaining, test_set = train_test_split(images, test_size=test_size)
# Then further divide the remainder between validation and final training set.
train_set, val_set = train_test_split(remaining, test_size=val_size/(1-test_size))
with open(os.path.join(output_path,'train.txt'), 'w') as fp:
fp.write("\n".join([os.path.splitext(x)[0] for x in train_set]))
with open(os.path.join(output_path,'val.txt'),'w')as fp:
fp.write("\n".join([os.path.splitext(x)[0]for x in val_set]))
with open(os.path.join(output_path,'test.txt'),'w')as fp:
fp.write("\n".join([os.path.splitext(x)[0]for x in test_set]))
split_data('./images', './data_splits')
```
这段代码实现了基于给定参数自动将原始图片列表划分为三个独立的部分,并分别记录对应的名称到相应文本文件中以便日后加载时识别哪些属于哪一类用途。
阅读全文
相关推荐

















