【问题标题】:List image paths - 'Namespace' object has no attribute '__getitem__'列出图像路径 - “命名空间”对象没有属性“__getitem__”
【发布时间】:2018-06-10 18:52:39
【问题描述】:

我正在尝试返回图片路径,如下:

from imutils import paths
import argparse

# create parser
parser = argparse.ArgumentParser()
# define the command-line options
parser.add_argument('--dataset', required=True,help='path to the dataset')
# read the command-line arguments and interpret them
args = parser.parse_args()

imagePath = paths.list_images(args['dataset'])

print imagePath

但是,得到以下错误:

Traceback (most recent call last):
  File "test.py", line 11, in <module>
    imagePath = paths.list_images(args['dataset'])
TypeError: 'Namespace' object has no attribute '__getitem__'

我通过输入以下命令运行脚本:

$ python test.py --dataset /images

知道我在这里做错了什么吗?

谢谢。

【问题讨论】:

  • argsargparse.Namespace 对象,而不是字典。你想要一个属性。发送print(args) 以更清楚地了解它是什么。

标签: python image parsing path argparse


【解决方案1】:

这应该可行

imagePath = paths.list_images(args.dataset)

或者如果您出于某种原因需要 dict:

imagePath = paths.list_images(args.__dict__['dataset'])

【讨论】:

  • 感谢您的友好回复。对于第一条语句,我收到以下错误:“文件“test.py”,第 11 行,在 imagePath = paths.list_images(args.dataset) AttributeError: 'dict' object has no attribute 'dataset'"
  • 对于第二个语句,我收到以下错误:“文件“test.py”,第 11 行,在 imagePath = paths.list_images(args.__dict__['dataset']) AttributeError : 'dict' 对象没有属性 'dict' "
  • @Simplicity re: #1 - 这很奇怪,argparse 的版本是什么?并且您确定您没有在其中使用 vars,因为在最初的问题报告中报告的对象类型不是 dict 而是命名空间,re: #2 - 从错误消息看来,您在它的时候输入了 .dict应该是.__dict__
【解决方案2】:

我执行了以下操作以使程序正常运行(通知vars()):

from imutils import paths
import argparse

# create parser
parser = argparse.ArgumentParser()
# define the command-line options
parser.add_argument('--dataset', required=True,help='path to the dataset')
# read the command-line arguments and interpret them
args = vars(parser.parse_args())

print args

imagePath = paths.list_images(args['dataset'])

for image in imagePath:
    print image

【讨论】:

    猜你喜欢
    • 2015-04-09
    • 2013-05-12
    • 2021-11-14
    • 2018-09-03
    • 2020-10-01
    • 2013-03-17
    • 2015-10-26
    • 2014-08-14
    • 1970-01-01
    相关资源
    最近更新 更多