繁体   English   中英

我正在开发一个面部识别和考勤系统,该系统将姓名和时间写入 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 文件。为了避免多次“进入”时间记录同一个人,我正在编写一个逻辑来检查姓名是否出现在考勤中已经记录了,如果没有,则记录了出勤率。但是,尽管已经记录了一次,但同名的却一遍又一遍地登录,我无法理解问题所在。

这是代码片段:

画一个label,脸下面有名字

    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 ,然后检查当前名称是否在nameList第一项中。 如果不是,则将其写入文件:如果您的当前名称稍后出现在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}')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM