Update app.py
Browse files
app.py
CHANGED
@@ -61,6 +61,11 @@ def insert_student(name, roll_number, image_path):
|
|
61 |
except sqlite3.IntegrityError:
|
62 |
st.warning("Roll number already exists!")
|
63 |
|
|
|
|
|
|
|
|
|
|
|
64 |
# Face and Emotion Detection
|
65 |
def detect_faces_and_emotions(image):
|
66 |
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
@@ -69,11 +74,15 @@ def detect_faces_and_emotions(image):
|
|
69 |
|
70 |
for (x, y, w, h) in faces:
|
71 |
face = gray_image[y:y+h, x:x+w]
|
72 |
-
resized_face = cv2.resize(face, (48, 48))
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
75 |
emotion_prediction = emotion_model.predict(reshaped_face)
|
76 |
-
emotion_label = np.argmax(emotion_prediction)
|
77 |
return EMOTION_LABELS[emotion_label]
|
78 |
return None
|
79 |
|
@@ -81,7 +90,7 @@ def detect_faces_and_emotions(image):
|
|
81 |
st.title("Student Registration and Emotion Detection")
|
82 |
create_table()
|
83 |
|
84 |
-
menu = ["Register Student", "Face Recognition and Emotion Detection"]
|
85 |
choice = st.sidebar.selectbox("Menu", menu)
|
86 |
|
87 |
if choice == "Register Student":
|
@@ -91,20 +100,38 @@ if choice == "Register Student":
|
|
91 |
name = st.text_input("Name")
|
92 |
roll_number = st.text_input("Roll Number")
|
93 |
image_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
|
|
|
94 |
submitted = st.form_submit_button("Register")
|
95 |
|
96 |
if submitted:
|
97 |
-
if name and roll_number
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
else:
|
107 |
-
st.warning("Please fill in all fields
|
108 |
|
109 |
elif choice == "Face Recognition and Emotion Detection":
|
110 |
st.subheader("Recognize Faces and Detect Emotions")
|
@@ -139,3 +166,13 @@ elif choice == "Face Recognition and Emotion Detection":
|
|
139 |
st.warning("No face detected.")
|
140 |
except Exception as e:
|
141 |
st.error(f"Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
except sqlite3.IntegrityError:
|
62 |
st.warning("Roll number already exists!")
|
63 |
|
64 |
+
def get_all_students():
|
65 |
+
with sqlite3.connect(DB_FILE) as conn:
|
66 |
+
cursor = conn.execute("SELECT * FROM students")
|
67 |
+
return cursor.fetchall()
|
68 |
+
|
69 |
# Face and Emotion Detection
|
70 |
def detect_faces_and_emotions(image):
|
71 |
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
74 |
|
75 |
for (x, y, w, h) in faces:
|
76 |
face = gray_image[y:y+h, x:x+w]
|
77 |
+
resized_face = cv2.resize(face, (48, 48)) # Resize face to 48x48
|
78 |
+
# Convert grayscale to RGB by duplicating the channel
|
79 |
+
rgb_face = cv2.cvtColor(resized_face, cv2.COLOR_GRAY2RGB) # Convert grayscale to RGB
|
80 |
+
normalized_face = rgb_face / 255.0 # Normalize pixel values to [0, 1]
|
81 |
+
reshaped_face = np.reshape(normalized_face, (1, 48, 48, 3)) # Ensure the input shape is (1, 48, 48, 3)
|
82 |
+
|
83 |
+
# Predict the emotion
|
84 |
emotion_prediction = emotion_model.predict(reshaped_face)
|
85 |
+
emotion_label = np.argmax(emotion_prediction) # Get the index of the highest probability
|
86 |
return EMOTION_LABELS[emotion_label]
|
87 |
return None
|
88 |
|
|
|
90 |
st.title("Student Registration and Emotion Detection")
|
91 |
create_table()
|
92 |
|
93 |
+
menu = ["Register Student", "Face Recognition and Emotion Detection", "View Attendance"]
|
94 |
choice = st.sidebar.selectbox("Menu", menu)
|
95 |
|
96 |
if choice == "Register Student":
|
|
|
100 |
name = st.text_input("Name")
|
101 |
roll_number = st.text_input("Roll Number")
|
102 |
image_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
|
103 |
+
use_webcam = st.checkbox("Use Webcam for Face Registration")
|
104 |
submitted = st.form_submit_button("Register")
|
105 |
|
106 |
if submitted:
|
107 |
+
if name and roll_number:
|
108 |
+
if use_webcam:
|
109 |
+
st.info("Use the camera input widget to capture an image.")
|
110 |
+
camera_image = st.camera_input("Capture Image")
|
111 |
+
if camera_image:
|
112 |
+
try:
|
113 |
+
img = Image.open(camera_image)
|
114 |
+
img = img.convert("RGB")
|
115 |
+
img_path = os.path.join(KNOWN_FACES_DIR, f"{roll_number}.png")
|
116 |
+
img.save(img_path)
|
117 |
+
insert_student(name, roll_number, img_path)
|
118 |
+
st.success(f"Student {name} Registered Successfully!")
|
119 |
+
except Exception as e:
|
120 |
+
st.error(f"Error: {e}")
|
121 |
+
elif image_file:
|
122 |
+
try:
|
123 |
+
img = Image.open(image_file)
|
124 |
+
img = img.convert("RGB")
|
125 |
+
img_path = os.path.join(KNOWN_FACES_DIR, f"{roll_number}.png")
|
126 |
+
img.save(img_path)
|
127 |
+
insert_student(name, roll_number, img_path)
|
128 |
+
st.success(f"Student {name} Registered Successfully!")
|
129 |
+
except Exception as e:
|
130 |
+
st.error(f"Error: {e}")
|
131 |
+
else:
|
132 |
+
st.warning("Please upload an image or use the webcam to register the face.")
|
133 |
else:
|
134 |
+
st.warning("Please fill in all fields.")
|
135 |
|
136 |
elif choice == "Face Recognition and Emotion Detection":
|
137 |
st.subheader("Recognize Faces and Detect Emotions")
|
|
|
166 |
st.warning("No face detected.")
|
167 |
except Exception as e:
|
168 |
st.error(f"Error: {e}")
|
169 |
+
|
170 |
+
elif choice == "View Attendance":
|
171 |
+
st.subheader("View Registered Students (Attendance)")
|
172 |
+
|
173 |
+
students = get_all_students()
|
174 |
+
if students:
|
175 |
+
for student in students:
|
176 |
+
st.write(f"Name: {student[1]}, Roll Number: {student[2]}")
|
177 |
+
else:
|
178 |
+
st.warning("No students registered.")
|