gpt4 book ai didi

python - ap.add_argument()的语法

转载 作者:行者123 更新时间:2023-12-02 17:07:45 35 4
gpt4 key购买 nike

我是OpenCV的新手。我正在使用anaconda spyder控制台编写Python代码。我在这里采用了代码形式(https://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/)
我被击中的代码是:

# USAGE
# python transform_example.py --image images/example_01.png --coords "[(73, 239), (356, 117), (475, 265), (187, 443)]"
# python transform_example.py --image images/example_02.png --coords "[(101, 185), (393, 151), (479, 323), (187, 441)]"
# python transform_example.py --image images/example_03.png --coords "[(63, 242), (291, 110), (361, 252), (78, 386)]"

# import the necessary packages
from pyimagesearch.transform import four_point_transform
import numpy as np
import argparse
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help = "path to the image file")
ap.add_argument("-c", "--coords",
help = "comma seperated list of source points")
args = vars(ap.parse_args())

# load the image and grab the source coordinates (i.e. the list of
# of (x, y) points)
# NOTE: using the 'eval' function is bad form, but for this example
# let's just roll with it -- in future posts I'll show you how to
# automatically determine the coordinates without pre-supplying them
image = cv2.imread(args["image"])
pts = np.array(eval(args["coords"]), dtype = "float32")

# apply the four point tranform to obtain a "birds eye view" of
# the image
warped = four_point_transform(image, pts)

# show the original and warped images
cv2.imshow("Original", image)
cv2.imshow("Warped", warped)
cv2.waitKey(0)

在行号13-17可以准确告知正在发生的事情。我知道我需要获取图像,获取纸张坐标(这些行就是为此目的),但是我无法获取图像文件。

最佳答案

在提供的链接中,使用可选参数解析参数,即参数必须以add_argument()中提到的字符串开头。

因此,您有:

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help = "path to the image file")
ap.add_argument("-c", "--coords", help = "comma seperated list of source points")
args = vars(ap.parse_args())

注意 "--image""--coords"。这些是可选参数,在终端中执行代码之前必须提及这些字符串。

因此,在终端上输入以下内容:
python transform_example.py --image images/example_01.png --coords "[(73, 239), (356, 117), (475, 265), (187, 443)]"

有关参数解析及其各种方式的更多详细信息,请参见 THIS PAGE

关于python - ap.add_argument()的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50756306/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com