使用python写一个脚本将yolo格式的目标检测数据集转换为voc格式的目标检测数据集
时间: 2023-06-19 22:10:04 浏览: 103
好的,以下是一个将yolo格式的数据集转换为voc格式的脚本。请确保您已经安装了必要的Python库,如xml.etree.ElementTree和opencv。
```python
import os
import cv2
import xml.etree.ElementTree as ET
classes = ['class1', 'class2', 'class3'] # 填写您的类别列表
yolo_labels_folder = 'yolo_labels/' # 填写您的yolo标签文件夹
voc_labels_folder = 'voc_labels/' # 填写您要输出的voc标签文件夹
image_folder = 'images/' # 填写您的图像文件夹
def convert(size, box):
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)
for filename in os.listdir(yolo_labels_folder):
if filename.endswith('.txt'):
img_id = filename[:-4]
img_path = image_folder + img_id + '.jpg'
img = cv2.imread(img_path)
h, w, _ = img.shape
# 创建新的voc标签文件
root = ET.Element('annotation')
ET.SubElement(root, 'folder').text = 'VOC2007'
ET.SubElement(root, 'filename').text = img_id + '.jpg'
source = ET.SubElement(root, 'source')
ET.SubElement(source, 'database').text = 'Unknown'
size = ET.SubElement(root, 'size')
ET.SubElement(size, 'width').text = str(w)
ET.SubElement(size, 'height').text = str(h)
ET.SubElement(size, 'depth').text = '3'
ET.SubElement(root, 'segmented').text = '0'
with open(yolo_labels_folder + filename, 'r') as f:
lines = f.readlines()
for line in lines:
line = line.strip().split(' ')
class_id = int(line[0])
x_min = int(float(line[1]) * w - float(line[3]) * w / 2)
y_min = int(float(line[2]) * h - float(line[4]) * h / 2)
x_max = int(float(line[1]) * w + float(line[3]) * w / 2)
y_max = int(float(line[2]) * h + float(line[4]) * h / 2)
obj = ET.SubElement(root, 'object')
ET.SubElement(obj, 'name').text = classes[class_id]
ET.SubElement(obj, 'pose').text = 'Unspecified'
ET.SubElement(obj, 'truncated').text = '0'
ET.SubElement(obj, 'difficult').text = '0'
bbox = ET.SubElement(obj, 'bndbox')
ET.SubElement(bbox, 'xmin').text = str(x_min)
ET.SubElement(bbox, 'ymin').text = str(y_min)
ET.SubElement(bbox, 'xmax').text = str(x_max)
ET.SubElement(bbox, 'ymax').text = str(y_max)
# 保存voc标签文件
tree = ET.ElementTree(root)
tree.write(voc_labels_folder + img_id + '.xml')
```
请将代码中的“classes”,“yolo_labels_folder”,“voc_labels_folder”和“image_folder”替换为您的自定义变量。此脚本假定yolo标签文件和图像文件具有相同的文件名(但文件扩展名不同),并且它们都位于yolo_labels_folder和image_folder中。
此脚本将在voc_labels_folder中生成与yolo标签文件相对应的voc标签文件。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)