使用单帧的Opencv维度在python中进行网络摄像头人脸检测

时间:2016-03-31 13:07:41

标签: python opencv face-detection

我一直在尝试使用来自realpython.com的代码以及我在此网站上看到的建议,从我的网络摄像头运行人脸检测功能。

import cv2
import sys
import os

cascPath = "{base_path}/folder_with_your_xml/haarcascade_frontalface_default.xml".format(
    base_path=os.path.abspath(os.path.dirname(__file__)))

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

这是我运行代码时遇到的错误:

File "videocam.py", line 16, in <module>
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function cvtColor

从我收集到的内容来看,因为frame不是三维的,而是一个NoneType对象。我只是刚开始使用OpenCV和面部识别,所以我不完全确定如何解决这个问题。

0 个答案:

没有答案