-2

I am using a Raspberry Pi 3 Model B, with Raspbian, opencv 2.x and Python 3 installed.

I want to access my USB Webcam and take a picture with it. 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

cascPath = sys.argv[1]

IndexError: list index out of range

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

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Umair Assad
  • 69
  • 1
  • 2
  • 10

3 Answers3

0

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.

What you need to do is provide a CLI parameter which is for cascPath.

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.

Zixian Cai
  • 945
  • 1
  • 10
  • 17
0

You code snippet looks identical to the code written by Shantnu Tiwari. See the webpage 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". This xml-file contains all information that the haarcascade algorithmn in OpenCV needs. 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

Cincy
  • 1
  • 1
0

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. 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. Otherwise, you can compile your code on the python shell thus; python webcam.py haarcascade_frontalface_default.xml.

DavidW
  • 29,336
  • 6
  • 55
  • 86