File size: 3,560 Bytes
3b10478
0092fd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

    import cv2
    import face_recognition
    import numpy as np
    import pickle
    from utils import mark_attendance, is_before_deadline, get_today_files
    import pytz
    import os

    ENCODINGS_FILE = "known_faces/encodings.pkl"
    if not os.path.exists(ENCODINGS_FILE):
        print("No encodings found. Please register students first using register_gui.py")
        raise SystemExit(1)

    with open(ENCODINGS_FILE, "rb") as f:
        known_face_encodings, known_face_names, known_face_classes = pickle.load(f)

    video_capture = cv2.VideoCapture(0)
    IST = pytz.timezone("Asia/Kolkata")
    print("✅ System ready. Attendance open until 8:20 AM IST. Press 'q' in the window to quit.")

    # Define square box (pixel coordinates on the full-res frame)
    # Adjust these to fit your camera/resolution if needed
    BOX_TOP_LEFT = (200, 100)
    BOX_BOTTOM_RIGHT = (440, 340)

    while True:
        ret, frame = video_capture.read()
        if not ret:
            break

        # Optionally flip if your camera mirrors
        # frame = cv2.flip(frame, 1)

        full_h, full_w = frame.shape[:2]

        # Draw the square box on the frame
        cv2.rectangle(frame, BOX_TOP_LEFT, BOX_BOTTOM_RIGHT, (255, 0, 0), 2)
        cv2.putText(frame, "Align face in box", (BOX_TOP_LEFT[0], BOX_TOP_LEFT[1]-10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255,0,0), 2)

        # Resize for faster processing (face_recognition expects RGB)
        small_frame = cv2.resize(frame, (0,0), fx=0.25, fy=0.25)
        rgb_small_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)

        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

        for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
            # Scale back up to full frame size
            top *= 4; right *= 4; bottom *= 4; left *= 4
            cx = (left + right) // 2
            cy = (top + bottom) // 2

            # Check if face center is inside the box
            if (BOX_TOP_LEFT[0] < cx < BOX_BOTTOM_RIGHT[0]) and (BOX_TOP_LEFT[1] < cy < BOX_BOTTOM_RIGHT[1]):
                matches = face_recognition.compare_faces(known_face_encodings, face_encoding, tolerance=0.5)
                name, student_class = "Unknown", ""

                if len(known_face_encodings) > 0:
                    face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
                    best_match_index = np.argmin(face_distances)
                    if matches[best_match_index]:
                        name = known_face_names[best_match_index]
                        student_class = known_face_classes[best_match_index]

                        # Mark attendance only if before deadline (8:20 IST)
                        if is_before_deadline(8, 20):
                            mark_attendance(name, student_class)

                # Draw rectangle for the face and label
                cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
                cv2.rectangle(frame, (left, bottom-30), (right, bottom), (0,255,0), cv2.FILLED)
                cv2.putText(frame, f"{name} ({student_class})", (left+6, bottom-6),
                            cv2.FONT_HERSHEY_DUPLEX, 0.7, (0,0,0), 1)

        cv2.imshow("Cythia Face Attendance (press q to quit)", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    video_capture.release()
    cv2.destroyAllWindows()