IndexError: list index out of range: sys.argv[1] out of range
IndexError: list index out of range: sys.argv[1] out of range
我正在尝试 运行 以下代码,但 运行ning 一直出错,
"IndexError: list index out of range"
我知道这与 sys.argv[1] 无关;我有一个基本的想法,这个函数是从命令行调用我的项目的第一行 - 当我尝试从 CMD 运行 项目时,我 运行 进入同样的错误。
代码:
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()
错误:
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
如有任何见解或建议,我们将不胜感激!
sys.argv[1]
此代码需要第一个命令行参数。如果缺少它,那么您将遇到此错误。
示例:
some_script.py
import sys
sys.argv[1]
如果您不提供 myfirst_agr
(如下所示),那么它会抛出您所看到的索引错误。
some_script.py myfirst_agr
更多例子是here
我正在尝试 运行 以下代码,但 运行ning 一直出错,
"IndexError: list index out of range"
我知道这与 sys.argv[1] 无关;我有一个基本的想法,这个函数是从命令行调用我的项目的第一行 - 当我尝试从 CMD 运行 项目时,我 运行 进入同样的错误。
代码:
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()
错误:
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
如有任何见解或建议,我们将不胜感激!
sys.argv[1]
此代码需要第一个命令行参数。如果缺少它,那么您将遇到此错误。
示例:
some_script.py
import sys
sys.argv[1]
如果您不提供 myfirst_agr
(如下所示),那么它会抛出您所看到的索引错误。
some_script.py myfirst_agr
更多例子是here