Spaces:
Runtime error
Runtime error
supitsparth
commited on
Commit
•
520b2f8
1
Parent(s):
0940651
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import streamlit as st
|
3 |
+
import tensorflow as tf
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
trained_model = tf.keras.models.load_model('facial_emotions_model.h5')
|
7 |
+
classes=['Angry','Disgust','Fear','Happy','Neutral','Sad','Surprise']
|
8 |
+
|
9 |
+
|
10 |
+
st.title('Emotion Detection')
|
11 |
+
|
12 |
+
# run = st.checkbox('Run')
|
13 |
+
|
14 |
+
frame_window = st.image([])
|
15 |
+
frame_window2 = st.image([])
|
16 |
+
|
17 |
+
cam = cv2.VideoCapture(0)
|
18 |
+
|
19 |
+
# pred = 'Detecting'
|
20 |
+
|
21 |
+
a = st.empty()
|
22 |
+
face_cascade = cv2.CascadeClassifier('haarcascade.xml')
|
23 |
+
# a.text_area(value='detecting', label='')
|
24 |
+
while True:
|
25 |
+
ret, frame = cam.read()
|
26 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
27 |
+
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
|
28 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
29 |
+
frame_window2.image(frame)
|
30 |
+
|
31 |
+
for (x, y, w, h) in faces:
|
32 |
+
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
|
33 |
+
frame = frame[y:y+h, x:x+h]
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
if frame.any():
|
40 |
+
resized = cv2.resize(frame, (48, 48))
|
41 |
+
resized_gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
|
42 |
+
reshaped_array = np.reshape(resized_gray, (1, 48, 48, 1))
|
43 |
+
result=trained_model.predict(reshaped_array)
|
44 |
+
y_pred=np.argmax(result[0])
|
45 |
+
|
46 |
+
pred = classes[y_pred]
|
47 |
+
# print('The person facial emotion is:',classes[y_pred])
|
48 |
+
a.write(pred)
|
49 |
+
# a = st.empty()
|
50 |
+
|
51 |
+
|
52 |
+
frame_window.image(resized_gray)
|
53 |
+
|
54 |
+
|
55 |
+
|