1

I am using OpenCV and face_recognition to create a facial recognition attendance system. I have stored the photos of people as encoding files, and I want to identify these people when their faces appear on camera. My system is working, but I would like to add a few features to make it more user friendly.

Imports and libraries used:

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

This is the code for encoding the files:

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"
]

This code identifies the faces and prints the names:

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

My output

Output

Notice how time is the same even after scanning at different times.

Christian
  • 4,902
  • 4
  • 24
  • 42
Aryan Jain
  • 11
  • 2
  • 1
    If I get you right the question is not on face recognition but about writing a current timestamp to some output file? Could you please add an example of your output and what you would expect? – Markus Jun 18 '23 at 15:16

2 Answers2

2

You haven't shown us, but I assume that you have now = datetime.datetime.now() at the beginning. That's the issue. The value of that variable never changes. All of your rows will have exactly the same value. You need to re-fetch the value every time, like

present_time = datetime.datetime.now().strftime('%H.%M.%S')
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
2

The call to strftime is present_time = now.strftime("%H.%M.%S") but I don't see now being initialized anywhere. If it is initialized outside the loop, it will be fixed.

What if you changed that line to present_time = datetime.now.strftime("%H.%M.%S")?