2017-12-30 17 views
0

나는 이미지 경로를 반환하기 위해 노력하고있어이 없습니다 :목록 이미지 경로 - 다음과 같이 '네임 스페이스'객체는 어떤 속성 '__getitem__'

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__' 
다음 오류가 발생,

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 

을하지만, 내가 잘못 여기

$ python test.py --dataset /images 

어떤 생각을하고 있어요 무엇 :

나는 다음과 같은 명령을 입력하여 스크립트를 실행?

감사합니다.

+1

'args'는 사전이 아닌'argparse.Namespace' 객체입니다. 속성이 필요합니다. 그것이 무엇인지 명확하게 알기 위해서는'print (args)'를해라. – hpaulj

답변

0

imagePath = paths.list_images(args.dataset) 

작동합니다 또는 당신이 필요로하는 경우 어떤 이유로 DICT :

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

친절한 답변을 보내 주셔서 감사합니다. 첫 번째 문에 대해 다음과 같은 오류가 발생합니다. "File"test.py ", 11 행, imagePath = paths.list_images (args.dataset) AttributeError : 'dict'객체에 'dataset'속성이 없습니다." – Simplicity

+0

"파일"test.py ", 줄 11, imagePath = paths.list_images (args .__ dict __ [ 'dataset']) AttributeError : 'dict'개체에 특성이 없습니다. '__dict__' " – Simplicity

+0

@ Simplicity re : # 1 - 그게 이상 하네, argparse의 버전이 뭐니? 그리고 당신은 거기에 vars를 사용하지 않는다고 확신한다. 초기 문제를 일으킨다.보고 된 객체의 타입은 dict가 아니라 네임 스페이스이다 : # 2 - 에러 메시지에서'.dict'를 넣었다. while while '.__ dict__'이어야합니다. – Ivan

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 
관련 문제