[英]When i run this 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.我只需要访问我的网络摄像头即可拍照。
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()
cv2.destroyAllWindows()
This is the code这是代码
sys.argv[1]
is the first CLI parameter and if you don't provide any CLI parameter, len(sys.argv)=1
so you can only access sys.argv[0] which is the script name. sys.argv[1]
是第一个 CLI 参数,如果您不提供任何 CLI 参数,则len(sys.argv)=1
因此您只能访问 sys.argv[0],即脚本名称。
What you need to do is provide a CLI parameter which is for cascPath
.您需要做的是提供一个用于
cascPath
的 CLI 参数。
The Cascade is a xml file contains OpenCV data to detect objects which might comes with the tutorial you read / the code snippet you downloaded. Cascade 是一个包含 OpenCV 数据的 xml 文件,用于检测您阅读的教程/下载的代码片段中可能附带的对象。
You code snippet looks identical to the code written by Shantnu Tiwari.您的代码片段看起来与 Shantnu Tiwari 编写的代码相同。 See the webpagehttps://realpython.com/face-detection-in-python-using-a-webcam/ .
请参阅网页https://realpython.com/face-detection-in-python-using-a-webcam/ 。 Here he analyzes the code and has a video clip showing the face-detection-code in action.
在这里,他分析了代码,并有一个视频剪辑显示了面部检测代码的运行情况。
Goto Shantus's GitHub site https://github.com/shantnu/Webcam-Face-Detect to download the python-code and the file "haarcascade_frontalface_default.xml" .
转到 Shantus 的 GitHub 站点https://github.com/shantnu/Webcam-Face-Detect下载 python 代码和文件“haarcascade_frontalface_default.xml” 。 This xml-file contains all information that the haarcascade algorithmn in OpenCV needs.
这个 xml 文件包含 OpenCV 中的 haarcascade 算法所需的所有信息。 Run:
跑:
python webcam.py haarcascade_frontalface_default.xml
You'll find more xml files for various detections (eyes, full body, ...) on the OpenCV GitHub site: https://github.com/opencv/opencv/tree/master/data/haarcascades您可以在 OpenCV GitHub 站点上找到更多用于各种检测(眼睛、全身等)的 xml 文件: https : //github.com/opencv/opencv/tree/master/data/haarcascades
Had that same error.有同样的错误。 Reasons for the error have been explained here already.
这里已经解释了错误的原因。 You just need to provide the path to the
"haarcascade_frontalface_default.xml"
file you downloaded.您只需要提供您下载的
"haarcascade_frontalface_default.xml"
文件的路径。 That is, instead of cascPath = sys.argv[1]
, use: cascPath = haarcascade_frontalface_default.xml
, if your python and xml files are in the same directory.也就是说,如果您的 python 和 xml 文件在同一目录中,请使用:
cascPath = haarcascade_frontalface_default.xml
cascPath = sys.argv[1]
而不是cascPath = sys.argv[1]
。 Otherwise, you can compile your code on the python shell thus;否则,您可以在 python shell 上编译您的代码;
python webcam.py haarcascade_frontalface_default.xml.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.