|
| 1 | +import face_recognition |
| 2 | +import cv2 |
| 3 | +import numpy as np |
| 4 | +import csv |
| 5 | +import os |
| 6 | +import streamlit as st |
| 7 | +from datetime import datetime |
| 8 | +from ultralytics import YOLO |
| 9 | + |
| 10 | +# Load YOLO model |
| 11 | +yolo_model = YOLO("yolov8/yolov8n-face.pt") |
| 12 | + |
| 13 | +# Load and encode images |
| 14 | +image_dir = "photos/" |
| 15 | +encodings = {} |
| 16 | +known_face_names = [] |
| 17 | + |
| 18 | +for fname in os.listdir(image_dir): |
| 19 | + file_path = os.path.join(image_dir, fname) |
| 20 | + image = face_recognition.load_image_file(file_path) |
| 21 | + encoding = face_recognition.face_encodings(image) |
| 22 | + if encoding: |
| 23 | + encodings[fname] = encoding[0] |
| 24 | + known_face_names.append(fname.split('.')[0]) # Assuming the names are derived from file names |
| 25 | + |
| 26 | +# Known face encodings and names |
| 27 | +known_face_encodings = list(encodings.values()) |
| 28 | +known_face_names = [name.split('.')[0] for name in encodings.keys()] |
| 29 | + |
| 30 | +# Streamlit app |
| 31 | +st.title("Face Recognition Attendance System") |
| 32 | +uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) |
| 33 | + |
| 34 | +if uploaded_image is not None: |
| 35 | + # Read and process the uploaded image |
| 36 | + img = face_recognition.load_image_file(uploaded_image) |
| 37 | + |
| 38 | + # Create or open a CSV file for the current date |
| 39 | + now = datetime.now() |
| 40 | + current_date = now.strftime("%Y-%m-%d") |
| 41 | + attendance_file = current_date + '.csv' |
| 42 | + |
| 43 | + # Initialize students list |
| 44 | + students = known_face_names.copy() |
| 45 | + attendance_records = [] # Store attendance records |
| 46 | + |
| 47 | + # Get YOLO results |
| 48 | + yolo_results = yolo_model(img) |
| 49 | + yolo_boxes = yolo_results[0].boxes |
| 50 | + |
| 51 | + # Find face locations and encodings for detected faces |
| 52 | + face_locations = face_recognition.face_locations(img) |
| 53 | + face_encodings = face_recognition.face_encodings(img, face_locations) |
| 54 | + |
| 55 | + face_names = [] # List to hold names for all detected faces |
| 56 | + |
| 57 | + for face_encoding in face_encodings: |
| 58 | + matches = face_recognition.compare_faces(known_face_encodings, face_encoding) |
| 59 | + name = "Unknown" # Default to "Unknown" for unmatched faces |
| 60 | + |
| 61 | + if True in matches: # If there is a match |
| 62 | + best_match_index = np.argmin(face_recognition.face_distance(known_face_encodings, face_encoding)) |
| 63 | + name = known_face_names[best_match_index] # Get the name of the matched face |
| 64 | + |
| 65 | + # Attendance tracking |
| 66 | + if name in students: |
| 67 | + students.remove(name) |
| 68 | + # Record attendance in the list |
| 69 | + attendance_records.append([name, 'present']) |
| 70 | + face_names.append(name) # Append the name to the list (either known or "Unknown") |
| 71 | + |
| 72 | + # Draw rectangles and labels for all detected faces |
| 73 | + for (top, right, bottom, left), name in zip(face_locations, face_names): |
| 74 | + cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2) |
| 75 | + cv2.putText(img, name, (left, bottom + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 2) |
| 76 | + |
| 77 | + # Display the image in Streamlit |
| 78 | + st.image(img, caption='Processed Image', channels="RGB") |
| 79 | + |
| 80 | + # Write to CSV |
| 81 | + with open(attendance_file, 'w', newline='') as f: |
| 82 | + lnwriter = csv.writer(f) |
| 83 | + lnwriter.writerow(["Name", "Status"]) # Write header |
| 84 | + # Write present records |
| 85 | + lnwriter.writerows(attendance_records) |
| 86 | + # Write absent students |
| 87 | + for name in students: |
| 88 | + lnwriter.writerow([name, 'absent']) |
0 commit comments