1
import cv2  
import sys

# Get user supplied values
imagePath = sys.argv[1]
cascPath = sys.argv[2]

# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)

# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Detect faces in the image
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30, 30)
    #flags = cv2.CV_HAAR_SCALE_IMAGE
)

print("Found {0} faces!".format(len(faces)))

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

cv2.imshow("Faces found", image)
cv2.waitKey(0)  

I installed OpenCv and found an example code. But surprisingly it didn't work. I get this error message when I run it.

Traceback (most recent call last):
  File "/Users/Esat/Desktop/Python/Softwares/untitled.py", line 5, in <module>
    imagePath = sys.argv[1]
IndexError: list index out of range

What should I do?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
antisycop2
  • 11
  • 1
  • 1
    That error message means that it didn't find a command line parameter it was expecting; the path to the image. How are you invoking the script? You need something like `python3 script.py imagepath cascpath`. – jonrsharpe Apr 22 '17 at 13:21

0 Answers0