问题描述
在运行tools/x2coco.py 时,一直报错
assert xmax > xmin and ymax > ymin, “Box size error.”
AssertionError: Box size error
解决方法
修改x2coco.py,查看是哪里出了问题。
def voc_get_coco_annotation(obj, label2id):
label = obj.findtext('name')
filename = obj.findtext('filename') #加上名字
assert label in label2id, "label is not in label2id."
category_id = label2id[label]
bndbox = obj.find('bndbox')
xmin = int(bndbox.findtext('xmin'))
ymin = int(bndbox.findtext('ymin'))
xmax = int(bndbox.findtext('xmax'))
ymax = int(bndbox.findtext('ymax'))
#修改这里
assert xmax > xmin and ymax > ymin, "{}Box size error.{}and{}".format(filename,xmax,xmin)
o_width = xmax - xmin
o_height = ymax - ymin
anno = {
'area': o_width * o_height,
'iscrowd': 0,
'bbox': [xmin, ymin, o_width, o_height],
'category_id': category_id,
'ignore': 0,
}
return anno
最后发现是有一个文件标注有问题,xmax和xmin一样,用labelImg打开发现有一个错误标注,删掉后再运行就没有报错。
附上一个检测数据标注的代码
# chechk if xmin coordinates have 0 value,todo:checkif max is greater than width
import numpy as np
from PIL import Image
import xml.dom.minidom
import os
if __name__ == '__main__':
badFilesList = []
curDir = '/home/work/train/JPEGImages'
path = "/home/aistudio/work/train/Annotations/"
Assertion_provoker_y = []
Assertion_provoker_x = []
files = os.listdir(path)
for file in files:
file = os.path.join(path, file)
dom = xml.dom.minidom.parse(file)
root = dom.documentElement
size = dom.getElementsByTagName("size")
height = dom.getElementsByTagName("height")[0]
width = dom.getElementsByTagName("width")[0]
width = width.childNodes[0].data
height = height.childNodes[0].data
objects = dom.getElementsByTagName("object")
for object in objects:
bndbox = object.getElementsByTagName('bndbox')[0] ########
xmin = bndbox.getElementsByTagName('xmin')[0]
xmin_data = xmin.childNodes[0].data
ymin = bndbox.getElementsByTagName('ymin')[0]
ymin_data = ymin.childNodes[0].data
xmax = bndbox.getElementsByTagName('xmax')[0]
xmax_data = xmax.childNodes[0].data
ymax = bndbox.getElementsByTagName('ymax')[0]
ymax_data = ymax.childNodes[0].data
# check if the xmin or yin is 0
if (int(xmin_data) == 0):
print(file, "the x of it is 0")
if (int(ymin_data) == 0):
print(file, "the y of it is 0")
##check if min is greater than max
if (int(ymin_data) > int(ymax_data)) or ymax_data ==ymin_data:
Assertion_provoker_y.append(file)
if (int(xmin_data) > int(xmax_data)) or xmax_data ==xmin_data:
Assertion_provoker_x.append(file)
# check if x is greater than width,or y is greater than height
if (int(xmax_data) > int(width)) :
Assertion_provoker_x.append(file)
if (int(xmin_data) > int(width)):
Assertion_provoker_x.append(file)
if (int(ymax_data) > int(height)):
Assertion_provoker_y.append(file)
if (int(ymin_data) > int(height)):
Assertion_provoker_y.append(file)
print("checking assertion error done,already checked", len(files),
"files,with {} y_provoker".format(len(Assertion_provoker_y)))
print("checking assertion error done,already checked", len(files),
"files,with {} x_provoker".format(len(Assertion_provoker_x)))
print(Assertion_provoker_x)
#######################################################