import os.path
import cv2
def faceDetect(src):
pass
def main():
if not os.path.isfile('haarcascade_frontalface_default.xml'):
raise RuntimeError("%s: not found" % 'haarcascade_frontalface_default.xml')
face_xml = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_xml = cv2.CascadeClassifier("haarcascade_eye.xml")
img = cv2.imread("image0.jpg", 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_xml.detectMultiScale(gray, 1.3, 5)
print("faces %d" %(len(faces)))
if __name__ == "__main__":
main()
编写人脸识别Demo,从github上面下载这两个文件haarcascade_eye.xml, haarcascade_frontalface_default.xml
编译不过,一直倒腾,怀疑是环境问题。后面使用安装Opencv路径下的这两个文件就没有问题了
File "C:/Users/miss.j/Desktop/Python/34.haar+adboost人脸识别/人脸识别.py", line 19, in main
eye_xml = cv2.CascadeClassifier("haarcascade_eye.xml")
SystemError:
使用安装opencv绝对路径下的xml:
face_xml = cv2.CascadeClassifier('C:\\Users\\miss.j\\AppData\\Local\\Programs\\Python\\Python37-32\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml')
eye_xml = cv2.CascadeClassifier("C:\\Users\\miss.j\\AppData\\Local\\Programs\\Python\\Python37-32\\Lib\\site-packages\\cv2\\data\\haarcascade_eye.xml")