Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,19 @@
|
|
1 |
-
import
|
2 |
-
import streamlit as st
|
3 |
import cv2
|
4 |
import numpy as np
|
5 |
-
import
|
6 |
-
|
7 |
from datetime import datetime
|
8 |
from PIL import Image
|
|
|
9 |
|
10 |
-
#
|
11 |
DATABASE_NAME = "emotion_recognition.db"
|
12 |
KNOWN_FACES_DIR = "known_faces"
|
13 |
if not os.path.exists(KNOWN_FACES_DIR):
|
14 |
os.makedirs(KNOWN_FACES_DIR)
|
15 |
|
|
|
16 |
def init_db():
|
17 |
conn = sqlite3.connect(DATABASE_NAME)
|
18 |
cursor = conn.cursor()
|
@@ -44,20 +45,16 @@ def fetch_recent_activity():
|
|
44 |
conn.close()
|
45 |
return rows
|
46 |
|
47 |
-
# Load
|
48 |
@st.cache_resource
|
49 |
def load_emotion_model():
|
50 |
-
|
51 |
-
|
52 |
-
return model
|
53 |
-
except Exception as e:
|
54 |
-
st.error("Error loading the emotion detection model. Please ensure the file exists and is accessible.")
|
55 |
-
return None
|
56 |
|
57 |
emotion_model = load_emotion_model()
|
58 |
-
emotion_labels = ['angry', 'fear', 'happy', 'neutral', 'sad', 'surprise']
|
59 |
|
60 |
-
# Initialize LBPH
|
61 |
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
|
62 |
|
63 |
def train_recognizer():
|
@@ -65,11 +62,11 @@ def train_recognizer():
|
|
65 |
labels = []
|
66 |
for name in os.listdir(KNOWN_FACES_DIR):
|
67 |
person_dir = os.path.join(KNOWN_FACES_DIR, name)
|
68 |
-
if not os.path.isdir(person_dir):
|
69 |
continue
|
70 |
for filename in os.listdir(person_dir):
|
71 |
filepath = os.path.join(person_dir, filename)
|
72 |
-
if not filepath.lower().endswith(('.jpg', '.jpeg', '.png')):
|
73 |
continue
|
74 |
image = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
|
75 |
if image is not None:
|
@@ -83,17 +80,19 @@ def train_recognizer():
|
|
83 |
face_recognizer.train(faces, np.array(labels))
|
84 |
return label_ids_rev
|
85 |
|
|
|
86 |
|
87 |
-
# Sidebar
|
88 |
sidebar_choice = st.sidebar.selectbox("Choose an option", ["Emotion Detection", "Register New Face", "View Recent Activity"])
|
89 |
|
90 |
# Main App Logic
|
91 |
if sidebar_choice == "Register New Face":
|
92 |
st.header("Register New Face")
|
93 |
name = st.text_input("Enter Name")
|
94 |
-
|
|
|
95 |
|
96 |
-
if capture_button and name:
|
97 |
cap = cv2.VideoCapture(0)
|
98 |
st.write("Capturing face... Look into the camera.")
|
99 |
captured_faces = []
|
@@ -132,6 +131,29 @@ if sidebar_choice == "Register New Face":
|
|
132 |
else:
|
133 |
st.warning("No faces captured. Please try again.")
|
134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
elif sidebar_choice == "View Recent Activity":
|
136 |
st.header("Recent Activity")
|
137 |
logs = fetch_recent_activity()
|
@@ -141,7 +163,7 @@ elif sidebar_choice == "View Recent Activity":
|
|
141 |
else:
|
142 |
st.write("No recent activity found.")
|
143 |
|
144 |
-
else:
|
145 |
st.header("Emotion Detection with Face Recognition")
|
146 |
mode = st.radio("Choose mode", ["Image", "Camera"])
|
147 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
@@ -156,25 +178,24 @@ else: # Emotion Detection
|
|
156 |
label_id, confidence = face_recognizer.predict(face_resized)
|
157 |
label = label_ids_rev.get(label_id, "Unknown")
|
158 |
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
return frame, result_text
|
170 |
|
171 |
if mode == "Image":
|
172 |
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
173 |
if uploaded_image:
|
174 |
image = np.array(Image.open(uploaded_image))
|
175 |
-
frame,
|
176 |
-
st.image(frame, caption=
|
177 |
-
|
178 |
elif mode == "Camera":
|
179 |
cap = cv2.VideoCapture(0)
|
180 |
st.write("Press 'q' to exit.")
|
|
|
1 |
+
import os
|
|
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
+
import sqlite3
|
5 |
+
import streamlit as st
|
6 |
from datetime import datetime
|
7 |
from PIL import Image
|
8 |
+
from keras.models import load_model
|
9 |
|
10 |
+
# Constants
|
11 |
DATABASE_NAME = "emotion_recognition.db"
|
12 |
KNOWN_FACES_DIR = "known_faces"
|
13 |
if not os.path.exists(KNOWN_FACES_DIR):
|
14 |
os.makedirs(KNOWN_FACES_DIR)
|
15 |
|
16 |
+
# Initialize Database
|
17 |
def init_db():
|
18 |
conn = sqlite3.connect(DATABASE_NAME)
|
19 |
cursor = conn.cursor()
|
|
|
45 |
conn.close()
|
46 |
return rows
|
47 |
|
48 |
+
# Load Emotion Detection Model
|
49 |
@st.cache_resource
|
50 |
def load_emotion_model():
|
51 |
+
model = load_model('CNN_Model_acc_75.h5')
|
52 |
+
return model
|
|
|
|
|
|
|
|
|
53 |
|
54 |
emotion_model = load_emotion_model()
|
55 |
+
emotion_labels = ['angry', 'fear', 'happy', 'neutral', 'sad', 'surprise']
|
56 |
|
57 |
+
# Initialize LBPH Face Recognizer
|
58 |
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
|
59 |
|
60 |
def train_recognizer():
|
|
|
62 |
labels = []
|
63 |
for name in os.listdir(KNOWN_FACES_DIR):
|
64 |
person_dir = os.path.join(KNOWN_FACES_DIR, name)
|
65 |
+
if not os.path.isdir(person_dir):
|
66 |
continue
|
67 |
for filename in os.listdir(person_dir):
|
68 |
filepath = os.path.join(person_dir, filename)
|
69 |
+
if not filepath.lower().endswith(('.jpg', '.jpeg', '.png')):
|
70 |
continue
|
71 |
image = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
|
72 |
if image is not None:
|
|
|
80 |
face_recognizer.train(faces, np.array(labels))
|
81 |
return label_ids_rev
|
82 |
|
83 |
+
label_ids_rev = train_recognizer()
|
84 |
|
85 |
+
# Sidebar Options
|
86 |
sidebar_choice = st.sidebar.selectbox("Choose an option", ["Emotion Detection", "Register New Face", "View Recent Activity"])
|
87 |
|
88 |
# Main App Logic
|
89 |
if sidebar_choice == "Register New Face":
|
90 |
st.header("Register New Face")
|
91 |
name = st.text_input("Enter Name")
|
92 |
+
use_camera = st.checkbox("Use Camera to Capture Face")
|
93 |
+
capture_button = st.button("Capture Face" if use_camera else "Upload Image")
|
94 |
|
95 |
+
if use_camera and capture_button and name:
|
96 |
cap = cv2.VideoCapture(0)
|
97 |
st.write("Capturing face... Look into the camera.")
|
98 |
captured_faces = []
|
|
|
131 |
else:
|
132 |
st.warning("No faces captured. Please try again.")
|
133 |
|
134 |
+
elif not use_camera and capture_button and name:
|
135 |
+
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
136 |
+
if uploaded_image:
|
137 |
+
image = np.array(Image.open(uploaded_image))
|
138 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
139 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
140 |
+
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(100, 100))
|
141 |
+
|
142 |
+
if len(faces) > 0:
|
143 |
+
person_dir = os.path.join(KNOWN_FACES_DIR, name)
|
144 |
+
if not os.path.exists(person_dir):
|
145 |
+
os.makedirs(person_dir)
|
146 |
+
for (x, y, w, h) in faces:
|
147 |
+
face_roi = gray_image[y:y + h, x:x + w]
|
148 |
+
face_filename = os.path.join(person_dir, f"{name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg")
|
149 |
+
cv2.imwrite(face_filename, face_roi)
|
150 |
+
label_ids_rev = train_recognizer()
|
151 |
+
st.success(f"Face for {name} has been registered successfully!")
|
152 |
+
else:
|
153 |
+
st.warning("No face detected. Try another image.")
|
154 |
+
else:
|
155 |
+
st.warning("Please upload an image.")
|
156 |
+
|
157 |
elif sidebar_choice == "View Recent Activity":
|
158 |
st.header("Recent Activity")
|
159 |
logs = fetch_recent_activity()
|
|
|
163 |
else:
|
164 |
st.write("No recent activity found.")
|
165 |
|
166 |
+
else:
|
167 |
st.header("Emotion Detection with Face Recognition")
|
168 |
mode = st.radio("Choose mode", ["Image", "Camera"])
|
169 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
|
|
178 |
label_id, confidence = face_recognizer.predict(face_resized)
|
179 |
label = label_ids_rev.get(label_id, "Unknown")
|
180 |
|
181 |
+
# Emotion Detection
|
182 |
+
face_color = cv2.resize(frame[y:y + h, x:x + w], (48, 48)) / 255.0
|
183 |
+
face_color = np.expand_dims(cv2.cvtColor(face_color, cv2.COLOR_BGR2RGB), axis=0)
|
184 |
+
emotion_prediction = emotion_model.predict(face_color)
|
185 |
+
emotion = emotion_labels[np.argmax(emotion_prediction[0])]
|
186 |
|
187 |
+
log_attendance(label, emotion)
|
188 |
+
result_text = f"{label} is feeling {emotion}"
|
189 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
190 |
+
cv2.putText(frame, result_text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
191 |
return frame, result_text
|
192 |
|
193 |
if mode == "Image":
|
194 |
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
195 |
if uploaded_image:
|
196 |
image = np.array(Image.open(uploaded_image))
|
197 |
+
frame, result_text = process_frame(image)
|
198 |
+
st.image(frame, caption=result_text)
|
|
|
199 |
elif mode == "Camera":
|
200 |
cap = cv2.VideoCapture(0)
|
201 |
st.write("Press 'q' to exit.")
|