简体   繁体   English

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

[英]How to get rid of this problem in openCV tkinter code

I am working on a facial recognition based attendance system.This is my first project so i am not well acquainted with how to deal with errors.我正在研究基于面部识别的考勤系统。这是我的第一个项目,所以我不太了解如何处理错误。 I am stuck in the middle and don't know how to solve the error.我卡在中间,不知道如何解决错误。 My terminal displays the following error:我的终端显示以下错误:

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

I HAVE PUT MY ACTUAL CODE BELOW.This code is intended to read classifier.xml file that stores the trained dataset.我已将我的实际代码放在下面。此代码旨在读取存储训练数据集的 classifier.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()

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

Change your data type from int to varchar in your data base to get rid of this error.将数据库中的数据类型从int更改为varchar以消除此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM