我正在开发一个面部识别和考勤系统,该系统将姓名和时间写入 CSV 文件,但同一个人被多次登录

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

我正在开发一个面部识别和考勤系统,该系统将姓名和时间写入 CSV file.In 以避免多次记录同一个人 'in' 我正在编写一个逻辑检查该名字是否已经出现在出勤日志中,如果没有,则出勤是 looged.But 尽管已经记录过一次,但同一个名字被一遍又一遍地记录,我无法理解这个问题。

这是代码片段:

在脸下方画一个有名字的标签

    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}')

您有一个逻辑错误:您将整个文件读入 nameList,然后检查当前名称是否在 [=11= 的 第一项 中].如果不是,则将其写入文件:如果您当前的名称稍后出现在 nameList 中,您将写入它,尽管您不应该这样做。

你需要读取整个文件,然后检查它是否在你的nameList中的任何地方,然后决定你是否写入。

对于检查,您应该使用 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}')