Spaces:
Sleeping
Sleeping
Create automaticAttedance.py
Browse files- automaticAttedance.py +41 -0
automaticAttedance.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
def fill_attendance(update_message, video_path=None):
|
| 5 |
+
# Load the trained model and Haar Cascade
|
| 6 |
+
recognizer = cv2.face.LBPHFaceRecognizer_create()
|
| 7 |
+
recognizer.read("trainer.yml")
|
| 8 |
+
face_detector = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
|
| 9 |
+
|
| 10 |
+
# Initialize video capture
|
| 11 |
+
if video_path:
|
| 12 |
+
cap = cv2.VideoCapture(video_path)
|
| 13 |
+
else:
|
| 14 |
+
cap = cv2.VideoCapture(0)
|
| 15 |
+
|
| 16 |
+
print("Taking attendance...")
|
| 17 |
+
while True:
|
| 18 |
+
ret, frame = cap.read()
|
| 19 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 20 |
+
faces = face_detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
|
| 21 |
+
|
| 22 |
+
for (x, y, w, h) in faces:
|
| 23 |
+
face_image = frame[y:y + h, x:x + w]
|
| 24 |
+
label, confidence = recognizer.predict(face_image)
|
| 25 |
+
|
| 26 |
+
# Here, we can map label to the student's name using a CSV file or any database
|
| 27 |
+
print(f"Detected student ID: {label} with confidence: {confidence}")
|
| 28 |
+
|
| 29 |
+
# For simplicity, let's just print the student ID
|
| 30 |
+
update_message(f"Student {label} marked present")
|
| 31 |
+
|
| 32 |
+
# Draw a rectangle around the face and display the student ID
|
| 33 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 34 |
+
|
| 35 |
+
cv2.imshow("Attendance", frame)
|
| 36 |
+
|
| 37 |
+
if cv2.waitKey(1) & 0xFF == ord('q'): # Press 'q' to exit
|
| 38 |
+
break
|
| 39 |
+
|
| 40 |
+
cap.release()
|
| 41 |
+
cv2.destroyAllWindows()
|