[英]KeyError when using Pandas to access csv files
我已经使用 pandas 成功创建了 csv 文件。 我收到以下错误:
回溯(最后一次调用):文件“C:\Users\Manoj Kumar\PycharmProjects\trex\venv\lib\site-packages\pandas\core\indexes\base.py”,第 3078 行,在 get_loc 返回 self._engine .get_loc(key) 文件 "pandas_libs\index.pyx",第 140 行,在 pandas._libs.index.IndexEngine.get_loc 文件 "pandas_libs\index.pyx",第 162 行,在 pandas.IndexEngine.get_loc 文件中"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'
在处理上述异常的过程中,又出现了一个异常:
回溯(最近一次调用最后一次):文件“C:\Users\Manoj Kumar\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py”,第 1702 行,在调用中返回 self.func(*args) 文件“ C:/Users/Manoj Kumar/PycharmProjects/trex/基于面部识别的考勤管理 - Copy/train.py",第 206 行,在 TrackImages aa = df.iloc[df['Id'] == Id]['Name' ].values 文件“C:\Users\Manoj Kumar\PycharmProjects\trex\venv\lib\site-packages\pandas\core\frame.py”,第 2688 行,在getitem返回 self._getitem_column(key) 文件“C: \Users\Manoj Kumar\PycharmProjects\trex\venv\lib\site-packages\pandas\core\frame.py”,第 2695 行,在 _getitem_column 中返回 self._get_item_cache(key) 文件“C:\Users\Manoj Kumar\PycharmProjects \trex\venv\lib\site-packages\pandas\core\generic.py”,第 2489 行,在 _get_item_cache 值 = self._data.get(item) 文件“C:\Users\Manoj Kumar\PycharmProjects\trex\venv \lib\site-packages\pandas\core\internals.py”,第 4115 行,在 get loc = self.items.get_loc(item) 文件“C:\Users\Manoj Kumar\PycharmProjec ts\trex\venv\lib\site-packages\pandas\core\indexes\base.py”,第 3080 行,在 get_loc 返回 self._engine.get_loc(self._maybe_cast_indexer(key)) 文件“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 文件“pandas_libs\hashtable_class_helper.pxi”,第 1500 行,在 pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'Id'
尝试从代码访问 csv 文件时。 代码是:
recognizer = cv2.face.EigenFaceRecognizer_create() # cv2.createLBPHFaceRecognizer()
recognizer.read("TrainingImageLabel\Trainner.yml")
harcascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(harcascadePath);
df = pd.read_csv("StudentDetails\StudentDetails.csv")
cam = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
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)
if np.all(np.array(np.array(faces).shape)) and faces is not None:
for (x, y, w, h) in faces:
cv2.rectangle(im, (x, y), (x + w, y + h), (225, 0, 0), 2)
gray = gray[y:y + h, x:x + w]
gray = cv2.resize(gray, (100, 100))
Id, conf = recognizer.predict(gray)
print(Id, conf)
if (conf < 2000):
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 > 2000):
noOfFile = len(os.listdir("ImagesUnknown")) + 1
cv2.imwrite("ImagesUnknown\Image" + str(noOfFile) + ".jpg", im[y:y + h, x:x + w])
cv2.putText(im, str(tt), (x, y + h), font, 1, (255, 255, 255), 2)
attendance = attendance.drop_duplicates(subset=['Id'], keep='first')
cv2.imshow('im', im)
if (cv2.waitKey(1) == ord('q')):
break
ts = time.time()
date = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d')
timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%H:%M:%S')
Hour, Minute, Second = timeStamp.split(":")
fileName = "Attendance\Attendance_" + date + "_" + Hour + "-" + Minute + "-" + Second + ".csv"
attendance.to_csv(fileName, index=False)
cam.release()
cv2.destroyAllWindows()
# print(attendance)
res = attendance
message2.configure(text=res)```
The objective of the code is to recognize faces.
好像你在这一行失败了:
aa = df.loc[df['Id'] == Id]['Name'].values
这可能是因为 csv 不包含名为“Id”的列。
请检查:(:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.