我正在尝试返回图像路径,如下所示:
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
知道我在这里做错了吗?
感谢。
答案 0 :(得分:1)
这应该有效
imagePath = paths.list_images(args.dataset)
或者,如果你出于某种原因需要词典:
imagePath = paths.list_images(args.__dict__['dataset'])
答案 1 :(得分:0)
我做了以下工作以使程序正常工作(注意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