2017-05-07 64 views
0

我試圖從github運行一個代碼,它使用Python對圖像進行分類,但是我收到一個錯誤消息。Python錯誤:其中一個參數是必需的

這裏是代碼:

import argparse as ap 
import cv2 
import imutils 
import numpy as np 
import os 
from sklearn.svm import LinearSVC 
from sklearn.externals import joblib 
from scipy.cluster.vq import * 



# Get the path of the testing set 
parser = ap.ArgumentParser() 
group = parser.add_mutually_exclusive_group(required=True) 
group.add_argument("-t", "--testingSet", help="Path to testing Set") 
group.add_argument("-i", "--image", help="Path to image") 
parser.add_argument('-v',"--visualize", action='store_true') 
args = vars(parser.parse_args()) 

# Get the path of the testing image(s) and store them in a list 
image_paths = [] 
if args["testingSet"]: 
    test_path = args["testingSet"] 
    try: 
     testing_names = os.listdir(test_path) 
    except OSError: 
     print "No such directory {}\nCheck if the file exists".format(test_path) 
     exit() 
for testing_name in testing_names: 
     dir = os.path.join(test_path, testing_name) 
     class_path = imutils.imlist(dir) 
     image_paths+=class_path 
    else: 
     image_paths = [args["image"]] 

,這是我得到

usage: getClass.py [-h] 
       (- C:/Users/Lenovo/Downloads/iris/bag-of-words-master/dataset/test TESTINGSET | - C:/Users/Lenovo/Downloads/iris/bag-of-words-master/dataset/test/test_1.jpg IMAGE) 
       [- C:/Users/Lenovo/Downloads/iris/bag-of-words-master/dataset] 
getClass.py: error: one of the arguments - C:/Users/Lenovo/Downloads/iris/bag-of-words-master/dataset/test/--testingSet - C:/Users/Lenovo/Downloads/iris/bag-of-words-master/dataset/test/test_1.jpg/--image is required 

可以請你幫我這個錯誤消息?在哪裏以及如何寫文件路徑?

+0

你試過的確切命令是什麼? –

回答

0

這是您自己的程序發佈時發生的錯誤。該消息不是關於文件路徑,而是關於參數的數量。這條線

group = parser.add_mutually_exclusive_group(required=True) 

說,只有你的命令行參數(-t-i)一個是允許的。但從錯誤消息看來,您在命令行上提供了--testingSet--image。因爲你只有3個參數,我不得不懷疑你是否真的需要參數組。

要使命令行工作,請刪除互斥組並將參數直接添加到解析器。

parser.add_argument("-t", "--testingSet", help="Path to testing Set") 
parser.add_argument("-i", "--image", help="Path to image") 
parser.add_argument('-v',"--visualize", action='store_true') 
相關問題