我已经构建了一个数据集来训练YOLOv4,并且我拥有YOLO格式的所有标签(我使用了LabelImg(。现在我想用相同的数据集训练SSD,因此我需要PASCAL VOC格式的标签。我已经看到了一些相反的转换方法(voc到yolo(,但不是我想要的。由于我有成千上万的图像,我想找到一种方法来自动化整个过程,而不必逐一检查每个图像/标签。
有人对如何处理这个问题有什么想法吗?
提前感谢!
我也遇到过同样的问题。我写了一个脚本,将为你做这个转换。您可以在此处找到代码链接:https://github.com/JPM-Tech/Object-Detection/tree/main/Scripts/converters/convert-yolo-to-xml.py.这是一些关于如何使用它的文档的链接:https://github.com/JPM-Tech/Object-Detection/tree/main/Scripts/converters#convert-yolo到xml。希望这能像帮助我一样帮助你。
我和我的同学创建了一个名为PyLabel的python包,以帮助其他人完成此任务和其他标签任务。你可以在这个笔记本上看到一个例子https://github.com/pylabel-project/samples/blob/main/yolo2coco.ipynb.
一般的解决方案与上面提到的其他解决方案相同,但代码封装在这个包中,所以您只需要对最小的代码进行修改。例如:
!pip install pylabel
from pylabel import importer
dataset = importer.ImportYoloV5(path=path_to_annotations)
dataset.export.ExportToVoc(dataset)
你可以在这里找到幕后使用的源代码https://github.com/pylabel-project/pylabel/tree/main/pylabel
以下是创建XML的脚本,其中提供了相关信息,即脚本开头的变量。
import numpy as np
from pathlib import Path
import xml.etree.cElementTree as ET
from PIL import Image
import os
image_path = "" # provide image path
anno_folder = "" # provide .XML folder path
filename = "" # provide image name
#Read each bounding box as a list of dictionary and append it in the list for each file
All_bboxes = "[{"xmin":1433,"xmax":1503,"ymin":1570,"ymax":1700,"skuLabel":"bus"}]"
img = np.array(Image.open(image_path).convert('RGB'))
annotation = ET.Element('annotation')
ET.SubElement(annotation, 'folder').text = str(anno_folder)
ET.SubElement(annotation, 'filename').text = str(filename)
ET.SubElement(annotation, 'path').text = str(filename)
source = ET.SubElement(annotation, 'source')
ET.SubElement(source, 'database').text = 'Unknown'
size = ET.SubElement(annotation, 'size')
ET.SubElement(size, 'width').text = str (img.shape[1])
ET.SubElement(size, 'height').text = str(img.shape[0])
ET.SubElement(size, 'depth').text = str(img.shape[2])
ET.SubElement(annotation, 'segmented').text = '0'
for item in All_bboxes:
label = item['Label']
xmax = item['xmax']
xmin = item['xmin']
ymin = item['ymin']
ymax = item['ymax']
object = ET.SubElement(annotation, 'object')
ET.SubElement(object, 'name').text = label
ET.SubElement(object, 'pose').text = 'Unspecified'
ET.SubElement(object, 'truncated').text = '0'
ET.SubElement(object, 'difficult').text = '0'
bndbox = ET.SubElement(object, 'bndbox')
ET.SubElement(bndbox, 'xmin').text = str(xmin)
ET.SubElement(bndbox, 'ymin').text = str(ymin)
ET.SubElement(bndbox, 'xmax').text = str(xmax)
ET.SubElement(bndbox, 'ymax').text = str(ymax)
tree = ET.ElementTree(annotation)
xml_file_name = os.path.join(anno_folder, f'{filename.split('.')[0]}.xml')
tree.write(xml_file_name)
在Yolo格式中,您有x y width height
,而在PASCAL VOC中,您则有xmin ymin xmax ymax
。因此,在将它们加载到All_bboxes
时,您必须将其转换。此函数将.xml格式转换为.txt,因此可以帮助您转换坐标。
第页。S.您可以在上面的代码上循环,使其适用于多个文件。
您可以使用此工具
pip install pascal-voc
from pascal import annotation_from_yolo
from pascal.utils import save_xml
label_map = {
0: "person",
1: "dog"
}
if __name__ == "__main__":
ann = annotation_from_yolo(
"000001.txt",
label_map=label_map,
img_w=353,
img_h=500)
xml = ann.to_xml()
save_xml("000001.xml", xml)