适用于Mac的Open Cv + Numpy + Python人脸检测

时间:2019-02-25 02:29:47

标签: python python-2.7 opencart2.x

如何在Mac上针对python 2.7进行OpenCV人脸检测。我尝试了许多不同的代码,但是它们都不起作用。我正在终端中运行它们。您能否给我一个网站或代码的链接。

我收到此错误:

cv2.error: OpenCV(4.0.0) /Users/travis/build/skvark/opencv-python/opencv/modules/objdetect/src/cascadedetect.cpp:1658: error: (-215:Assertion failed) !empty() in function 'detectMultiScale'

使用以下代码:

import numpy as np
import cv2
faceCascade = 
cv2.CascadeClassifier('Cascades/haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height
while True:
    ret, img = cap.read()
    img = cv2.flip(img, -1)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(
        gray,     
        scaleFactor=1.2,
        minNeighbors=5,     
        minSize=(20, 20)
    )
    for (x,y,w,h) in faces:
         cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]  
    cv2.imshow('video',img)
    k = cv2.waitKey(30) & 0xff
    if k == 27: # press 'ESC' to quit
        break
cap.release()
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:0)

您的错误消息表明文件路径可能无效,程序找不到 .xml 文件。并且您可能想用诸如'Cascades/haarcascade_frontalface_default.xml'

这样的绝对路径替换'username/file/../cascade.xml'