我目前正在开发我的人脸识别opencv,这个问题在我登录后一直显示出来

2024-05-19 02:50:58 发布

您现在位置:Python中文网/ 问答频道 /正文

Tkinter回调中的异常

Traceback (most recent call last): File "C:\Users\Claire\AppData\Roaming\Python\Python36\site-packages\pandas\core\indexes\base.py", line 2898, in get_loc return self._engine.get_loc(casted_key) File "pandas_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas_libs\hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'Id'

上述异常是以下异常的直接原因:

Traceback (most recent call last): File "C:\Program Files\Python36\lib\tkinter_init_.py", line 1702, in call return self.func(*args) File "C:\Users\Claire\Desktop\New folder (6)\login-verification-master\run.py", line 161, in login_submit TrackImages(a) File "C:\Users\Claire\Desktop\New folder (6)\login-verification-master\run.py", line 135, in TrackImages aa=df.loc[df['Id'] == Id]['Name'].values File "C:\Users\Claire\AppData\Roaming\Python\Python36\site-packages\pandas\core\frame.py", line 2906, in getitem indexer = self.columns.get_loc(key) File "C:\Users\Claire\AppData\Roaming\Python\Python36\site-packages\pandas\core\indexes\base.py", line 2900, in get_loc raise KeyError(key) from err KeyError: 'Id'

这是产生错误的代码

def TrackImages(UserId):
    recognizer = cv2.face.LBPHFaceRecognizer_create()#cv2.createLBPHFaceRecognizer()
    recognizer.read("TrainingImageLabel\Trainner.yml")
    harcascadePath = "haarcascade_frontalface_default.xml"
    faceCascade = cv2.CascadeClassifier(harcascadePath);
    df=pd.read_csv("Details\Details.csv")
    cam = cv2.VideoCapture(0)
    font = cv2.FONT_HERSHEY_SIMPLEX          
    run_count=0;run=True
    while run:
        
        ret, im =cam.read()
        gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
        faces=faceCascade.detectMultiScale(gray, 1.2,5)    
        for(x,y,w,h) in faces:
            cv2.rectangle(im,(x,y),(x+w,y+h),(225,0,0),2)
            Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
            print(Id, conf)
            if(conf < 50):
                aa=df.loc[df.get['Id'] == Id]['Name'].values
                tt=str(Id)+"-"+aa
                if (str(Id)==UserId):
                    print(Id, conf)
                    message.configure(text="Face Recognised Successfully")
                    run=False
            else:
                Id='Unknown'                
                tt=str(Id)            
            cv2.putText(im,str(tt),(x,y+h), font, 1,(255,255,255),2)        
        run_count += 1    
        cv2.imshow('im',im) 
        if (cv2.waitKey(1)==ord('q') or run_count==150):
            message.configure(text="Unable to Recognise Face")
            break
    
    cam.release()
    cv2.destroyAllWindows()
    


def login_submit():
    a=txt.get()
    b=txt2.get()
    if (a in data):
        if(data[a] == b):
            TrackImages(a)
        else:
            message.configure(text="Id and Password does not Match")
    else:
        message.configure(text="Entered Id does not Exists")

    login_clear()

Tags: runinpyidpandasgetlinelogin
2条回答

下面是存储数据帧的代码

def TakeImages():        
        Id=(txt3.get())
        name=(txt4.get())
        ret=0
        if (Id not in data):
            cam = cv2.VideoCapture(0)
            harcascadePath = "haarcascade_frontalface_default.xml"
            detector=cv2.CascadeClassifier(harcascadePath)
            sampleNum=0
            while(True):
                ret, img = cam.read()
                gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                faces = detector.detectMultiScale(gray, 1.3, 5)
                for (x,y,w,h) in faces:
                    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)        
                    #incrementing sample number 
                    sampleNum=sampleNum+1
                    #saving the captured face in the dataset folder TrainingImage
                    cv2.imwrite("TrainingImage\ "+name +'.'+Id+'.'+ str(sampleNum) + ".jpg", gray[y:y+h,x:x+w])
                    #display the frame
                    cv2.imshow('frame',img)
                #wait for 100 miliseconds 
                if cv2.waitKey(100) & 0xFF == ord('q'):
                    break
                # break if the sample number is morethan 100
                elif sampleNum>100:
                    break
            cam.release()
            cv2.destroyAllWindows() 
            res = "Images Saved for ID : " + Id +" Name : "+ name
            row = [Id , name]
            with open('Details\Details.csv','a+') as csvFile:
                writer = csv.writer(csvFile)
                writer.writerow(row)
            csvFile.close()
            message.configure(text= res)
            ret=1
        else:
            res = "User name Already Exists...Try another one!!!"
            message.configure(text= res)
        return ret

以下是我的csv的外观。 enter image description here

您的错误如下:

aa=df.loc[df.get['Id'] == Id]['Name'].values

您使用了square方括号而不是small方括号

改用这个:

aa=df.loc[df.get('Id') == Id]['Name'].values

或者,您也可以使用以下选项:

aa=df.loc[df['Id'] == Id]['Name'].values

相关问题 更多 >

    热门问题