[英]IndexError: list index out of range: sys.argv[1] out of range
I'm trying to run the following code but keep running into an error,我正在尝试运行以下代码,但一直遇到错误,
"IndexError: list index out of range" “IndexError:列表索引超出范围”
I understand that this is todo with the sys.argv[1];我知道这与 sys.argv[1]; I have a basic idea that this function is call the first line of my project from the command line - when I try to run the project from CMD I run into the same error.
我有一个基本想法,即这个 function 是从命令行调用我的项目的第一行 - 当我尝试从 CMD 运行项目时,我遇到了同样的错误。
The code:编码:
import cv2
import sys
cascadePath = sys.argv[1]
faceCascade = cv2.CascadeClassifier(cascadePath)
video_capture = cv2.VideoCapture(0)
while True:
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.CASCADE_SCALE_IMAGE
)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
Error:错误:
Traceback (most recent call last):
File "C:/Users/Niku/PycharmProjects/FaceDetection/FaceDetection.py", line 4, in <module>
cascadePath = sys.argv[1]
IndexError: list index out of range
Any insight or advice would be greatly appreciated!任何见解或建议将不胜感激!
sys.argv[1]
This code expects first command line argument. sys.argv[1]
此代码需要第一个命令行参数。 If that's missing then you will encounter this error.如果缺少,那么您将遇到此错误。 Example:
例子:
some_script.py some_script.py
import sys
sys.argv[1]
If you dont provide myfirst_agr
(as shown below) then it will throw Index error what you are seeing.如果您不提供
myfirst_agr
(如下所示),那么它将抛出您所看到的索引错误。
some_script.py myfirst_agr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.