我正在开发基于面部识别的考勤系统。这是我的第一个项目,所以我不太熟悉如何处理错误。我卡在中间,不知道如何解决错误。我的终端显示以下错误:
PS E:\Project Work> & "C:/Users/Naik Faheem/AppData/Local/Programs/
Python/Python39/python.exe" "e:/Project Work/test.py"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Naik Faheem\AppData\Local\Programs\Python\
Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "e:\Project Work\test.py", line 62, in face_recog
img=recognize(img,clf,facecascade)
File "e:\Project Work\test.py", line 52, in recognize
coord= draw_boundary(img,facecascade,1.1,10,255,"Face",clf)
File "e:\Project Work\test.py", line 39, in draw_boundary
n="+".join(n)
TypeError: can only join an iterable
[ WARN:1] global C:\Users\runneradmin\AppD
我已将我的实际代码放在下面。此代码旨在读取存储训练数据集的分类器.xml 文件。
from tkinter import *
from PIL import Image, ImageTk
from tkinter import ttk
from tkinter import messagebox
import mysql.connector
import cv2
class Face_recognition:
def __init__(self,root):
self.root=root
self.root.title('Face Detection')
self.root.geometry('1500x800+0+0')
self.root.config(bg='gray')
title_lbl=Label(self.root,text='FACE RECOGNITION',
font=('times new roman',35,'bold'),bg='gray',fg='darkblue')
title_lbl.place(x=0,y=0,width=1500,height=40)
btn=Button(self.root,command=self.face_recog,text='Detect Face',
font=('times new roman',12,'bold'),bg='red')
btn.place(x=650,y=60,width=200,height=40)
def face_recog(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)
coord= []
for (x,y,w,h) in features:
cv2.rectangle(img,(x,y),(x+w,y+h),255,3)
id,predict= clf.predict(gray_image[y:y+h,x:x+w])
confidence= int(100*(1-predict/300))
con= mysql.connector.connect( user='root', host='localhost',
password='',database='stu_details')
cur=con.cursor()
cur.execute("select name from stu_info where name= "+str(id-1))
n=cur.fetchone()
n="+".join(n)
if confidence>70:
cv2.putText(img,f"Name: {n}",(x,y-60),cv2.FONT_HERSHEY_COMPLEX,1,255,2)
else:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,255),4)
cv2.putText(img,"Unknown face",(x,y10),
cv2.FONT_HERSHEY_COMPLEX_SMALL,0.8,(255,255,255),3)
coord=[x,y,w,h]
return coord
def recognize(img,clf,facecascade):
coord= draw_boundary(img,facecascade,1.1,10,255,"Face",clf)
return img
facecascade= cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
clf= cv2.face.LBPHFaceRecognizer_create()
clf.read("classifier.xml")
video_cap=cv2.VideoCapture(0)
while True:
ret,img=video_cap.read()
img=recognize(img,clf,facecascade)
cv2.imshow("Welcome to face recognition",img)
if cv2.waitKey(1)==13:
break
video_cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
root=Tk()
app=Face_recognition(root)
root.mainloop()