Iam having trouble with Python command â cascPath = sys.argv[1] â i get error IndexError: list index out of range
<p>I am using a Raspberry Pi 3 Model B, with Raspbian, opencv 2.x and Python 3 installed.</p>

<p>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</p>

<pre><code>cascPath = sys.argv[1]
</code></pre>

<p>I get the error</p>

<blockquote>
  <p>Traceback (most recent call last):</p>
  
  <p>File "/home/pi/test.py", line 4, in</p>
  
  <p><code>cascPath = sys.argv[1]</code></p>
</blockquote>

<p>IndexError: list index out of range</p>

<p>I simply need to access my webcam to take a picture.</p>

<p>Iam using the following code : </p>

<pre><code>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) &amp; 0xFF == ord('q'):
        break

#When everything is done, release the capture
video_capture.release()
</code></pre>

<p>If somebody knows an exact python code for taking picture it would be best too, I have tried many different ways but all in vain. <strong>PLEASE RESPOND ASAP i quote again <em>I simply want to take Picture using my "webcam"</em></strong> </p>

<p>Regards</p>

<p>This code try to recognize faces on image and <code>sys.argv[1]</code> expects you run script with path to XML file which help recognize faces.</p>

<p>If you don't want to recognize faces then you need only this code to display on monitor video from camera.</p>

<pre><code>import cv2

import sys

video_capture = cv2.VideoCapture(0)

while True:

    # Capture frame-by-frame
    ret, frame = video_capture.read()

    # Display the resulting frame
    cv2.imshow('Video', frame)

    # exit if you press key `q`
    if cv2.waitKey(1) &amp; 0xFF == ord('q'):
        break

#When everything is done, release the capture
video_capture.release()
</code></pre>

<p>Or this to save image </p>

<pre><code>import cv2

video_capture = cv2.VideoCapture(0)

# Capture frame
ret, frame = video_capture.read()

# Write frame in file
cv2.imwrite('image.jpg', frame)

# When everything is done, release the capture
video_capture.release()
</code></pre>

python opencv debian webcam raspberry-pi3