AttributeError: ‘NoneType’ object has no attribute 'shape’解决方案
在使用OpenCV处理图片时,比如笔者在书写下列代码时:
cv_img = cv2.imread(dataset_root_path + "total/" + filestr + "/img.png")
self.add_image("shapes", image_id=i, path=img_floder + "/" + imglist[i],
width=cv_img.shape[1], height=cv_img.shape[0], mask_path=mask_path, yaml_path=yaml_path)
提示笔者:
AttributeError: 'NoneType' object has no attribute 'shape'
问题分析:
这个原因是因为找不到这个cv_img 图片。
解决方案:
有可能您出现的问题是处在一个循环中,那么找出来的话,可以参考使用抛出异常,具体如下:
cv_img = cv2.imread(dataset_root_path + "total/" + filestr + "/img.png")
try:
self.add_image("shapes", image_id=i, path=img_floder + "/" + imglist[i],
width=cv_img.shape[1], height=cv_img.shape[0], mask_path=mask_path, yaml_path=yaml_path)
except:
print(dataset_root_path + "total/" + filestr + "/img.png")
那么抛出来的路径,追进去看看,你会发现你的名字可能写错了,总之他找不到了~~~~改了让他找到就行了。