Update app.py
Browse files
app.py
CHANGED
@@ -47,11 +47,15 @@ def fetch_recent_activity():
|
|
47 |
# Load pre-trained emotion detection model
|
48 |
@st.cache_resource
|
49 |
def load_emotion_model():
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
52 |
|
53 |
emotion_model = load_emotion_model()
|
54 |
-
emotion_labels = ['angry', 'fear', 'happy', 'neutral', 'sad', 'surprise']
|
55 |
|
56 |
# Initialize LBPH face recognizer
|
57 |
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
|
@@ -63,12 +67,14 @@ def train_recognizer():
|
|
63 |
for filename in os.listdir(os.path.join(KNOWN_FACES_DIR, name)):
|
64 |
filepath = os.path.join(KNOWN_FACES_DIR, name, filename)
|
65 |
image = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
|
66 |
-
|
67 |
-
|
|
|
68 |
label_ids = {name: idx for idx, name in enumerate(set(labels))}
|
69 |
label_ids_rev = {idx: name for name, idx in label_ids.items()}
|
70 |
labels = [label_ids[label] for label in labels]
|
71 |
-
|
|
|
72 |
return label_ids_rev
|
73 |
|
74 |
label_ids_rev = train_recognizer()
|
@@ -80,38 +86,46 @@ sidebar_choice = st.sidebar.selectbox("Choose an option", ["Emotion Detection",
|
|
80 |
if sidebar_choice == "Register New Face":
|
81 |
st.header("Register New Face")
|
82 |
name = st.text_input("Enter Name")
|
83 |
-
capture_button = st.button("Capture Face
|
|
|
84 |
if capture_button and name:
|
85 |
cap = cv2.VideoCapture(0)
|
86 |
st.write("Capturing face... Look into the camera.")
|
87 |
captured_faces = []
|
88 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
89 |
|
90 |
-
while len(captured_faces) < 5:
|
91 |
ret, frame = cap.read()
|
92 |
if not ret:
|
93 |
st.error("Error capturing video")
|
94 |
break
|
|
|
95 |
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
96 |
faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(100, 100))
|
|
|
97 |
for (x, y, w, h) in faces:
|
98 |
face_roi = gray_frame[y:y + h, x:x + w]
|
99 |
captured_faces.append(face_roi)
|
100 |
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
|
|
|
101 |
cv2.imshow("Face Registration", frame)
|
102 |
if cv2.waitKey(1) & 0xFF == ord('q'):
|
103 |
break
|
|
|
104 |
cap.release()
|
105 |
cv2.destroyAllWindows()
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
115 |
|
116 |
elif sidebar_choice == "View Recent Activity":
|
117 |
st.header("Recent Activity")
|
@@ -137,51 +151,24 @@ else: # Emotion Detection
|
|
137 |
label_id, confidence = face_recognizer.predict(face_resized)
|
138 |
label = label_ids_rev.get(label_id, "Unknown")
|
139 |
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
# Log Attendance
|
147 |
-
log_attendance(label, emotion)
|
148 |
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
return frame, result_text
|
154 |
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
image = np.array(Image.open(uploaded_image))
|
162 |
-
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
163 |
-
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(100, 100))
|
164 |
-
|
165 |
-
if len(faces) > 0:
|
166 |
-
for (x, y, w, h) in faces:
|
167 |
-
face_roi = gray_image[y:y + h, x:x + w]
|
168 |
-
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) # Annotate detected face
|
169 |
-
|
170 |
-
# Save face if user provides a name
|
171 |
-
if register_image_button and name_for_image:
|
172 |
-
person_dir = os.path.join(KNOWN_FACES_DIR, name_for_image)
|
173 |
-
if not os.path.exists(person_dir):
|
174 |
-
os.makedirs(person_dir)
|
175 |
-
face_filename = os.path.join(person_dir, f"{name_for_image}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg")
|
176 |
-
cv2.imwrite(face_filename, face_roi)
|
177 |
-
st.success(f"Face for {name_for_image} has been saved successfully!")
|
178 |
-
label_ids_rev = train_recognizer() # Retrain recognizer after adding new face
|
179 |
-
|
180 |
-
else:
|
181 |
-
st.warning("No face detected in the uploaded image. Please try another image.")
|
182 |
-
|
183 |
-
st.image(image, caption="Processed Image with Face Annotations")
|
184 |
-
|
185 |
|
186 |
elif mode == "Camera":
|
187 |
cap = cv2.VideoCapture(0)
|
|
|
47 |
# Load pre-trained emotion detection model
|
48 |
@st.cache_resource
|
49 |
def load_emotion_model():
|
50 |
+
try:
|
51 |
+
model = load_model('CNN_Model_acc_75.h5')
|
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'] if emotion_model else []
|
59 |
|
60 |
# Initialize LBPH face recognizer
|
61 |
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
|
|
|
67 |
for filename in os.listdir(os.path.join(KNOWN_FACES_DIR, name)):
|
68 |
filepath = os.path.join(KNOWN_FACES_DIR, name, filename)
|
69 |
image = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
|
70 |
+
if image is not None:
|
71 |
+
faces.append(image)
|
72 |
+
labels.append(name)
|
73 |
label_ids = {name: idx for idx, name in enumerate(set(labels))}
|
74 |
label_ids_rev = {idx: name for name, idx in label_ids.items()}
|
75 |
labels = [label_ids[label] for label in labels]
|
76 |
+
if faces:
|
77 |
+
face_recognizer.train(faces, np.array(labels))
|
78 |
return label_ids_rev
|
79 |
|
80 |
label_ids_rev = train_recognizer()
|
|
|
86 |
if sidebar_choice == "Register New Face":
|
87 |
st.header("Register New Face")
|
88 |
name = st.text_input("Enter Name")
|
89 |
+
capture_button = st.button("Capture Face")
|
90 |
+
|
91 |
if capture_button and name:
|
92 |
cap = cv2.VideoCapture(0)
|
93 |
st.write("Capturing face... Look into the camera.")
|
94 |
captured_faces = []
|
95 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
96 |
|
97 |
+
while len(captured_faces) < 5: # Capture 5 images
|
98 |
ret, frame = cap.read()
|
99 |
if not ret:
|
100 |
st.error("Error capturing video")
|
101 |
break
|
102 |
+
|
103 |
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
104 |
faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(100, 100))
|
105 |
+
|
106 |
for (x, y, w, h) in faces:
|
107 |
face_roi = gray_frame[y:y + h, x:x + w]
|
108 |
captured_faces.append(face_roi)
|
109 |
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
|
110 |
+
|
111 |
cv2.imshow("Face Registration", frame)
|
112 |
if cv2.waitKey(1) & 0xFF == ord('q'):
|
113 |
break
|
114 |
+
|
115 |
cap.release()
|
116 |
cv2.destroyAllWindows()
|
117 |
|
118 |
+
if captured_faces:
|
119 |
+
person_dir = os.path.join(KNOWN_FACES_DIR, name)
|
120 |
+
if not os.path.exists(person_dir):
|
121 |
+
os.makedirs(person_dir)
|
122 |
+
for i, face in enumerate(captured_faces):
|
123 |
+
cv2.imwrite(os.path.join(person_dir, f"{name}_{i}.jpg"), face)
|
124 |
+
|
125 |
+
label_ids_rev = train_recognizer()
|
126 |
+
st.success(f"{name} has been registered successfully with {len(captured_faces)} captured images!")
|
127 |
+
else:
|
128 |
+
st.warning("No faces captured. Please try again.")
|
129 |
|
130 |
elif sidebar_choice == "View Recent Activity":
|
131 |
st.header("Recent Activity")
|
|
|
151 |
label_id, confidence = face_recognizer.predict(face_resized)
|
152 |
label = label_ids_rev.get(label_id, "Unknown")
|
153 |
|
154 |
+
if emotion_model:
|
155 |
+
face_color = cv2.resize(frame[y:y + h, x:x + w], (48, 48)) / 255.0
|
156 |
+
face_color = np.expand_dims(cv2.cvtColor(face_color, cv2.COLOR_BGR2RGB), axis=0)
|
157 |
+
emotion_prediction = emotion_model.predict(face_color)
|
158 |
+
emotion = emotion_labels[np.argmax(emotion_prediction[0])]
|
|
|
|
|
|
|
159 |
|
160 |
+
log_attendance(label, emotion)
|
161 |
+
result_text = f"{label} is feeling {emotion}"
|
162 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
163 |
+
cv2.putText(frame, result_text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
164 |
return frame, result_text
|
165 |
|
166 |
+
if mode == "Image":
|
167 |
+
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
168 |
+
if uploaded_image:
|
169 |
+
image = np.array(Image.open(uploaded_image))
|
170 |
+
frame, _ = process_frame(image)
|
171 |
+
st.image(frame, caption="Processed Image with Face Annotations")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
elif mode == "Camera":
|
174 |
cap = cv2.VideoCapture(0)
|