Python错误:参数-c / - conf是必需的

时间:2016-02-29 05:09:45

标签: json python-2.7 opencv camera argparse

我是python的新手,我的母语是C.我正在使用OpenCV在动作触发的监视系统中使用python编写代码。我的代码基于Adrian Rosebrock在其博客 pyimagesearch.com中制作的代码。最初的代码是为Raspiberry Pi开发的,附带了Pi Camera模块,现在我正在努力适应我的笔记本电脑的网络摄像头。他为一个简单的动作检测代码做了一个更简单的教程,它在我的电脑上运行得非常好。但是我对其他代码感到很难过。可能这是一个愚蠢的错误,但作为开始,我无法找到这个问题的具体答案。


此图像包含导致错误的代码部分(第15行)以及屏幕左侧的项目结构。
Image of python project for surveillance


类似的部分,原始代码:

# import the necessary packages
from pyimagesearch.tempimage import TempImage
from dropbox.client import DropboxOAuth2FlowNoRedirect
from dropbox.client import DropboxClient
from picamera.array import PiRGBArray
from picamera import PiCamera
import argparse
import warnings
import datetime
import imutils
import json
import time
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-c", "--conf", required=True,
    help="path to the JSON configuration file")
args = vars(ap.parse_args())

# filter warnings, load the configuration and initialize the Dropbox
# client
warnings.filterwarnings("ignore")
conf = json.load(open(args["conf"]))
client = None

到现在为止我只改变了这些东西:

  • 排除pi相机的导入亲属。
  • camera = PiCamera()更改camera = cv2.VideoCapture(0)。这样我就可以使用笔记本电脑的网络摄像头。
  • 排除:

    camera.resolution = tuple(conf["resolution"])
    camera.framerate = conf["fps"]
    rawCapture = PiRGBArray(camera, size=tuple(conf["resolution"]))
    
  • for f in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):替换while True:行。
  • 排除rawCapture.truncate(0)程序中的两行。


可能还有更多需要修理的东西,如果你现在请告诉我,但首先我想了解如何解决这个错误。我在Windows 7中使用PyCharm与Python 2.7和OpenCV 3.1。很抱歉没有发布整个代码,但一旦这是我在网站上的第一个问题,我有0声誉,显然我可以发布2个链接。整个原始代码在pyimagesearch.com中。谢谢你的时间!

2 个答案:

答案 0 :(得分:1)

我认为你可能没有正确运行它。错误消息很明确。您正在添加参数,这意味着您需要在运行时提供它们,而不是您正在进行的操作。

在你提供的教程链接中查看他如何运行

http://www.pyimagesearch.com/2015/06/01/home-surveillance-and-motion-detection-with-the-raspberry-pi-python-and-opencv#crayon-56d3c5551ac59089479643

答案 1 :(得分:0)

请注意@ Rhoit链接中的图6屏幕截图。

python pi_surveillance.py --conf conf.json

使用名称和这些--conf conf.json字词初始化程序。

在您的代码中:

ap = argparse.ArgumentParser()
ap.add_argument("-c", "--conf", required=True,
    help="path to the JSON configuration file")

ap是一段代码,它从命令行读取这些输入,并解析信息。该定义指定需要--conf参数,如图6所示。

错误表示您省略了此信息:

argument -c/--conf is required