[英]I am working on a facial recognition and attendance system which writes the name and time in a CSV file, but the same person is logged multiple times
I am working on a facial recognition and attendance system which writes the name and time in a CSV file.In order to avoid logging the same person for multiple 'in' times i am writtng a logic which checks if the name is present in the attendance log already,if not then the attendance is looged.But the same name is logged over and over inspite of it being already logged once,i am unable to understand the problem.我正在开发一个面部识别和考勤系统,该系统将姓名和时间写入 CSV 文件。为了避免多次“进入”时间记录同一个人,我正在编写一个逻辑来检查姓名是否出现在考勤中已经记录了,如果没有,则记录了出勤率。但是,尽管已经记录了一次,但同名的却一遍又一遍地登录,我无法理解问题所在。
this is the code snippet:这是代码片段:
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
#markAttendance(name)
with open('ATTLOG.csv', "r+") as g:
myDatalist = g.readlines()
nameList=[]
for line in myDatalist:
entry = line.split(',')
nameList.append(entry[0])
if name not in nameList:
now=datetime.now()
dtString = now.strftime('%H:%M:%S')
g.writelines(f'\n{name},{dtString}')
You have a logic bug: you read the whole file into nameList
, then you check if the current name is in the first item of nameList
.您有一个逻辑错误:您将整个文件读入
nameList
,然后检查当前名称是否在nameList
的第一项中。 If not you write it into the file: If your current name comes later in nameList
, you will write it although you should not.如果不是,则将其写入文件:如果您的当前名称稍后出现在
nameList
中,您将写入它,尽管您不应该这样做。
You need to read the whole file, then check if it is anywhere in your nameList
and then decide if you write or not.您需要阅读整个文件,然后检查它是否在您的
nameList
中的任何位置,然后决定是否写入。
For checking you should use a set()
- checking for "is in" is way faster then with a list.对于检查,您应该使用
set()
- 检查“在”中比使用列表要快得多。
already_in_file = set()
with open('ATTLOG.csv', "r") as g: # just read
for line in g:
already_in_file.add(line.split(",")[0])
# process your current entry:
if name not in already_in_file:
with open('ATTLOG.csv', "a") as g: # append
now = datetime.now()
dtString = now.strftime('%H:%M:%S')
g.writelines(f'\n{name},{dtString}')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.