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()
`
这是我的代码...我认为这是100%正确的...但错误表明
AttributeError:模块“cv2.face”没有属性“LBPHFaceRecognizer_create”
我已经尝试了 Google、YouTube、StackOverflow 等多种方法。但没有解决该错误。
我更改了Python版本
pip 安装 opencv-python
还有
pip 安装 opencv-contrib-python
我已经这样做了很多次了,但没有解决办法
该错误意味着您使用了错误的方法在 OpenCV 中创建 LBPH 人脸识别器。
对于 OpenCV 4.x 或更高版本:
cv2.face.LBPHFaceRecognizer_create()
对于 OpenCV 3.x 或更早版本:cv2.createLBPHFaceRecognizer()
使用
cv2.__version__
检查您的 OpenCV 版本,并相应地使用正确的方法。