pycocotools 包处调用coco.py 报错TypeError: Argument 'bb' has incorrect type (expected numpy.ndarray, got list)
报这个错误是传入的segment点是四个以内(包含四个)的会触发的错误。找到环境安装位置的coco.py文件,修改420行,增加条件判断就好了✔️。
if len(segm[0])<5:
segm[0].append(0)
修改后代码如下:
def annToRLE(self, ann):
"""
Convert annotation which can be polygons, uncompressed RLE to RLE.
:return: binary mask (numpy 2D array)
"""
t = self.imgs[ann['image_id']]
h, w = t['height'], t['width']
segm = ann['segmentation']
if len(segm[0])<5: #增加此行
segm[0].append(0) #增加此行
if type(segm) == list:
# polygon -- a single object might consist of multiple parts
# we merge all parts into one mask rle code
rles = maskUtils.frPyObjects(segm, h, w)
rle = maskUtils.merge(rles)
elif type(segm['counts']) == list:
# uncompressed RLE
rle = maskUtils.frPyObjects(segm, h, w)
else:
# rle
rle = ann['segmentation']
return rle
如果还报错,比如我的新报错信息:
报错信息:File "pycocotools/_mask.pyx", line 303, in pycocotools._mask.frPyObjects
File "pycocotools/_mask.pyx", line 265, in pycocotools._mask.frPoly
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (15,) + inhomogeneous part.
报错原因:说我的数据是不均匀的,打印了以下segm才发现,是因为我的数据里有长度为3的list,也就是说我的数据中有两个点甚至一个点组成的目标,显然这不是我关心的目标,因为他太小了,所以我索性在这把这种样本删除了。
修改代码:
if len(segm[0])==4: #如果差一个点,可以补全
segm[0].append(0)
elif len(segm[0])<4: #如果差好几个,索性删掉
segm.remove(segm[0])
修改后函数为:
def annToRLE(self, ann):
"""
Convert annotation which can be polygons, uncompressed RLE to RLE.
:return: binary mask (numpy 2D array)
"""
t = self.imgs[ann['image_id']]
h, w = t['height'], t['width']
segm = ann['segmentation']
#-------------------修改位置----------------------------
if len(segm[0])==4: #如果差一个点,可以补全
segm[0].append(0)
elif len(segm[0])<4: #如果差好几个,索性删掉
segm.remove(segm[0])
#-------------------修改位置----------------------------
if type(segm) == list:
# polygon -- a single object might consist of multiple parts
# we merge all parts into one mask rle code
rles = maskUtils.frPyObjects(segm, h, w)
rle = maskUtils.merge(rles)
elif type(segm['counts']) == list:
# uncompressed RLE
rle = maskUtils.frPyObjects(segm, h, w)
else:
# rle
rle = ann['segmentation']
return rle
⛵⛵⭐⭐