Vidit123/Emotion-recognition
0
1import cv22from deepface import DeepFace3 4# Load face cascade classifier5face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')6 7# Start capturing video8cap = cv2.VideoCapture(0)9 10while True:11 # Capture frame-by-frame12 ret, frame = cap.read()13 14 # Flip the frame horizontally to make it a mirror image15 frame = cv2.flip(frame, 1)16 17 # Convert frame to grayscale18 gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)19 20 # Convert grayscale frame to RGB format21 rgb_frame = cv2.cvtColor(gray_frame, cv2.COLOR_GRAY2RGB)22 23 # Detect faces in the frame24 faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))25 26 for (x, y, w, h) in faces:27 # Extract the face ROI (Region of Interest)28 face_roi = rgb_frame[y:y + h, x:x + w]29 30 # Perform emotion analysis on the face ROI31 result = DeepFace.analyze(face_roi, actions=['emotion'], enforce_detection=False)32 33 # Determine the dominant emotion34 emotion = result[0]['dominant_emotion']35 36 # Draw rectangle around face and label with predicted emotion37 cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)38 cv2.putText(frame, emotion, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)39 40 # Display the resulting frame41 cv2.imshow('Real-time Emotion Detection', frame)42 43 # Press 'q' to exit44 if cv2.waitKey(1) & 0xFF == ord('q'):45 break46 47# Release the capture and close all windows48cap.release()49cv2.destroyAllWindows()50 