2016-10-08 11 views
-1

Raspbian, opencv 2.x 및 Python 3이 설치된 Raspberry Pi 3 모델 B를 사용하고 있습니다. .파이썬 명령에 문제가있는 Iam "cascPath = sys.argv [1]"오류가 발생합니다. IndexError : 목록 색인이 범위를 벗어납니다.

내 USB 웹캠에 액세스하여 사진을 찍고 싶습니다. 나는 엄청난 양의 코드를 발견했지만 아무도 사용하지 않습니다. 나는 더 나은 하나를 발견하지만 난 명령

cascPath = sys.argv[1] 

을 실행할 때 나는

Traceback (most recent call last):

File "/home/pi/test.py", line 4, in

cascPath = sys.argv[1]

IndexError: list index out of range

가 단순히 사진을 촬영 웹캠에 액세스하는 데 필요한 오류가 발생합니다.

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

'sys.argv에 [1]'기대는 인수와 함께 당신에게 스크립트를 실행 저장 -'파이썬 test.py의 some_argument'. 그러나이 인자는'cascPath = some_argument' 코드에서 직접 사용할 수 있습니다. 나는 얼굴을 인식하기 위해 XML 파일의 경로 여야한다고 생각한다. – furas

답변

2

이 코드 시도가 이미지의 얼굴을 인식하고 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() 
+0

WOW Furas 정말 도움이되었습니다. –

+0

내 디렉토리의 어딘가에 그림을 저장하고 싶다면이 코드에 약간의 옵션을 추가 할 수 있습니까? –

+0

'cv2.imwrite ('some_directory/image.jpg', 프레임)' – furas