【问题标题】:ValueError :must start with a character '-'ValueError : 必须以字符 '-' 开头
【发布时间】:2019-06-29 02:19:32
【问题描述】:
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 '-'
>>>

【问题讨论】:

  • add_argument 的位置参数是在命令行上给出的参数名称,就像你对 "--confidence" 的正确名称一样。您似乎正在尝试添加参数值,这是对 argparse 工作原理的误解。有关如何使用此库,请参阅 argparse
  • 我已经编辑了代码并删除了 image、prototxt 和 model 的参数值,但现在我收到了这个错误。“用法:detectfaces.py [-h] -i IMAGE -p PROTOTXT - m MODEL [-c CONFIDENCE] detectfaces.py: 错误:需要以下参数:-i/--image, -p/--prototxt, -m/--model"
  • 您的参数解析器描述了您的程序需要哪些参数。现在,当您运行程序时,您必须指定这些参数。这是argparse 的重点:从命令行解析参数。我建议你阅读the documentation

标签: python python-3.x argparse face-detection


【解决方案1】:

试试这个:

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())

【讨论】:

  • 用法:detectfaces.py [-h] -i INPUT_IMAGE -p DEPLOY_FILE_PATH -m MODEL [-c CONFIDENCE] detectfaces.py:错误:需要以下参数:-i/--input_image, -p/--deploy_file_path, -m/--model
  • 您通过添加 required=True 使选项成为强制性的,因此您需要传递那些互补的参数..
【解决方案2】:

您提供的路径(例如"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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-31
    • 2021-02-09
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多