YOLO数据预处理或则训练时出现ValueError: invalid literal for int() with base 10: ‘‘64.1245678,解决方式
报错:ValueError: invalid literal for int() with base 10: ''64.1245678
解决方式:在做目标检测的时候,方框xmin,ymin,xmax,ymax含有小数的时候,在train时候,位置:
bboxes = np.array([list(map(int, box.split(','))) for box in line[1:]])
容易报错:ValueError: invalid literal for int() with base 10: ''64.1245678
具体原因就是无法转换成int类别。
使用了先转float在转int也没法,最后直接在xml文件转换为txt文件的时候,先转float,在转int,就可以了。代码就可以正常运行。
具体代码如下:
import os
import argparse
import xml.etree.ElementTree as ET
def convert_voc_annotation(data_path, data_type, anno_path, use_difficult_bbox=True):
classes = [ 'license-plate']
img_inds_file = os.path.join(data_path, 'ImageSets_ssd', 'Main', data_type + '.txt')
print(img_inds_file)
with open(img_inds_file, 'r') as f:
txt = f.readlines()
image_inds = [line.strip() for line in txt]
with open(anno_path, 'a') as f:
for image_ind in image_inds:
image_path = os.path.join(data_path, 'valid/images', image_ind + '.jpg')
annotation = image_path
label_path = os.path.join(data_path, 'valid/annots', image_ind + '.xml')
root = ET.parse(label_path).getroot()
objects = root.findall('object')
for obj in objects:
difficult = obj.find('difficult').text.strip()
if (not use_difficult_bbox) and(int(difficult) == 1):
continue
bbox = obj.find('bndbox')
class_ind = classes.index(obj.find('name').text.lower().strip())
xmin = int(round((float(bbox.find('xmin').text.strip())),2))
xmax = int(round((float(bbox.find('xmax').text.strip())),2))
ymin = int(round(float((bbox.find('ymin').text.strip())),2))
ymax = int(round(float((bbox.find('ymax').text.strip())),2))
print(xmin)
annotation += ' ' + ','.join([str(xmin), str(ymin), str(xmax), str(ymax), str(class_ind)])
print(annotation)
print(annotation)
f.write(annotation + "\n")
return len(image_inds)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--data_path", default="/content/drive/MyDrive/voc_2007/car_liscence/")
parser.add_argument("--train_annotation", default="/content/drive/MyDrive/voc_2007/car_liscence/data/voc_train.txt")
parser.add_argument("--test_annotation", default="/content/drive/MyDrive/voc_2007/car_liscence/data/voc_test.txt")
flags = parser.parse_args()
if os.path.exists(flags.train_annotation):os.remove(flags.train_annotation)
if os.path.exists(flags.test_annotation):os.remove(flags.test_annotation)
num1 = convert_voc_annotation(os.path.join(flags.data_path), 'test', flags.test_annotation, False)