如何解决AttributeError: module 'cv2.face' has no attribute 'LBPHFaceRecognizer_create'?
import cv2
from tkinter import *
from PIL import Image, ImageTk
import mysql.connector
class Face_Recognition:
def __init__(self, root):
self.root = root
self.root.geometry("1366x768+0+0")
self.root.title("Face Recognition")
self.img = Image.open("photos/pagebg.png")
self.photoimg = ImageTk.PhotoImage(self.img)
self.label = Label(self.root, image=self.photoimg)
self.label.pack(fill=BOTH, expand=YES)
self.label.bind("<Configure>", self.resize_image)
def resize_image(self, event):
new_width = event.width
new_height = event.height
self.img = self.img.resize((new_width, new_height))
self.photoimg = ImageTk.PhotoImage(self.img)
self.label.config(image=self.photoimg)
self.button_frame = Frame(self.label, bg='white')
self.button_frame.place(x=560, y=180, width=180, height=180)
img02 = Image.open(r"E:\new project\photos\attandance.jpg")
img02 = img02.resize((180, 180))
self.photoimg02 = ImageTk.PhotoImage(img02)
b1 = Button(self.button_frame, image=self.photoimg02, border=0, cursor="hand2", command=self.face_function)
b1.pack(fill=BOTH, expand=YES)
self.button_frame = Frame(self.label, bg='black')
self.button_frame.place(x=560, y=320, width=180, height=40)
b1_1 = Button(self.button_frame, text="Attendance", border=0, command=self.face_function, cursor="hand2",
font=("Roca Two", 15, "bold"), bg="#D3D3D3", fg="black")
b1_1.pack(fill=BOTH, expand=YES)
def face_function(self) :
def draw_boundary(img , classifier , scalefactor , minNeighbors , color , text , clf) :
gray_image = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)
features = classifier.detectMultiScale(gray_image , scalefactor , minNeighbors)
for (x , y , w , h) in features :
cv2.rectangle(img , (x , y) , (x + w , y + h) , (0 , 255 , 0) , 3)
id , predict = clf.predict(gray_image [ y :y + h , x :x + w ])
confidence = int((100 * (1 - predict / 300)))
if confidence > 77 :
try :
conn = mysql.connector.connect(host="localhost" , user="root" , password="shihir09876" ,
database="student_info")
my_cursor = conn.cursor()
my_cursor.execute("SELECT `StudentName` FROM `student` WHERE `Student ID` = %s" , (id,))
result = my_cursor.fetchone()
if result :
StudentName , department = result
cv2.putText(img , f"Name: {StudentName}" , (x , y - 55) , cv2.FONT_HERSHEY_COMPLEX , 1 ,
(255 , 255 , 255) , 3)
cv2.putText(img , f"ID: {id}" , (x , y - 40) , cv2.FONT_HERSHEY_COMPLEX , 1 ,
(255 , 255 , 255) , 3)
cv2.putText(img , f"Department: {department}" , (x , y - 25) , cv2.FONT_HERSHEY_COMPLEX ,
1 , (255 , 255 , 255) , 3)
except mysql.connector.Error as e :
print("Error while fetching data from MySQL" , e)
finally :
if conn.is_connected() :
my_cursor.close()
conn.close()
else :
cv2.rectangle(img , (x , y) , (x + w , y + h) , (0 , 0 , 255) , 3)
cv2.putText(img , "Unknown" , (x , y - 25) , cv2.FONT_HERSHEY_COMPLEX , 1 , (255 , 255 , 255) , 3)
def recognize(img , clf , face_cascade) :
draw_boundary(img , face_cascade , 1.1 , 10 , (255 , 25 , 255) , "face" , clf)
return img
face_cascade = cv2.CascadeClassifier("data/haarcascade_frontalface_default.xml")
clf =cv2.face.LBPHFaceRecognizer_create()
clf.read("train_data.xml")
video_cap = cv2.VideoCapture(0)
while True :
ret , img = video_cap.read()
img = recognize(img , clf , face_cascade)
cv2.imshow("Welcome To Face Recognition" , img)
if cv2.waitKey(1) == 13 :
break
video_cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
root = Tk()
obj = Face_Recognition(root)
root.mainloop()
这是我的代码……我觉得是完全正确的……但是错误提示是
AttributeError: module 'cv2.face' has no attribute 'LBPHFaceRecognizer_create'
我已经在谷歌、YouTube、StackOverflow等地方尝试了很多方法,但都没有解决这个错误。
我还换了Python版本
用命令
pip install opencv-python
还有
pip install opencv-contrib-python
我已经做了很多次,但还是没有找到解决办法。
1 个回答
0
这个错误的意思是你在用错误的方法来创建一个LBPH人脸识别器,使用的是OpenCV这个库。
如果你使用的是OpenCV 4.x或更新的版本,你应该用这个方法:cv2.face.LBPHFaceRecognizer_create()
。如果你用的是OpenCV 3.x或更早的版本,那就要用这个方法:cv2.createLBPHFaceRecognizer()
。
你可以通过运行cv2.__version__
来检查你当前的OpenCV版本,然后根据版本使用正确的方法。