簡體   English   中英

我在使用Python命令“ cascPath = sys.argv [1]”時遇到問題,出現錯誤IndexError:列表索引超出范圍

[英]Iam having trouble with Python command “ cascPath = sys.argv[1] ” i get error IndexError: list index out of range

我正在使用Raspberry Pi 3 Model B,安裝了Raspbian,opencv 2.x和Python 3。

我想訪問我的USB網絡攝像頭並用它拍照。 我發現了很多代碼,但是沒有任何用處。 我發現一個更好,但是當我運行命令時

cascPath = sys.argv[1]

我得到了錯誤

追溯(最近一次通話):

文件“ /home/pi/test.py”,第4行,在

cascPath = sys.argv[1]

IndexError:列表索引超出范圍

我只需要訪問網絡攝像頭即可拍照。

我正在使用以下代碼:

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()

這段代碼嘗試識別圖像上的面孔,並且sys.argv[1]期望您運行帶有XML文件路徑的腳本來幫助識別面孔。

如果您不想識別人臉,則只需此代碼即可在攝像機的監視器視頻上顯示。

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

#When everything is done, release the capture
video_capture.release()

或此保存圖像

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM