【发布时间】:2021-07-12 04:14:06
【问题描述】:
我对 argparse 的含义不是很了解。当我用这段代码测试时,它总是这个错误:
usage: test1.py [-h] -i IMAGE
test1.py: error: the following arguments are required: -i/--image
这是我的代码:
# (1) import the necessary packages
import argparse
import imutils
import cv2
# (2) construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to the input image")
args = vars(ap.parse_args())
# (3) load the image, convert it to grayscale, blur it slightly,
# and threshold it
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imwrite('C:\\Users\\Hoang Cao Chuyen\\Downloads\\shapes.png', gray)
blurred = cv2.GaussianBlur(gray, (5,5), 0)
cv2.imwrite('C:\\Users\\Hoang Cao Chuyen\\Downloads\\shapes.png', blurred)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
cv2.imwrite('C:\\Users\\Hoang Cao Chuyen\\Downloads\\shapes.png', thresh)
# find contours in the thresholded image
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
【问题讨论】:
-
你在命令行中输入了什么?当你设置
required=True时,你需要像test1.py -i image这样输入,test1.py image会报错。 -
你是如何测试这个的?
image的值是多少,您是如何提供的? -
使用
pycharm,您必须在一些额外的窗口中指定命令行参数。您不能只按run按钮。我不知道为什么许多新用户会错过这个。 IDE 文档没有解释如何使用参数运行脚本吗? -
谢谢你们。你这么好心回答我。正如我首先所说,我没有理解argparse的极端含义。我阅读了一些文档但仍然无法。
-
path 的输入是我的 C:/ 路径中的图像,但是'-image','-i',我不知道它来自哪里?