carrat 2021-12-20 06:12 采纳率: 57.1%
浏览 315
已结题

出现“TypeError: 'NoneType' object is not subscriptable”,怎么解决?

问题相关代码
def read_img(img_fp):
    img = cv2.imread(img_fp)[:, :, ::-1]
    h_origin, w_origin, _ = img.shape
    return img, h_origin, w_origin

运行结果及报错内容

TypeError: 'NoneType' object is not subscriptable

直接删掉imread(img_fp)后面的[:, :, ::-1]
def read_img(img_fp):
    img = cv2.imread(img_fp)
    h_origin, w_origin, _ = img.shape
    return img, h_origin, w_origin

结果报错内容为 AttributeError: 'NoneType' object has no attribute 'shape'

  • 写回答

4条回答 默认 最新

  • 陈年椰子 2021-12-20 06:53
    关注

    这个情况, 两种可能, 1是图片路径不对,2是图片文件格式与扩展名不符。
    找个标准图片先测试。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    carrat 2021-12-20 11:37

    
    def main(net, args):
        img_path = args.image_path.replace("file://", "")
        img_fn = img_path.split("/")[-1]
        assert os.path.exists(img_path)
        img_origin, h_origin, w_origin = read_img(img_path)
        tmp_img = test_preprocess(img_origin, to_tensor=True,
                                  pad=False).to(args.device)
    

    不知道是不是这个地方img_path = args.image_path.replace("file://", "")有问题,我不太清楚这两个""之间应该怎么填?因为是test.py代码里的,我在第一个""里面添加的是测试集文件夹路径,但是后面那个不清楚

    回复
查看更多回答(3条)
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 12月29日
  • 已采纳回答 12月22日
  • 创建了问题 12月20日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部