Aswinprem04
commited on
Commit
•
a90804b
1
Parent(s):
0667c2f
Upload emotion_app.py
Browse files- emotion_app.py +46 -0
emotion_app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import tensorflow as tf
|
4 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
5 |
+
|
6 |
+
# Load your pre-trained model
|
7 |
+
model = tf.keras.models.load_model('Final_Resnet50_Best_model.keras')
|
8 |
+
|
9 |
+
# Emotion labels
|
10 |
+
emotion_labels = ['Angry', 'Disgust', 'Fear', 'Happy', 'Neutral', 'Sad', 'Surprise']
|
11 |
+
|
12 |
+
# Initialize the face classifier
|
13 |
+
face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
14 |
+
|
15 |
+
|
16 |
+
cap = cv2.VideoCapture(0)
|
17 |
+
|
18 |
+
while True:
|
19 |
+
ret, frame = cap.read()
|
20 |
+
if not ret:
|
21 |
+
break
|
22 |
+
|
23 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
24 |
+
faces = face_classifier.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30),
|
25 |
+
flags=cv2.CASCADE_SCALE_IMAGE)
|
26 |
+
|
27 |
+
for (x, y, w, h) in faces:
|
28 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
29 |
+
face = gray[y:y + h, x:x + w]
|
30 |
+
face = cv2.resize(face, (224, 224))
|
31 |
+
face = face.astype("float") / 255.0
|
32 |
+
face = img_to_array(face)
|
33 |
+
face = np.expand_dims(face, axis=0)
|
34 |
+
|
35 |
+
prediction = model.predict(face)[0]
|
36 |
+
emotion = emotion_labels[np.argmax(prediction)]
|
37 |
+
|
38 |
+
cv2.putText(frame, emotion, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2)
|
39 |
+
|
40 |
+
cv2.imshow("Emotion Detector", frame)
|
41 |
+
|
42 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
43 |
+
break
|
44 |
+
|
45 |
+
cap.release()
|
46 |
+
cv2.destroyAllWindows()
|