【发布时间】: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