import streamlit as st import numpy as np import cv2 from PIL import Image import requests import face_recognition from keras.models import load_model import os # Set page title and description st.set_page_config( page_title="Face Recognition Attendance System With Emotion Detection", page_icon="📷", layout="centered", initial_sidebar_state="collapsed" ) st.title("Attendance System Using Face Recognition and Emotion Detection 📷") st.markdown("This app recognizes faces in an image, detects emotions, and updates attendance records with the current timestamp.") # Load emotion detection model @st.cache_resource def load_emotion_model(): model = load_model('CNN_Model_acc_75.h5') return model emotion_model = load_emotion_model() # Emotion labels emotion_labels = ['angry', 'fear', 'happy', 'neutral', 'sad', 'surprise'] # Load known faces and classnames Images = [] classnames = [] directory = "photos" myList = os.listdir(directory) for cls in myList: if os.path.splitext(cls)[1] in [".jpg", ".jpeg"]: img_path = os.path.join(directory, cls) curImg = cv2.imread(img_path) Images.append(curImg) classnames.append(os.path.splitext(cls)[0]) # Function to update attendance data def update_data(name, emotion): url = "https://huggingface.glitch.me" url1 = "/update" data = {'name': name, 'emotion': emotion} try: response = requests.post(url + url1, data=data) if response.status_code == 200: st.success("Attendance updated successfully!") else: st.warning("Failed to update attendance!") except Exception as e: st.error(f"Error updating attendance: {e}") # Function to display image with overlay def display_image_with_overlay(image, name, emotion): cv2.putText(image, f"{name} is feeling {emotion}", (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) st.image(image, use_column_width=True, output_format="PNG") # Load images for face recognition encodeListknown = [face_recognition.face_encodings(img)[0] for img in Images] # Upload image using the file uploader img_file_buffer = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) if img_file_buffer is not None: test_image = Image.open(img_file_buffer) image = np.asarray(test_image) imgS = cv2.resize(image, (0, 0), None, 0.25, 0.25) imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB) facesCurFrame = face_recognition.face_locations(imgS) encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame) name = "Unknown" # Default name for unknown faces match_found = False # Flag to track if a match is found # Emotion detection part face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') emotion = "Neutral" # Default emotion if len(encodesCurFrame) > 0: for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame): # Emotion detection y1, x2, y2, x1 = faceLoc roi = imgS[y1:y2, x1:x2] roi = cv2.resize(roi, (48, 48)) # Resize to fit model roi = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB) roi = np.expand_dims(roi, axis=0) / 255.0 # Preprocess the image emotion_predictions = emotion_model.predict(roi) emotion = emotion_labels[np.argmax(emotion_predictions)] # Face recognition logic matches = face_recognition.compare_faces(encodeListknown, encodeFace) faceDis = face_recognition.face_distance(encodeListknown, encodeFace) matchIndex = np.argmin(faceDis) if matches[matchIndex]: name = classnames[matchIndex].upper() update_data(name, emotion) match_found = True y1, x2, y2, x1 = faceLoc y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4 cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.rectangle(image, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED) cv2.putText(image, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2) display_image_with_overlay(image, name, emotion) if match_found: st.success(f"Face recognized: {name} and Emotion: {emotion}") else: st.warning("Face not detected, or no match found in the database.") else: st.warning("No faces detected in the image. Face recognition failed.")