使用ArgParse,获取KeyError

发布于 2025-01-23 04:59:15 字数 910 浏览 1 评论 0原文

import argparse
import imutils
import cv2 as cv

ap = argparse.ArgumentParser()

ap.add_argument("-i", "--input image", required=True, help='Input the Image')
ap.add_argument("-o", "--output image", required=True, help='Output the Image')

args = vars(ap.parse_args())

img = cv.imread(args["input"])

gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
gaussian = cv.GaussianBlur(gray, (5, 5), 0)

threshold = cv.threshold(gaussian, 60, 255, cv.THRESH_BINARY)[1]

contr = cv.findContours(threshold.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
contr = imutils.grab_contours(contr)

for c in contr:
    cv.drawContours(img, [c], -1, (0, 0, 255), 2)

txt = 'Yes I Found {} the Shapes in Image'.format(len(contr))
textPut = cv.putText(img, txt, (10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)

cv.imshow(args["output"], img)
import argparse
import imutils
import cv2 as cv

ap = argparse.ArgumentParser()

ap.add_argument("-i", "--input image", required=True, help='Input the Image')
ap.add_argument("-o", "--output image", required=True, help='Output the Image')

args = vars(ap.parse_args())

img = cv.imread(args["input"])

gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
gaussian = cv.GaussianBlur(gray, (5, 5), 0)

threshold = cv.threshold(gaussian, 60, 255, cv.THRESH_BINARY)[1]

contr = cv.findContours(threshold.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
contr = imutils.grab_contours(contr)

for c in contr:
    cv.drawContours(img, [c], -1, (0, 0, 255), 2)

txt = 'Yes I Found {} the Shapes in Image'.format(len(contr))
textPut = cv.putText(img, txt, (10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)

cv.imshow(args["output"], img)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

装纯掩盖桑 2025-01-30 04:59:15

让我们将代码减少到MWVE:

#test.py
import argparse

ap = argparse.ArgumentParser()

ap.add_argument("-i", "--input image", required=True, help='Input the Image')
ap.add_argument("-o", "--output image", required=True, help='Output the Image')

args = vars(ap.parse_args())
print(args)

使用此问题运行的

python test.py -i input.jpg -o output.jpg

{'input image': 'input.jpg', 'output image': 'output.jpg'}

,您滥用了第二个参数,不是flag +描述,而是只有标志,然后将其用作密钥。您应该这样做:

ap.add_argument("-i", "--input", required=True, help='Input the Image')
ap.add_argument("-o", "--output", required=True, help='Output the Image')

在上述代码中替换python test.py -i input.jpg -o output.jpg现在给出:

{'input': 'input.jpg', 'output': 'output.jpg'}

您还可以设置在args明确,例如:

ap.add_argument("-i", "--input", required=True, help='Input the Image', dest="in")
ap.add_argument("-o", "--output", required=True, help='Output the Image', dest="out")

它将给出

{'in': 'input.jpg', 'out': 'output.jpg'}

Let's reduce your code to a MWVE:

#test.py
import argparse

ap = argparse.ArgumentParser()

ap.add_argument("-i", "--input image", required=True, help='Input the Image')
ap.add_argument("-o", "--output image", required=True, help='Output the Image')

args = vars(ap.parse_args())
print(args)

running this with

python test.py -i input.jpg -o output.jpg

gives

{'input image': 'input.jpg', 'output image': 'output.jpg'}

The issue is that you misused the second argument, it is not a flag + description, but only the flag, which is then used as the key. You should do it like this:

ap.add_argument("-i", "--input", required=True, help='Input the Image')
ap.add_argument("-o", "--output", required=True, help='Output the Image')

Replacing that in above code and running with python test.py -i input.jpg -o output.jpg gives now:

{'input': 'input.jpg', 'output': 'output.jpg'}

You can also set the name of the field used in args explicitly, e.g.:

ap.add_argument("-i", "--input", required=True, help='Input the Image', dest="in")
ap.add_argument("-o", "--output", required=True, help='Output the Image', dest="out")

which will give

{'in': 'input.jpg', 'out': 'output.jpg'}
浅浅淡淡 2025-01-30 04:59:15

如果不存在参数,则只能获得keyError

当我收到此错误时,我有信心我拥有键/参数,因此我继续一次又一次地运行代码,而无需查看代码并继续重复获得KeyError。我检查了我的代码,事实证明我以某种方式删除了该特定的参数错误。

You'd only get keyError if the argument doesn't exist.

When I received this error, I was confident that I had the key/argument so I kept on running my code again and again without reviewing the code and kept on getting keyError repeatedly. I checked my code and it turns out that I had somehow removed that particular argument mistakenly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文