Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ import cv2
|
|
7 |
from keras.models import load_model
|
8 |
from datetime import datetime
|
9 |
from huggingface_hub import HfApi
|
|
|
10 |
|
11 |
# Constants
|
12 |
KNOWN_FACES_DIR = "known_faces" # Directory to save user images
|
@@ -88,30 +89,65 @@ def save_image_to_hugging_face(image, name, roll_no):
|
|
88 |
|
89 |
return local_path
|
90 |
|
91 |
-
# Process each frame for emotion detection
|
92 |
-
def process_frame(frame):
|
93 |
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
94 |
faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
95 |
|
|
|
|
|
|
|
96 |
for (x, y, w, h) in faces:
|
97 |
-
roi_gray = gray_frame[y:y+h, x:x+w]
|
98 |
roi_color = frame[y:y+h, x:x+w]
|
|
|
|
|
|
|
99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
face_roi = cv2.resize(roi_color, (48, 48))
|
101 |
face_roi = np.expand_dims(face_roi, axis=0)
|
102 |
face_roi = face_roi / float(48)
|
103 |
predictions = model.predict(face_roi)
|
104 |
emotion = emotion_labels[np.argmax(predictions[0])]
|
105 |
|
106 |
-
# Display emotion text on face
|
107 |
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
108 |
-
cv2.putText(frame, emotion, (x, y+h), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
109 |
|
110 |
return frame
|
111 |
|
112 |
# User Interface for registration
|
113 |
st.title("Student Registration and Attendance")
|
114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
# Choose input method for the image (webcam or file upload)
|
116 |
capture_mode = st.radio("Choose an option to upload your image", ["Use Webcam", "Upload File"])
|
117 |
|
@@ -144,22 +180,28 @@ if st.button("Register"):
|
|
144 |
# Save user data to the database
|
145 |
save_to_database(name, roll_no, image_path)
|
146 |
|
147 |
-
#
|
148 |
-
|
149 |
-
|
150 |
-
ret, frame = cap.read()
|
151 |
-
if not ret:
|
152 |
-
break
|
153 |
-
|
154 |
-
frame = process_frame(frame)
|
155 |
-
st.image(frame, channels="BGR", use_column_width=True)
|
156 |
-
break # Stop after capturing one frame
|
157 |
|
158 |
-
|
159 |
|
160 |
except Exception as e:
|
161 |
st.error(f"An error occurred: {e}")
|
162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
# Display registered students and attendance history
|
164 |
if st.checkbox("Show registered students"):
|
165 |
conn = sqlite3.connect(DATABASE)
|
|
|
7 |
from keras.models import load_model
|
8 |
from datetime import datetime
|
9 |
from huggingface_hub import HfApi
|
10 |
+
import face_recognition
|
11 |
|
12 |
# Constants
|
13 |
KNOWN_FACES_DIR = "known_faces" # Directory to save user images
|
|
|
89 |
|
90 |
return local_path
|
91 |
|
92 |
+
# Process each frame for emotion detection and face recognition
|
93 |
+
def process_frame(frame, known_face_encodings, known_face_names):
|
94 |
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
95 |
faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
96 |
|
97 |
+
face_locations = []
|
98 |
+
face_encodings = []
|
99 |
+
face_names = []
|
100 |
for (x, y, w, h) in faces:
|
|
|
101 |
roi_color = frame[y:y+h, x:x+w]
|
102 |
+
# Face recognition
|
103 |
+
rgb_frame = frame[:, :, ::-1] # Convert to RGB (face_recognition expects RGB)
|
104 |
+
face_encoding = face_recognition.face_encodings(rgb_frame, [(y, x + w, y + h, x)])[0]
|
105 |
|
106 |
+
# Check if the detected face matches any known face
|
107 |
+
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
|
108 |
+
name = "Unknown"
|
109 |
+
|
110 |
+
if True in matches:
|
111 |
+
first_match_index = matches.index(True)
|
112 |
+
name = known_face_names[first_match_index]
|
113 |
+
|
114 |
+
# Emotion detection
|
115 |
face_roi = cv2.resize(roi_color, (48, 48))
|
116 |
face_roi = np.expand_dims(face_roi, axis=0)
|
117 |
face_roi = face_roi / float(48)
|
118 |
predictions = model.predict(face_roi)
|
119 |
emotion = emotion_labels[np.argmax(predictions[0])]
|
120 |
|
121 |
+
# Display name and emotion text on face
|
122 |
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
123 |
+
cv2.putText(frame, f"{name} - {emotion}", (x, y+h), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
124 |
|
125 |
return frame
|
126 |
|
127 |
# User Interface for registration
|
128 |
st.title("Student Registration and Attendance")
|
129 |
|
130 |
+
# Initialize the database and create the table if it does not exist
|
131 |
+
initialize_database()
|
132 |
+
|
133 |
+
# Load known faces from the database
|
134 |
+
known_face_encodings = []
|
135 |
+
known_face_names = []
|
136 |
+
|
137 |
+
conn = sqlite3.connect(DATABASE)
|
138 |
+
cursor = conn.cursor()
|
139 |
+
cursor.execute("SELECT name, image_path FROM students")
|
140 |
+
rows = cursor.fetchall()
|
141 |
+
|
142 |
+
for row in rows:
|
143 |
+
name, image_path = row
|
144 |
+
image = face_recognition.load_image_file(image_path)
|
145 |
+
encoding = face_recognition.face_encodings(image)[0]
|
146 |
+
known_face_encodings.append(encoding)
|
147 |
+
known_face_names.append(name)
|
148 |
+
|
149 |
+
conn.close()
|
150 |
+
|
151 |
# Choose input method for the image (webcam or file upload)
|
152 |
capture_mode = st.radio("Choose an option to upload your image", ["Use Webcam", "Upload File"])
|
153 |
|
|
|
180 |
# Save user data to the database
|
181 |
save_to_database(name, roll_no, image_path)
|
182 |
|
183 |
+
# Update the known faces list
|
184 |
+
known_face_encodings.append(face_recognition.face_encodings(image)[0])
|
185 |
+
known_face_names.append(name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
|
187 |
+
st.success(f"Student {name} registered successfully!")
|
188 |
|
189 |
except Exception as e:
|
190 |
st.error(f"An error occurred: {e}")
|
191 |
|
192 |
+
# Detect faces and emotions from webcam
|
193 |
+
cap = cv2.VideoCapture(0)
|
194 |
+
while True:
|
195 |
+
ret, frame = cap.read()
|
196 |
+
if not ret:
|
197 |
+
break
|
198 |
+
|
199 |
+
frame = process_frame(frame, known_face_encodings, known_face_names)
|
200 |
+
st.image(frame, channels="BGR", use_column_width=True)
|
201 |
+
break # Stop after capturing one frame
|
202 |
+
|
203 |
+
cap.release()
|
204 |
+
|
205 |
# Display registered students and attendance history
|
206 |
if st.checkbox("Show registered students"):
|
207 |
conn = sqlite3.connect(DATABASE)
|