You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

人脸识别考勤系统CSV记录时间不更新问题求助

解决人脸识别考勤系统时间记录一致的问题

我用OpenCV和face_recognition搭建了一套人脸识别考勤系统,已将人员照片存储为编码文件,系统可识别摄像头中的人脸并完成考勤记录,但存在问题:在不同时间扫描人脸时,CSV文件中记录的时间始终保持一致。

使用的导入库

import face_recognition
import cv2
import numpy as np
import csv
import os
import time 
from datetime import datetime
from tkinter import *
import tkinter as tk
import tkinter.messagebox

人脸编码代码

dhoni_image = face_recognition.load_image_file("photos/dhoni.jpg")
dhoni_encoding = face_recognition.face_encodings(dhoni_image)[0]

virat_image = face_recognition.load_image_file("photos/virat.jpg")
virat_encoding = face_recognition.face_encodings(virat_image)[0]

sachin_image = face_recognition.load_image_file("photos/sachin.jpg")
sachin_encoding = face_recognition.face_encodings(sachin_image)[0]

kalam_image = face_recognition.load_image_file("photos/kalam.jpg")
kalam_encoding = face_recognition.face_encodings(kalam_image)[0]
    
aryan_image = face_recognition.load_image_file("photos/aryan.jpg")
aryan_encoding = face_recognition.face_encodings(aryan_image)[0]

known_face_encoding = [
aryan_encoding,
dhoni_encoding,
virat_encoding,
sachin_encoding,
kalam_encoding
]

known_faces_names = [
"aryan",
"dhoni",
"virat",
"sachin",
"kalam"
]

人脸识别及考勤记录代码(原问题代码)

face_locations = []
face_encodings = []
face_names = []
s=True

while True:
    _,frame = video_capture.read()
    
    small_frame = cv2.resize(frame,(0,0),fx=0.25,fy=0.25)
    rgb_small_frame = small_frame[:,:,::-1]
    
    if s:
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings (rgb_small_frame, face_locations)
        face_names = []
        for face_encoding in face_encodings:
            matches = face_recognition.compare_faces(known_face_encoding,face_encoding)
            name=""
            face_distance = face_recognition.face_distance(known_face_encoding,face_encoding)
            best_match_index = np.argmin(face_distance)
            if matches[best_match_index]:
                name = known_faces_names[best_match_index]

            face_names.append(name)
            if name in known_faces_names:
    
                if name in students:
                    students.remove(name)
                    print(students)
                    present_time = now.strftime("%H.%M.%S")
                    tkinter.messagebox.showinfo("Welcome",message="Welcome "+name)
                    lnwriter.writerow([name,present_time])
                    
    cv2.imshow("attendence system",frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

问题截图

考勤记录时间一致的输出截图


问题原因与解决方法

核心问题

你代码中的now变量是在循环外部提前初始化的,它只会被赋值一次,后续每次调用now.strftime()都是使用这个固定的初始时间,不会随实时时间更新。

修复代码

将获取当前时间的代码now = datetime.now()放到考勤记录的代码块内部,确保每次识别到人脸时都实时获取当前时间:

修改后的循环内考勤记录部分代码:

# ... 其他代码不变 ...
if name in known_faces_names:
    if name in students:
        students.remove(name)
        print(students)
        # 实时获取当前时间
        now = datetime.now()
        present_time = now.strftime("%H.%M.%S")
        tkinter.messagebox.showinfo("Welcome",message="Welcome "+name)
        lnwriter.writerow([name,present_time])
# ... 其他代码不变 ...

额外建议

  1. 建议记录完整的日期+时间,比如把格式改为"%Y-%m-%d %H:%M:%S",方便后续考勤统计
  2. 确保video_capture、students列表、lnwriter(CSV写入对象)在循环前正确初始化,比如:
# 初始化摄像头
video_capture = cv2.VideoCapture(0)
# 初始化待考勤人员列表,和known_faces_names保持一致
students = known_faces_names.copy()
# 初始化CSV写入
with open('attendance.csv', 'w', newline='') as f:
    lnwriter = csv.writer(f)
    # 写入表头(可选)
    lnwriter.writerow(["Name", "Time"])
    # 然后放你的循环代码...

这样修改后,每次识别到人脸时都会获取当前最新的时间,CSV里的记录时间就会和实际打卡时间一致了。


内容的提问来源于stack exchange,提问作者Aryan Jain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 12:32:04