gpt4 book ai didi

python - 名称错误 : name 'args' is not defined?

转载 作者:太空宇宙 更新时间:2023-11-03 20:19:46 26 4
gpt4 key购买 nike

我正在尝试 https://www.pyimagesearch.com/2018/02/26/face-detection-with-opencv-and-deep-learning/?__s=zxeickrdq5xagzqfnz7z 中给出的代码

import numpy as np
import argparse
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i", "-C:/datasets/FACE/IMG_0375", required=True,help="path to input image")
ap.add_argument("-p", "-C:/datasets/FACE/deploy.prototxt", required=True,help="path to Caffe 'deploy' prototxt file")
ap.add_argument("-m", "-C:/datasets/FACE/res10_300x300_ssd_iter_140000", required=True,help="path to Caffe pre-trained model")

# load our serialized model from disk
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["-p"], args["-m"])

# load the input image and construct an input blob for the image
# by resizing to a fixed 300x300 pixels and then normalizing it
image = cv2.imread(args["-i"])
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))

# load our serialized model from disk
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["-p"], args["-m"])

# load the input image and construct an input blob for the image
# by resizing to a fixed 300x300 pixels and then normalizing it
image = cv2.imread(args["-i"])
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0,
(300, 300), (104.0, 177.0, 123.0))

以下是显示的错误消息:

[INFO] loading model...

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-9-f999c8fd8971> in <module>
1 # load our serialized model from disk
2 print("[INFO] loading model...")
----> 3 net = cv2.dnn.readNetFromCaffe(args["-p"], args["-m"])
4
5 # load the input image and construct an input blob for the image

NameError: name 'args' is not defined

正如我期望 ap = argparse.ArgumentParser() 允许我使用 args 一样,为什么我会收到此错误?

最佳答案

记住你必须返回解析器。您已经获得了该值,但您必须返回包含这些值的对象:

ap = argparse.ArgumentParser()
ap.add_argument("-i", "-C:/datasets/FACE/IMG_0375", required=True,help="path to input image")
ap.add_argument("-p", "-C:/datasets/FACE/deploy.prototxt", required=True,help="path to Caffe 'deploy' prototxt file")
ap.add_argument("-m", "-C:/datasets/FACE/res10_300x300_ssd_iter_140000", required=True,help="path to Caffe pre-trained model")

#This line here :)
args = ap.parse_args()

关于python - 名称错误 : name 'args' is not defined?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58235773/

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