我试图使用 Python、OpenCv2 和 LBPH 实现人脸识别(从HERE下载)
我的 python 版本是 2.7.14
PIP 版本是 9.0.3
而 OpenCV 版本是 3.4.0
我的代码是
import cv2
import numpy as np
import NameFind
# --- import the Haar cascades for face and eye ditection
face_cascade = cv2.CascadeClassifier('Haar/haarcascade_frontalcatface.xml')
eye_cascade = cv2.CascadeClassifier('Haar/haarcascade_eye.xml')
spec_cascade = cv2.CascadeClassifier('Haar/haarcascade_eye_tree_eyeglasses.xml')
help(cv2.face)
# FACE RECOGNISER OBJECT
LBPH = cv2.face.LBPHFaceRecognizer_create(2, 2, 7, 7, 20)
EIGEN = cv2.face.createEigenFaceRecognizer(10, 5000)
FISHER = cv2.face.createFisherFaceRecognizer(5, 500)
# Load the training data from the trainer to recognise the faces
LBPH.load("Recogniser/trainingDataLBPH.xml")
EIGEN.load("Recogniser/trainingDataEigan.xml")
FISHER.load("Recogniser/trainingDataFisher.xml")
# ------------------------------------ PHOTO INPUT -----------------------------------------------------
img = cv2.imread('Me4.jpg') # ------->>> THE ADDRESS TO THE PHOTO
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Convert the Camera to gray
faces = face_cascade.detectMultiScale(gray, 1.3, 4) # Detect the faces and store the positions
print(faces)
for (x, y, w, h) in faces: # Frames LOCATION X, Y WIDTH, HEIGHT
Face = cv2.resize((gray[y: y+h, x: x+w]), (110, 110)) # The Face is isolated and cropped
ID, conf = LBPH.predict(Face) # LBPH RECOGNITION
print ID
NAME = NameFind.ID2Name(ID, conf)
NameFind.DispID(x, y, w, h, NAME, gray)
ID, conf = EIGEN.predict(Face) # EIGEN FACE RECOGNITION
NAME = NameFind.ID2Name(ID, conf)
NameFind.DispID3(x, y, w, h, NAME, gray)
ID, conf = FISHER.predict(Face) # FISHER FACE RECOGNITION
NAME = NameFind.ID2Name(ID, conf)
NameFind.DispID2(x, y, w, h, NAME, gray)
cv2.imshow('LBPH Face Recognition System', gray) # IMAGE DISPLAY
cv2.waitKey(0)
cv2.destroyAllWindows()
当我运行Recogniser_Image_All_Algorithms.py等任何人脸识别文件时出现此错误
回溯(最后一次调用):文件“Recogniser_Image_All_Algorithms.py”,第 11 行,在 LBPH = cv2.face.LBPHFaceRecognizer_create(2, 2, 7, 7, 20) AttributeError: 'module' object has no attribute 'face'
我用谷歌搜索了错误并找到了相同的答案,例如THIS ONE
检查这些提要后,我尝试使用python -m pip install opencv-contrib-python 安装 opencv-contrib
它说
已满足要求:c:\users\rak\anaconda3\lib\site-packages 中的 opencv-contrib-python 已满足要求:c:\users\rak\anaconda3\lib\site-packages 中的 numpy>=1.11.3 (来自 opencv-contrib-python)
但错误仍然相同,如何解决此错误。请帮助