我参加NVIDIA Sky Hackathon(VOC转kitti)

在第七届第七届NVIDIA SKy Hackathon中,我们团队遇到一个难题,将voc格式转为kitti格式,因为是初次遇到此类问题,我们上网搜了很多资料,最后是通过脚本来转换格式,在pycharm中生成kitti的txt文本。

#1以voc格式打标签

#转换文本格式

打开pycharm,从文件中选中一个.xml格式。在

xmlfile = ""中添加路径。
# import xmltodict
import xml.dom.minidom

xmlfile = "R-C.xml"

document_tree = xml.dom.minidom.parse(xmlfile)
collection = document_tree.documentElement
# print(collection.toxml())

class_record = []
xmin_record = []
ymin_record = []
xmax_record = []
ymax_record = []

filename = collection.getElementsByTagName("filename")
filename = filename[0].childNodes[0].data

name = collection.getElementsByTagName("name")
print(name[0].childNodes[0].data)

for name_object in name:
    class_name = name_object.childNodes[0].nodeValue
    class_name = str(class_name)
    class_record.append(class_name)
print(class_record)
print(len(class_record))

xmin = collection.getElementsByTagName("xmin")
# print(xmin[0].childNodes[0].data)
ymin = collection.getElementsByTagName("ymin")
xmax = collection.getElementsByTagName("xmax")
ymax = collection.getElementsByTagName("ymax")

for xmin_object in xmin:
    # print(good_object.childNodes)
    x1 = xmin_object.childNodes[0].nodeValue
    x1 = float(x1)
    xmin_record.append(x1)
print(xmin_record)

for ymin_object in ymin:
    y1 = ymin_object.childNodes[0].nodeValue
    y1 = float(y1)
    ymin_record.append(y1)
print(ymin_record)

for xmax_object in xmax:
    x2 = xmax_object.childNodes[0].nodeValue
    x2 = float(x2)
    xmax_record.append(x2)
print(xmax_record)

for ymax_object in ymax:
    y2 = ymax_object.childNodes[0].nodeValue
    y2 = float(y2)
    ymax_record.append(y2)
print(ymax_record)

label_file = str(filename).replace(".jpg", ".txt")
print(label_file)
for i in range(len(class_record)):
    with open(label_file, 'a') as j:
        j.write("{} 0.0 0 0.0 {:.1f} {:.1f} {:.1f} {:.1f} 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n".format(class_record[i],
                                                                                                xmin_record[i],
                                                                                                ymin_record[i],
                                                                                                xmax_record[i],
                                                                                                ymax_record[i]))

生成txt文本

我参加NVIDIA Sky Hackathon(VOC转kitti)_第1张图片

 参考资料xml convert to KITTI - cdekelon - 博客园

你可能感兴趣的:(深度学习,python)