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

问题描述:

我正在使用opencv2.4在线提供的向导,向您展示如何使用opencv2和python检测面部。我遵循指南并理解它所说的内容。然而,我似乎无法找到我的节目的问题,因为视频显示,但现在脸部被检测到,视频非常清晰。没有错误。我在调试模式下运行,值面仍然是一个空白元组,所以我假设这意味着它没有找到面。我不明白的是为什么,我认为它与散列表有关。Python Opencv2 +摄像头面部检测,没有检测到面部没有错误

哈希表我的意思是级联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() 

你有一个错误的道路,以你的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__))) 

现在应该正常工作。