ValueError异常:必须以字符开始“ - ”

问题描述 投票:-2回答:2
import numpy as np
import argparse
import cv2
ap=argparse.ArgumentParser()
ap.add_argument("-i","D:\python learning\IMG_20130614_000526.jpg",required=True,help="path to input image")
ap.add_argument("-p","D:\python learning\deep-learning-face-detection\deploy.prototxt.txt",required=True,help="path to Caffe 'deploy' prototxt file")
ap.add_argument("-m","D:\python learning\deep-learning-face-detection\res10_300x300_ssd_iter_140000.caffemodel",required=True,help="path to Caffe pretrained model")
ap.add_argument("-c", "--confidence",type=float,default=0.5,help="minimum probability to filter weak detections")
args=vars(ap.parse_args())

错误:

Traceback (most recent call last):
  File "D:\python learning\detectfaces.py", line 6, in <module>
    ap.add_argument("-p","D:\python learning\deep-learning-face-detection\deploy.prototxt.txt",required=True,help="path to Caffe 'deploy' prototxt file")
  File "C:\Users\RAJKUMAR\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 1320, in add_argument
    kwargs = self._get_optional_kwargs(*args, **kwargs)
  File "C:\Users\RAJKUMAR\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 1451, in _get_optional_kwargs
    raise ValueError(msg % args)
ValueError: invalid option string 'D:\\python learning\\deep-learning-face-detection\\deploy.prototxt.txt': must start with a character '-'
>>>
python python-3.x argparse face-detection
2个回答
0
投票

尝试这个:

import numpy as np
import argparse
import cv2
ap=argparse.ArgumentParser()
ap.add_argument("-i","--input_image",required=True,help="path to input image")
ap.add_argument("-p","--deploy_file_path",required=True,help="path to Caffe 'deploy' prototxt file")
ap.add_argument("-m","--model",required=True,help="path to Caffe pretrained model")
ap.add_argument("-c", "--confidence",type=float,default=0.5,help="minimum probability to filter weak detections")
args=vars(ap.parse_args())

0
投票

看来你是提供(例如"D:\python learning\IMG_20130614_000526.jpg")的路径,旨在为参数-i-p--m默认值。如果这是你正在尝试做的,它们指定为默认值。你的代码是指定它们作为参数名称(如--confidence)这就是为什么argparse告诉你,他们必须以连字符开头。

例如:

ap.add_argument("-i", "--input_image", required=True, help="path to input image", default=r"D:\python learning\IMG_20130614_000526.jpg")
© www.soinside.com 2019 - 2024. All rights reserved.