import cv2
import face_recognition
import pickle
from datetime import datetime
# Load encodings
with open("encodings.pickle", "rb") as f:
data = pickle.load(f)
# Initialize webcam
video_capture = cv2.VideoCapture(0)
# Track attendance
attendance_log = set()
while True:
ret, frame = video_capture.read()
if not ret:
break
# Resize frame for faster processing
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_frame = small_frame[:, :, ::-1]
# Detect faces and compare
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
for face_encoding, face_location in zip(face_encodings, face_locations):
matches = face_recognition.compare_faces(data["encodings"], face_encoding)
name = "Unknown"
if True in matches:
match_index = matches.index(True)
name = data["names"][match_index]
# Log attendance
if name not in attendance_log:
attendance_log.add(name)
print(f"{name} marked present at {datetime.now()}")
# Display bounding box and name
top, right, bottom, left = [v * 4 for v in face_location]
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Show video feed
cv2.imshow("Attendance System", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
video_capture.release()
cv2.destroyAllWindows()
Store the attendance data in an SQLite database for record-keeping.