OpenCV人脸检测失败

时间:2015-02-25 03:53:08

标签: python-2.7 opencv

运行以下代码时出现此错误:

OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file ..\..\..\..\opencv\modules\objdetect\src\cascadedetect.cpp, line 1580
Traceback (most recent call last):
  File "C:\Users\Sunny\Documents\NetBeansProjects\Face_Detection\src\face_detection.py", line 12, in <module>
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: ..\..\..\..\opencv\modules\objdetect\src\cascadedetect.cpp:1580: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale

代码是:

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('C:\opencv3\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:\opencv3\opencv\sources\data\haarcascades\haarcascade_eye.xml')

img = cv2.VideoCapture(0)

while(1):
    _,f=img.read()
    gray = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        cv2.rectangle(f,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = f[y:y+h, x:x+w]
        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

    cv2.imshow('img',f)
    if cv2.waitKey(25) == 27:
        break

cv2.destroyAllWindows()
img.release()

我真的不明白发生了什么。我错过了任何目录吗?任何帮助表示赞赏。我甚至不在乎你是否会给我一段新的代码,我只需要一些可以检测到我的脸并在其周围画一个矩形的东西。

0 个答案:

没有答案