kitti資料集轉成voc,老是報錯AttributeError: 'NoneType' object has no attribute 'shape',要瘋了,麻煩幫忙解決一下。感謝。

------utils.py------
import os,cv2,sys,shutil
from xml.dom.minidom import Document
def writexml(filename,saveimg,bboxes,xmlpath,typename):
doc = Document()
annotation = doc.createElement('annotation')
doc.appendChild(annotation)
folder = doc.createElement('folder')
folder_name = doc.createTextNode('kitti')
folder.appendChild(folder_name)
annotation.appendChild(folder)
filenamenode = doc.createElement('filename')
filename_name = doc.createTextNode(filename)
filenamenode.appendChild(filename_name)
annotation.appendChild(filenamenode)
source = doc.createElement('source')
annotation.appendChild(source)
database = doc.createElement('database')
database.appendChild(doc.createTextNode('kitti Database'))
source.appendChild(database)
annotation_s = doc.createElement('annotation')
annotation_s.appendChild(doc.createTextNode('kitti'))
source.appendChild(annotation_s)
image = doc.createElement('image')
image.appendChild(doc.createTextNode('flickr'))
source.appendChild(image)
flickrid = doc.createElement('flickrid')
flickrid.appendChild(doc.createTextNode('-1'))
source.appendChild(flickrid)
owner = doc.createElement('owner')
annotation.appendChild(owner)
flickrid_o = doc.createElement('flickrid')
flickrid_o.appendChild(doc.createTextNode('muke'))
owner.appendChild(flickrid_o)
name_o = doc.createElement('name')
name_o.appendChild(doc.createTextNode('muke'))
owner.appendChild(name_o)
size = doc.createElement('size')
annotation.appendChild(size)
width = doc.createElement('width')
width.appendChild(doc.createTextNode(str(saveimg.shape[1])))
height = doc.createElement('height')
height.appendChild(doc.createTextNode(str(saveimg.shape[0])))
depth = doc.createElement('depth')
depth.appendChild(doc.createTextNode(str(saveimg.shape[2])))
size.appendChild(width)
size.appendChild(height)
size.appendChild(depth)
segmented = doc.createElement('segmented')
segmented.appendChild(doc.createTextNode('0'))
annotation.appendChild(segmented)
for i in range(len(bboxes)):
bbox = bboxes[i]
objects = doc.createElement('object')
annotation.appendChild(objects)
object_name = doc.createElement('name')
object_name.appendChild(doc.createTextNode(typename[i]))
objects.appendChild(object_name)
pose = doc.createElement('pose')
pose.appendChild(doc.createTextNode('Unspecified'))
objects.appendChild(pose)
truncated = doc.createElement('truncated')
truncated.appendChild(doc.createTextNode('1'))
objects.appendChild(truncated)
difficult = doc.createElement('difficult')
difficult.appendChild(doc.createTextNode('0'))
objects.appendChild(difficult)
bndbox = doc.createElement('bndbox')
objects.appendChild(bndbox)
xmin = doc.createElement('xmin')
xmin.appendChild(doc.createTextNode(str(bbox[0])))
bndbox.appendChild(xmin)
ymin = doc.createElement('ymin')
ymin.appendChild(doc.createTextNode(str(bbox[1])))
bndbox.appendChild(ymin)
xmax = doc.createElement('xmax')
xmax.appendChild(doc.createTextNode(str(bbox[2])))
bndbox.appendChild(xmax)
ymax = doc.createElement('ymax')
ymax.appendChild(doc.createTextNode(str(bbox[3])))
bndbox.appendChild(ymax)
f = open(xmlpath, "w")
f.write(doc.toprettyxml(indent=''))
f.close()
------demo.py------
import cv2
import glob
from fasterrcnn_kitti.utils import writexml
trainval = open("F:/Database/kitti/ImageSets/Main/trainval.txt", "w")
train = open("F:/Database/kitti/ImageSets/Main/train.txt", "w")
val = open("F:/Database/kitti/ImageSets/Main/val.txt", "w")
test = open("F:/Database/kitti/ImageSets/Main/test.txt", "w")
list_anno_files = glob.glob("F:Database/kitti/training/label_2/*")
idx = 0
for file_path in list_anno_files:
with open(file_path) as file:
anno_infos = file.readlines()
print(anno_infos)
bboxes = []
typename = []
for anno_item in anno_infos:
anno_infos = anno_item.split(" ")
if anno_infos[0] == "Misc" or anno_infos[1] == "DontCare":
continue
else:
bbox = (int(float(anno_infos[4])),int(float(anno_infos[5])),
int(float(anno_infos[6])),int(float(anno_infos[7])))
bboxes.append(bbox)#bboxes是邊界框的坐標
typename.append(anno_infos[0])#typename是類別的名字
filename = file_path.split("/")[-1].replace("txt","png")#filename是圖片的名字
xmlpath = "F:/Database/kitti/Annotations/" + filename.replace("png", "xml")
imgpath = "F:/Database/kitti/JPEGImages/" + filename
saveimg = cv2.imread(imgpath)
writexml(filename, saveimg, bboxes, xmlpath, typename)
if idx > len(list_anno_files) * 0.9:
test.write(filename.replace(".png", "\n"))
else:
trainval.write(filename.replace(".png", "\n"))
if idx > len(list_anno_files)*0.7:
val.write(filename.replace(".png", "\n"))
else:
train.write(filename.replace(".png", "\n"))
idx += 1
trainval.close()
test.close()
val.close()
train.close()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/128774.html
標籤:機器視覺
上一篇:1、目錄的操作命令
