import cv2 import numpy as np import tensorflow as tf from tensorflow.keras.preprocessing.image import img_to_array # Load your pre-trained model model = tf.keras.models.load_model('Final_Resnet50_Best_model.keras') # Emotion labels emotion_labels = ['Angry', 'Disgust', 'Fear', 'Happy', 'Neutral', 'Sad', 'Surprise'] # Initialize the face classifier face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() if not ret: break gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_classifier.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) face = gray[y:y + h, x:x + w] face = cv2.resize(face, (224, 224)) face = face.astype("float") / 255.0 face = img_to_array(face) face = np.expand_dims(face, axis=0) prediction = model.predict(face)[0] emotion = emotion_labels[np.argmax(prediction)] cv2.putText(frame, emotion, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2) cv2.imshow("Emotion Detector", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()