在Python 3.6中打开HAAR Cascade

时间:2017-11-01 12:19:31

标签: python opencv3.0

我正在尝试使用OpenCV在python中构建面部识别软件。我正在使用HAAR级联,但每次运行程序时都会出现回溯错误。我想知道我是不是因为没有输入正确的文件位置而搞砸了,或者我只是在代码中的其他地方做错了。对于文件位置,我试过

detectFace = cv2.CascadeClassifier('Desktop/Fall 17/Intelligent System/Final Project/Facial Recognition/haarcascade_frontalface_default.xml') 

detectFace = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') 

我的代码:

import cv2
import sys
import numpy as np

cam = cv2.VideoCapture(0)

detectFace = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')


Id= input('enter user id')
sampleNum=0;

while(True):
    ret, img = cam.read();
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces = detectFace.detectMultiScale(gray,1,3,5);
    for(x,y,w,h) in faces:


        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

        sampleNum = sampleNum+1;


        cv2.imwrite("dataSet/User."+Id+"."
        +str(sampleNum)+".jpg",gray[y:y+h,x:x+w])



    cv2.imshow('faces',img);
    if cv2.waitKey(100) & 0xFF == ord('q'):
        break



    elif sampleNum>20:
        break
cam.realease()
cv2.destroyAllWindows

这是错误。

Traceback (most recent call last):
File "C:/Users/claws/Desktop/Fall 17/Intelligent Systems/Final 
Project/Facial Recognition/dataSet_Creator.py", line 16, in <module>
faces = detectFace.detectMultiScale(gray,1,3,5);
cv2.error: C:\projects\opencv-
python\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215) 
!empty() in function cv::CascadeClassifier::detectMultiScale

0 个答案:

没有答案