yolov11数据集转换
时间: 2025-04-30 16:48:06 浏览: 28
### YOLOv11 数据集转换方法
在实际应用中,YOLOv11 的数据集通常采用特定的标注格式存储。为了适配不同的需求或将数据集转换为其他框架支持的格式,可以遵循以下方式实现。
#### 1. **理解原始数据结构**
YOLOv11 使用的标准标注文件通常是 `.txt` 文件,每行表示一个目标框,其格式如下:
```
<class_id> <x_center> <y_center> <width> <height>
```
其中 `<class_id>` 是类别索引,而其余四个参数是以相对图像尺寸的比例形式给出的目标边界框中心坐标及其宽高[^1]。
#### 2. **目标格式分析**
要将 YOLOv11 数据集转换为目标格式(例如 Pascal VOC 或 COCO),需了解这些格式的具体定义:
- **Pascal VOC**: XML 文件描述每个对象的位置和类别。
- **COCO**: JSON 文件记录整个数据集的信息,包括图片路径、类别标签以及边界框位置。
#### 3. **编写转换脚本**
以下是 Python 脚本示例,用于将 YOLOv11 格式的标注转换为 Pascal VOC 和 COCO 格式。
##### (a) 转换至 Pascal VOC 格式
```python
import os
from xml.dom.minidom import Document
def yolo_to_voc(yolo_dir, image_size, output_dir):
for txt_file in os.listdir(yolo_dir):
if not txt_file.endswith('.txt'):
continue
doc = Document()
annotation = doc.createElement('annotation')
doc.appendChild(annotation)
folder = doc.createElement('folder')
folder.appendChild(doc.createTextNode('images'))
annotation.appendChild(folder)
filename = doc.createElement('filename')
filename.appendChild(doc.createTextNode(txt_file.replace('.txt', '.jpg')))
annotation.appendChild(filename)
size = doc.createElement('size')
width = doc.createElement('width')
height = doc.createElement('height')
w, h = image_size
width.appendChild(doc.createTextNode(str(w)))
height.appendChild(doc.createTextNode(str(h)))
size.appendChild(width)
size.appendChild(height)
annotation.appendChild(size)
with open(os.path.join(yolo_dir, txt_file), 'r') as f:
lines = f.readlines()
for line in lines:
class_id, x_center, y_center, box_w, box_h = map(float, line.strip().split())
xmin = int((x_center - box_w / 2) * w)
ymin = int((y_center - box_h / 2) * h)
xmax = int((x_center + box_w / 2) * w)
ymax = int((y_center + box_h / 2) * h)
object_elem = doc.createElement('object')
name = doc.createElement('name')
name.appendChild(doc.createTextNode(f'class_{int(class_id)}'))
bndbox = doc.createElement('bndbox')
xmnelem = doc.createElement('xmin')
xmnelem.appendChild(doc.createTextNode(str(xmin)))
ymnelem = doc.createElement('ymin')
ymnelem.appendChild(doc.createTextNode(str(ymin)))
xmxelem = doc.createElement('xmax')
xmxelem.appendChild(doc.createTextNode(str(xmax)))
ymxelem = doc.createElement('ymax')
ymxelem.appendChild(doc.createTextNode(str(ymax)))
bndbox.appendChild(xmnelem)
bndbox.appendChild(ymnelem)
bndbox.appendChild(xmxelem)
bndbox.appendChild(ymxelem)
object_elem.appendChild(name)
object_elem.appendChild(bndbox)
annotation.appendChild(object_elem)
with open(os.path.join(output_dir, txt_file.replace('.txt', '.xml')), 'w') as f:
f.write(doc.toprettyxml(indent=" "))
```
##### (b) 转换至 COCO 格式
```python
import json
import os
def yolo_to_coco(yolo_dir, images_dir, output_path):
categories = [{"id": i, "name": f"class_{i}"} for i in range(10)] # 假设有10类
annotations = []
images = []
ann_id = 1
for img_idx, txt_file in enumerate(os.listdir(yolo_dir)):
if not txt_file.endswith('.txt'):
continue
img_name = txt_file.replace('.txt', '.jpg')
img_width, img_height = 640, 640 # 默认假设分辨率一致
image_info = {
"file_name": img_name,
"height": img_height,
"width": img_width,
"id": img_idx + 1
}
images.append(image_info)
with open(os.path.join(yolo_dir, txt_file), 'r') as f:
lines = f.readlines()
for line in lines:
class_id, x_center, y_center, box_w, box_h = map(float, line.strip().split())
bbox_x = (x_center - box_w / 2) * img_width
bbox_y = (y_center - box_h / 2) * img_height
bbox_w = box_w * img_width
bbox_h = box_h * img_height
area = bbox_w * bbox_h
anno = {
"segmentation": [],
"area": area,
"iscrowd": 0,
"image_id": img_idx + 1,
"bbox": [bbox_x, bbox_y, bbox_w, bbox_h],
"category_id": int(class_id),
"id": ann_id
}
annotations.append(anno)
ann_id += 1
coco_data = {
"info": {},
"licenses": [],
"categories": categories,
"annotations": annotations,
"images": images
}
with open(output_path, 'w') as f:
json.dump(coco_data, f, indent=4)
```
上述代码分别实现了从 YOLOv11 到 Pascal VOC 和 COCO 的转换逻辑。
---
###
阅读全文
相关推荐


















