错误:必填参数-i /-image

时间:2018-06-20 17:00:22

标签: python-3.x argparse opencv3.0

  

我写了这段代码进行形状检测,但显示错误

# import the necessary packages
import shape_detector
import argparse
import imutils
import cv2
import sys

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i","--image", required=True,help="path to the input 
image",default = "~C:/Users/hp/Desktop/test2.png")
args = vars(ap.parse_args())
# load the image and resize it to a smaller factor so that
# the shapes can be approximated better
image = cv2.imread(args["image"])
resized = imutils.resize(image, width=300)
ratio = image.shape[0] / float(resized.shape[0])

# convert the resized image to grayscale, blur it slightly,
# and threshold it
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]

# find contours in the thresholded image and initialize the
# shape detector
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
sd = ShapeDetector()
# loop over the contours

在这部分之后,我使用了循环以检测图像 但显示此错误

usage: working.py [-h] -i IMAGE
working.py: error: the following arguments are required: -i/--image
  

请告诉我在哪里提及我的图像的路径,我是新手,所以随时告诉我我在哪里犯错

1 个答案:

答案 0 :(得分:0)

如果您不想提供-i参数,请从以下行中删除required=True

ap.add_argument("-i","--image", required=True,help="path to the input 
image",default = "~C:/Users/hp/Desktop/test2.png")

这将导致参数为可选,默认为"~C:/Users/hp/Desktop/test2.png"