简体   繁体   中英

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"

I understand that this is todo with the 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.

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. If that's missing then you will encounter this error. Example:

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.

some_script.py myfirst_agr

More examples are here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM