S.ATTENDENCE / automaticAttedance.py
Bhanumani12's picture
Create automaticAttedance.py
356295e verified
import cv2
import numpy as np
def fill_attendance(update_message, video_path=None):
# Load the trained model and Haar Cascade
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read("trainer.yml")
face_detector = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
# Initialize video capture
if video_path:
cap = cv2.VideoCapture(video_path)
else:
cap = cv2.VideoCapture(0)
print("Taking attendance...")
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
for (x, y, w, h) in faces:
face_image = frame[y:y + h, x:x + w]
label, confidence = recognizer.predict(face_image)
# Here, we can map label to the student's name using a CSV file or any database
print(f"Detected student ID: {label} with confidence: {confidence}")
# For simplicity, let's just print the student ID
update_message(f"Student {label} marked present")
# Draw a rectangle around the face and display the student ID
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("Attendance", frame)
if cv2.waitKey(1) & 0xFF == ord('q'): # Press 'q' to exit
break
cap.release()
cv2.destroyAllWindows()