如何在 openCV tkinter 代码中摆脱这个问题

问题描述

我正在研究基于面部识别的考勤系统。这是我的第一个项目,所以我不太熟悉如何处理错误。我卡在中间,不知道如何解决错误。我的终端显示以下错误

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',12,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,minNeighbors)

            coord= []

            for (x,y,w,h) in features:
                cv2.rectangle(img,(x,y),(x+w,y+h),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}",y-60),cv2.FONT_HERShey_COMPLEX,1,2)
                else:
                    cv2.rectangle(img,(255,255),4)
                    cv2.putText(img,"UnkNown face",y10),cv2.FONT_HERShey_COMPLEX_SMALL,0.8,3)

                    coord=[x,h]
                return coord
            def recognize(img,facecascade):
                       coord= draw_boundary(img,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,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()

解决方法

错误已修复: 以升序的 id 注册记录(例如 1,2,3....),这个问题就解决了

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...