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
Notice how time is the same even after scanning at different times.