Python Tkinter回调异常;KeyError:';Id';

Python Tkinter回调异常;KeyError:';Id';,python,pandas,Python,Pandas,我正在开发基于Tkinter和Opencv的人脸记录考勤系统。 PS-这里是新的NOOB编码器。。 在我单击“跟踪图像”按钮之前,一切都正常,这样可以识别人脸,自动拍摄并保存出勤记录,但一旦打开相机和相框,它就会被显示错误的控制台卡住 Exception in Tkinter callback Traceback (most recent call last): File "C:\Python37-32\lib\site-packages\pandas\core\indexes\ba

我正在开发基于Tkinter和Opencv的人脸记录考勤系统。 PS-这里是新的NOOB编码器。。 在我单击“跟踪图像”按钮之前,一切都正常,这样可以识别人脸,自动拍摄并保存出勤记录,但一旦打开相机和相框,它就会被显示错误的控制台卡住

    Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python37-32\lib\site-packages\pandas\core\indexes\base.py", line 3078, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Id'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python37-32\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "C:\Users\relan\Desktop\Aman\New folder\Face-Recognition-Based-Attendance-System-master\train.py", line 195, in TrackImages
    aa=df.loc[df['Id'] == Id]['Name'].values
  File "C:\Python37-32\lib\site-packages\pandas\core\frame.py", line 2688, in __getitem__
    return self._getitem_column(key)
  File "C:\Python37-32\lib\site-packages\pandas\core\frame.py", line 2695, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Python37-32\lib\site-packages\pandas\core\generic.py", line 2489, in _get_item_cache
    values = self._data.get(item)
  File "C:\Python37-32\lib\site-packages\pandas\core\internals.py", line 4115, in get
    loc = self.items.get_loc(item)
  File "C:\Python37-32\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Id'
框架卡住了,我不得不强制退出程序。。 我的trackImage函数代码如下所示

def TrackImages():

    col_names =  ['Id','Name','Date','Time']
    attendance = pd.DataFrame(columns = col_names)    
    while True:
        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])                                   
            if(conf < 50):
                ts = time.time()      
                date = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d')
                timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%H:%M:%S')
                aa=df.loc[df['Id'] == Id]['Name'].values
                tt=str(Id)+"-"+aa
                attendance.loc[len(attendance)] = [Id,aa,date,timeStamp]

            else:
                Id='Unknown'                
                tt=str(Id)  
            if(conf > 75):
                noOfFile=len(os.listdir("ImagesUnknown"))+1
                cv2.imwrite("ImagesUnknown\Image"+str(noOfFile) 
def TrackImages():
col_Name=['Id'、'Name'、'Date'、'Time']
考勤=pd.DataFrame(列=列名称)
尽管如此:
ret,im=cam.read()
灰色=cv2.CVT颜色(im、cv2.COLOR\U BGR2GRAY)
面=面级联。检测多尺度(灰色,1.2,5)
对于面中的(x,y,w,h):
cv2.矩形(im,(x,y),(x+w,y+h),(225,0,0),2)
Id,conf=recognizer.predict(灰色[y:y+h,x:x+w])
如果(形态<50):
ts=时间。时间()
date=datetime.datetime.fromtimestamp(ts).strftime(“%Y-%m-%d”)
timeStamp=datetime.datetime.fromtimestamp(ts).strftime(“%H:%M:%S”)
aa=df.loc[df['Id']==Id]['Name'].值
tt=str(Id)+“-”+aa
考勤.loc[len(考勤)]=[Id,aa,日期,时间戳]
其他:
Id='未知'
tt=str(Id)
如果(形态>75):
noOfFile=len(os.listdir(“ImagesUnknown”)+1
cv2.imwrite(“ImagesUnknown\Image”+str(noOfFile)

使用Python 3.7.0。

而不是
“if(conf<50):”
->
“如果df中的'Id'和(conf<50):“
似乎对我有用。

请提供。我们不需要所有代码。只是一个简单的示例,显示问题区域。@Mike SMT对此感到抱歉,但我无法找出错误在哪里。而不是“if(conf<50):”你能试试“如果df和(conf<50)中的'Id'”:我投票结束这个问题,因为没有尝试调试。重新标记,因为异常来自Pandas数据帧,所以OpenCV和tkinter的内容都不相关。请将此减少到,然后我们可以继续。