Download app.py from AnimeKami/Cynthia: direct link, hf CLI and curl.
- Browser
- Download file 3.56 kB
-
https://huggingface.co/spaces/AnimeKami/Cynthia/resolve/main/app.py
- Command line
-
hf download hf://spaces/AnimeKami/Cynthia/app.py
-
curl -L -o app.py https://huggingface.co/spaces/AnimeKami/Cynthia/resolve/main/app.py
3.56 kB
| \ | |
| 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() | |