简体   繁体   English

我在使用Python命令“ cascPath = sys.argv [1]”时遇到问题,出现错误IndexError:列表索引超出范围

[英]Iam having trouble with Python command “ cascPath = sys.argv[1] ” i get error IndexError: list index out of range

I am using a Raspberry Pi 3 Model B, with Raspbian, opencv 2.x and Python 3 installed. 我正在使用Raspberry Pi 3 Model B,安装了Raspbian,opencv 2.x和Python 3。

I want to access my USB Webcam and take a picture with it. 我想访问我的USB网络摄像头并用它拍照。 I've found tons of code but none are of any use. 我发现了很多代码,但是没有任何用处。 I found one which is better but when I run the command 我发现一个更好,但是当我运行命令时

cascPath = sys.argv[1]

I get the error 我得到了错误

Traceback (most recent call last): 追溯(最近一次通话):

File "/home/pi/test.py", line 4, in 文件“ /home/pi/test.py”,第4行,在

cascPath = sys.argv[1]

IndexError: list index out of range IndexError:列表索引超出范围

I simply need to access my webcam to take a picture. 我只需要访问网络摄像头即可拍照。

I am using the following code : 我正在使用以下代码:

import cv2

import sys

cascPath = sys.argv[1]

faceCascade = cv2.CascadeClassifier(cascPath)

video_capture = cv2.VideoCapture(0)

while True:

    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    # Display the resulting frame
    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

#When everything is done, release the capture
video_capture.release()

This code try to recognize faces on image and sys.argv[1] expects you run script with path to XML file which help recognize faces. 这段代码尝试识别图像上的面孔,并且sys.argv[1]期望您运行带有XML文件路径的脚本来帮助识别面孔。

If you don't want to recognize faces then you need only this code to display on monitor video from camera. 如果您不想识别人脸,则只需此代码即可在摄像机的监视器视频上显示。

import cv2

import sys

video_capture = cv2.VideoCapture(0)

while True:

    # Capture frame-by-frame
    ret, frame = video_capture.read()

    # Display the resulting frame
    cv2.imshow('Video', frame)

    # exit if you press key `q`
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

#When everything is done, release the capture
video_capture.release()

Or this to save image 或此保存图像

import cv2

video_capture = cv2.VideoCapture(0)

# Capture frame
ret, frame = video_capture.read()

# Write frame in file
cv2.imwrite('image.jpg', frame)

# When everything is done, release the capture
video_capture.release()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我运行此命令“cascPath = sys.argv[1]”时,我收到错误 IndexError: list index out of range - When i run this command " cascPath = sys.argv[1] " i get error IndexError: list index out of range driver.get(sys.argv[1]) 在 PC 上正常工作,而在其他 PC 上工作正常(sys.argv[1],IndexError: list index out of range 错误) - driver.get(sys.argv[1]) working fine on a PC and not on the other (sys.argv[1], IndexError: list index out of range error) IndexError:列表索引超出范围:sys.argv[1] 超出范围 - IndexError: list index out of range: sys.argv[1] out of range Sys.argv python错误列表索引超出范围 - Sys.argv python error list index out of range sys.argv[1] 索引错误:列表索引超出范围 - sys.argv[1] IndexError: list index out of range 读取 csv 文件错误:sys.argv[1], IndexError: list index out of range - Reading in csv file error: sys.argv[1], IndexError: list index out of range Python GetHostId.py获取sys.argv [1] IndexError:列表索引超出范围 - Python GetHostId.py getting sys.argv[1] IndexError: list index out of range fileku.write(sys.argv[i+3]+'\\n') IndexError:列表索引超出范围 - fileku.write(sys.argv[i+3]+'\n') IndexError: list index out of range Python sys.argv [1]索引超出范围 - Python sys.argv[1] index out of range python sys.argv[1] 列表索引超出范围 - python sys.argv[1] list index out of range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM