Python Opencv2 +网络摄像头面部检测,没有检测到面部没有错误

时间:2015-10-19 23:20:47

标签: python opencv image-processing face-recognition

我使用opencv2.4在线提供的指南,向您展示如何使用opencv2和python检测面部。我按照指南说明了它的内容。但是,我似乎无法找到我的程序的问题,因为视频显示但现在检测到脸部,视频非常清晰。没有错误。我在调试模式下运行,值面仍然是一个空元组,所以我假设这意味着它没有找到面。我不明白的是为什么我认为它与哈希表有关。

通过哈希表我的意思是级联xml文件。我理解级联基本上是检测面部伪影的准则吗?

指南的链接。哈希表,即xml文件位于链接的github上。

https://github.com/shantnu/FaceDetect/blob/master/haarcascade_frontalface_default.xml https://realpython.com/blog/python/face-detection-in-python-using-a-webcam/

import cv2
import sys
import os
#cascPath = sys.argv[1]
cascPath = os.getcwd()+'facehash.xml'
faceCascade = cv2.CascadeClassifier(cascPath)
print faceCascade
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
            )
    cv2.cv
    # 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()

1 个答案:

答案 0 :(得分:1)

您的xml分类器路径错误。 (我想你已经更改了名称以获得更短的形式)。

而不是您的cascPath:

cascPath = os.getcwd()+'facehash.xml'

试试这个:

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

现在它应该也可以。

相关问题