labelme 大圖 yolo
时间: 2025-05-07 18:11:13 浏览: 19
### 如何使用 LabelMe 标注大图像并将其转换为 YOLO 数据集
#### 工具安装
为了开始标注过程,首先需要确保已正确安装 `labelme` 工具。可以通过以下命令完成安装[^2]:
```bash
pip install labelme
```
#### 图像标注流程
1. **启动 LabelMe 工具**:
使用以下命令运行 LabelMe GUI 应用程序来加载待标注的大图像文件。
```bash
labelme
```
2. **打开目标图像**:
在工具界面中点击 “Open” 或者通过快捷键 Ctrl+O 打开本地存储的目标大图像。
3. **创建标注区域**:
对于每张图像中的对象,可以手动绘制边界框或者多边形区域。对于关键点检测任务,则需逐一标记每个感兴趣的关键点位置。
4. **保存标注结果**:
完成单幅图像的标注之后,记得及时保存当前工作成果至 JSON 文件格式。此操作可通过菜单栏上的 Save 功能实现。
#### 将 LabelMe 的 JSON 转换为 YOLO 格式
一旦完成了所有图片的手动标注,下一步就是把这些标注信息转化为适合 YOLO 训练使用的纯文本形式 (`.txt`) 。这里介绍两种方法:
##### 方法一:利用官方推荐插件 Labelme2YOLO 进行批量处理
按照如下步骤执行即可快速完成转化:
- 前往项目主页克隆仓库到本地环境:
```bash
git clone https://gitcode.com/gh_mirrors/la/Labelme2YOLO.git
cd Labelme2YOLO
```
- 准备好包含多个 `.json` 文件的一个目录路径作为输入源;
- 修改脚本参数指定输出目标文件夹名以及类别名称列表;
- 启动 Python 脚本来触发实际的数据预处理逻辑[^1]:
```python
from labelme2yolo import convert
convert(
input_dir="path/to/jsons",
output_dir="output/folder",
classes=["class_name_1","class_name_2"] # 替换成具体分类标签集合
)
```
##### 方法二:自定义编写转换器代码样例
如果不想依赖第三方库也可以自己动手开发简单的解析函数来自动生成对应格式的结果文档。下面给出一段基础示范代码片段供参考:
```python
import os
import json
def parse_labelme_json(json_file, out_txt):
with open(json_file) as f:
data = json.load(f)
img_width = data['imageWidth']
img_height = data['imageHeight']
shapes = [{'label': shape['label'], 'points': shape['points']} for shape in data['shapes']]
lines = []
for i, obj in enumerate(shapes):
cls_id = class_names.index(obj["label"])
points = obj['points'][0]
x_center = ((points[0]) / img_width )
y_center =((points[1])/img_height )
width=abs(points[2]-points[0])/img_width
height= abs(points[3]-points[1])/img_height
line=f"{cls_id} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f}\n"
lines.append(line)
with open(out_txt,'w')as wf :
wf.writelines(lines)
if __name__ == '__main__':
root_path='your/path'
class_names=['person','car',...]#替换为您数据集中涉及的所有类别的名字数组
files=os.listdir(root_path)
for fname in filter(lambda s:s.endswith('.json'),files):
src=os.path.join(root_path,fname)
dst=os.path.splitext(src)[0]+'.txt'
try:
parse_labelme_json(src,dst)
except Exception as e:
print(e)
```
上述例子假设每条记录仅含单一矩形框描述;针对更复杂情况比如旋转角度支持则还需额外扩展算法细节部分[^3]。
---
阅读全文
相关推荐


















