如何解决AttributeError:模块'cv2.face'无'LBPHFaceRecognizer_create'属性?
问题: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',已尝试更换Python版本、多次重装opencv-python与opencv-contrib-python,问题仍未解决。
解决方案
彻底清理现有OpenCV相关包
执行以下命令卸载所有可能冲突的OpenCV包:pip uninstall -y opencv-python opencv-contrib-python opencv-python-headless opencv-contrib-python-headless该操作确保环境中没有残留的不同版本OpenCV文件,避免模块冲突。
安装版本完全匹配的OpenCV包
LBPHFaceRecognizer属于opencv-contrib-python扩展模块,必须保证主包和扩展包版本一致。推荐安装稳定兼容版本:pip install opencv-python==4.5.5.62 opencv-contrib-python==4.5.5.62若需要更高版本,确保两个包的版本号完全相同即可。
验证安装有效性
打开Python交互终端,执行以下代码检查:import cv2 print(cv2.__version__) print(hasattr(cv2.face, 'LBPHFaceRecognizer_create'))若输出对应版本号和
True,说明模块已正常安装。检查模型文件兼容性
确保train_data.xml是使用同版本OpenCV训练生成的,不同版本的模型文件可能无法被正确读取,导致模块调用失败。同时确认haarcascade_frontalface_default.xml路径正确,可使用绝对路径避免文件找不到的问题。
内容的提问来源于stack exchange,提问作者Jahidul Islam
相关产品推荐
相关产品推荐

