Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
import cv2
|
3 |
import numpy as np
|
@@ -13,131 +14,157 @@ st.markdown("<h1 style='text-align: center;'>Emotion Detection with Face Recogni
|
|
13 |
# Smaller subtitle
|
14 |
st.markdown("<h3 style='text-align: center;'>angry, fear, happy, neutral, sad, surprise</h3>", unsafe_allow_html=True)
|
15 |
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
# Load the emotion model
|
19 |
@st.cache_resource
|
20 |
def load_emotion_model():
|
21 |
-
model = load_model('CNN_Model_acc_75.h5')
|
22 |
return model
|
23 |
|
24 |
model = load_emotion_model()
|
25 |
-
print("time taken to load model: ", time.time() - start)
|
26 |
|
27 |
# Emotion labels
|
28 |
emotion_labels = ['angry', 'fear', 'happy', 'neutral', 'sad', 'surprise']
|
29 |
|
30 |
-
#
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
if image is not None:
|
119 |
-
# Convert the image to a numpy array
|
120 |
-
frame = np.array(Image.open(image))
|
121 |
-
frame, result_text = process_frame(frame)
|
122 |
-
st.image(frame, caption='Processed Image', use_column_width=True)
|
123 |
-
st.markdown(f"<h3 style='text-align: center;'>{result_text}</h3>", unsafe_allow_html=True)
|
124 |
-
|
125 |
-
elif upload_choice == "Upload Image":
|
126 |
-
uploaded_image = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg", "gif"])
|
127 |
-
if uploaded_image:
|
128 |
-
image = Image.open(uploaded_image)
|
129 |
-
frame = np.array(image)
|
130 |
-
frame, result_text = process_frame(frame)
|
131 |
-
st.image(frame, caption='Processed Image', use_column_width=True)
|
132 |
-
st.markdown(f"<h3 style='text-align: center;'>{result_text}</h3>", unsafe_allow_html=True)
|
133 |
-
|
134 |
-
elif upload_choice == "Upload Video":
|
135 |
-
uploaded_video = st.file_uploader("Upload Video", type=["mp4", "mov", "avi", "mkv", "webm"])
|
136 |
-
if uploaded_video:
|
137 |
-
# Temporarily save the video to disk
|
138 |
-
with tempfile.NamedTemporaryFile(delete=False) as tfile:
|
139 |
-
tfile.write(uploaded_video.read())
|
140 |
-
video_source = cv2.VideoCapture(tfile.name)
|
141 |
-
video_feed(video_source)
|
142 |
-
|
143 |
-
st.sidebar.write("Emotion Labels: Angry, Fear, Happy, Neutral, Sad, Surprise")
|
|
|
1 |
+
import sqlite3
|
2 |
import streamlit as st
|
3 |
import cv2
|
4 |
import numpy as np
|
|
|
14 |
# Smaller subtitle
|
15 |
st.markdown("<h3 style='text-align: center;'>angry, fear, happy, neutral, sad, surprise</h3>", unsafe_allow_html=True)
|
16 |
|
17 |
+
# Database setup
|
18 |
+
DATABASE_NAME = "emotion_recognition.db"
|
19 |
+
|
20 |
+
def init_db():
|
21 |
+
conn = sqlite3.connect(DATABASE_NAME)
|
22 |
+
cursor = conn.cursor()
|
23 |
+
cursor.execute('''
|
24 |
+
CREATE TABLE IF NOT EXISTS registered_faces (
|
25 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
26 |
+
name TEXT NOT NULL,
|
27 |
+
image BLOB NOT NULL
|
28 |
+
)
|
29 |
+
''')
|
30 |
+
cursor.execute('''
|
31 |
+
CREATE TABLE IF NOT EXISTS attendance_log (
|
32 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
33 |
+
name TEXT NOT NULL,
|
34 |
+
emotion TEXT NOT NULL,
|
35 |
+
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
|
36 |
+
)
|
37 |
+
''')
|
38 |
+
conn.commit()
|
39 |
+
conn.close()
|
40 |
+
|
41 |
+
init_db()
|
42 |
+
|
43 |
+
def register_face(name, image):
|
44 |
+
conn = sqlite3.connect(DATABASE_NAME)
|
45 |
+
cursor = conn.cursor()
|
46 |
+
cursor.execute("INSERT INTO registered_faces (name, image) VALUES (?, ?)", (name, image))
|
47 |
+
conn.commit()
|
48 |
+
conn.close()
|
49 |
+
|
50 |
+
def fetch_registered_faces():
|
51 |
+
conn = sqlite3.connect(DATABASE_NAME)
|
52 |
+
cursor = conn.cursor()
|
53 |
+
cursor.execute("SELECT id, name FROM registered_faces")
|
54 |
+
rows = cursor.fetchall()
|
55 |
+
conn.close()
|
56 |
+
return rows
|
57 |
+
|
58 |
+
def log_attendance(name, emotion):
|
59 |
+
conn = sqlite3.connect(DATABASE_NAME)
|
60 |
+
cursor = conn.cursor()
|
61 |
+
cursor.execute("INSERT INTO attendance_log (name, emotion) VALUES (?, ?)", (name, emotion))
|
62 |
+
conn.commit()
|
63 |
+
conn.close()
|
64 |
+
|
65 |
+
def fetch_recent_activity():
|
66 |
+
conn = sqlite3.connect(DATABASE_NAME)
|
67 |
+
cursor = conn.cursor()
|
68 |
+
cursor.execute("SELECT name, emotion, timestamp FROM attendance_log ORDER BY timestamp DESC LIMIT 10")
|
69 |
+
rows = cursor.fetchall()
|
70 |
+
conn.close()
|
71 |
+
return rows
|
72 |
|
73 |
# Load the emotion model
|
74 |
@st.cache_resource
|
75 |
def load_emotion_model():
|
76 |
+
model = load_model('CNN_Model_acc_75.h5')
|
77 |
return model
|
78 |
|
79 |
model = load_emotion_model()
|
|
|
80 |
|
81 |
# Emotion labels
|
82 |
emotion_labels = ['angry', 'fear', 'happy', 'neutral', 'sad', 'surprise']
|
83 |
|
84 |
+
# Sidebar options
|
85 |
+
sidebar_choice = st.sidebar.selectbox("Choose an option", ["Emotion Detection", "Register New Face", "View Registered Faces", "Recent Activity"])
|
86 |
+
|
87 |
+
if sidebar_choice == "Register New Face":
|
88 |
+
st.header("Register New Face")
|
89 |
+
name = st.text_input("Enter Name")
|
90 |
+
uploaded_image = st.file_uploader("Upload Face Image", type=["png", "jpg", "jpeg"])
|
91 |
+
if name and uploaded_image:
|
92 |
+
image = np.array(Image.open(uploaded_image))
|
93 |
+
_, buffer = cv2.imencode('.jpg', image)
|
94 |
+
register_face(name, buffer.tobytes())
|
95 |
+
st.success(f"Successfully registered {name}!")
|
96 |
+
|
97 |
+
elif sidebar_choice == "View Registered Faces":
|
98 |
+
st.header("Registered Faces")
|
99 |
+
faces = fetch_registered_faces()
|
100 |
+
if faces:
|
101 |
+
for face_id, name in faces:
|
102 |
+
st.write(f"ID: {face_id}, Name: {name}")
|
103 |
+
else:
|
104 |
+
st.write("No faces registered yet.")
|
105 |
+
|
106 |
+
elif sidebar_choice == "Recent Activity":
|
107 |
+
st.header("Recent Activity (Attendance Log)")
|
108 |
+
logs = fetch_recent_activity()
|
109 |
+
if logs:
|
110 |
+
for name, emotion, timestamp in logs:
|
111 |
+
st.write(f"Name: {name}, Emotion: {emotion}, Timestamp: {timestamp}")
|
112 |
+
else:
|
113 |
+
st.write("No recent activity found.")
|
114 |
+
|
115 |
+
else: # Emotion Detection
|
116 |
+
st.sidebar.write("Emotion Labels: Angry, Fear, Happy, Neutral, Sad, Surprise")
|
117 |
+
|
118 |
+
upload_choice = st.radio("Choose input source", ["Upload Image", "Upload Video", "Camera"])
|
119 |
+
|
120 |
+
def process_frame(frame):
|
121 |
+
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
122 |
+
faces = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml').detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
123 |
+
|
124 |
+
result_text = ""
|
125 |
+
for (x, y, w, h) in faces:
|
126 |
+
roi_gray = gray_frame[y:y+h, x:x+w]
|
127 |
+
roi_color = frame[y:y+h, x:x+w]
|
128 |
+
face_roi = cv2.resize(roi_color, (48, 48))
|
129 |
+
face_roi = cv2.cvtColor(face_roi, cv2.COLOR_BGR2RGB) / 255.0
|
130 |
+
face_roi = np.expand_dims(face_roi, axis=0)
|
131 |
+
|
132 |
+
predictions = model.predict(face_roi)
|
133 |
+
emotion = emotion_labels[np.argmax(predictions[0])]
|
134 |
+
|
135 |
+
label = "Unknown" # Placeholder for face recognition (add later)
|
136 |
+
log_attendance(label, emotion)
|
137 |
+
|
138 |
+
result_text = f"{label} is feeling {emotion}"
|
139 |
+
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
|
140 |
+
cv2.putText(frame, result_text, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
141 |
+
return frame, result_text
|
142 |
+
|
143 |
+
if upload_choice == "Upload Image":
|
144 |
+
uploaded_image = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg"])
|
145 |
+
if uploaded_image:
|
146 |
+
image = np.array(Image.open(uploaded_image))
|
147 |
+
frame, result_text = process_frame(image)
|
148 |
+
st.image(frame, caption='Processed Image', use_column_width=True)
|
149 |
+
st.markdown(f"<h3 style='text-align: center;'>{result_text}</h3>", unsafe_allow_html=True)
|
150 |
+
|
151 |
+
elif upload_choice == "Upload Video":
|
152 |
+
uploaded_video = st.file_uploader("Upload Video", type=["mp4", "avi", "mkv"])
|
153 |
+
if uploaded_video:
|
154 |
+
with tempfile.NamedTemporaryFile(delete=False) as tfile:
|
155 |
+
tfile.write(uploaded_video.read())
|
156 |
+
video_source = cv2.VideoCapture(tfile.name)
|
157 |
+
while True:
|
158 |
+
ret, frame = video_source.read()
|
159 |
+
if not ret:
|
160 |
+
break
|
161 |
+
frame, result_text = process_frame(frame)
|
162 |
+
st.image(frame, channels="BGR", use_column_width=True)
|
163 |
+
|
164 |
+
elif upload_choice == "Camera":
|
165 |
+
image = st.camera_input("Take a picture")
|
166 |
+
if image:
|
167 |
+
frame = np.array(Image.open(image))
|
168 |
+
frame, result_text = process_frame(frame)
|
169 |
+
st.image(frame, caption='Processed Image', use_column_width=True)
|
170 |
+
st.markdown(f"<h3 style='text-align: center;'>{result_text}</h3>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|