Update app.py
Browse files
app.py
CHANGED
@@ -43,46 +43,38 @@ except Exception as e:
|
|
43 |
# Database Functions
|
44 |
def initialize_database():
|
45 |
""" Initializes the SQLite database by creating the students table if it doesn't exist. """
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
conn.close()
|
59 |
|
60 |
def save_to_database(name, roll_no, image_path):
|
61 |
""" Saves the student's data to the database. """
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
finally:
|
74 |
-
conn.close()
|
75 |
|
76 |
def save_image_to_hugging_face(image, name, roll_no):
|
77 |
""" Saves the image locally to the KNOWN_FACES_DIR and uploads it to Hugging Face. """
|
78 |
-
# Ensure the directory exists
|
79 |
-
if not os.path.exists(KNOWN_FACES_DIR):
|
80 |
-
os.makedirs(KNOWN_FACES_DIR)
|
81 |
-
|
82 |
-
# Construct the local file path
|
83 |
filename = f"{name}_{roll_no}.jpg"
|
84 |
local_path = os.path.join(KNOWN_FACES_DIR, filename)
|
85 |
-
|
86 |
try:
|
87 |
# Convert image to RGB if necessary
|
88 |
if image.mode != "RGB":
|
@@ -90,7 +82,7 @@ def save_image_to_hugging_face(image, name, roll_no):
|
|
90 |
|
91 |
# Save the image to the known_faces directory
|
92 |
image.save(local_path)
|
93 |
-
|
94 |
# Upload the saved file to Hugging Face
|
95 |
api.upload_file(
|
96 |
path_or_fileobj=local_path,
|
@@ -106,7 +98,6 @@ def save_image_to_hugging_face(image, name, roll_no):
|
|
106 |
return local_path
|
107 |
|
108 |
|
109 |
-
|
110 |
# Initialize the database when the app starts
|
111 |
initialize_database()
|
112 |
|
@@ -148,11 +139,10 @@ if st.button("Register"):
|
|
148 |
|
149 |
# Display registered student data
|
150 |
if st.checkbox("Show registered students"):
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
conn.close()
|
156 |
|
157 |
st.write("### Registered Students")
|
158 |
for row in rows:
|
@@ -166,6 +156,7 @@ def detect_faces_and_emotions(image):
|
|
166 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
167 |
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.3, minNeighbors=5)
|
168 |
|
|
|
169 |
for (x, y, w, h) in faces:
|
170 |
face = gray_image[y:y+h, x:x+w]
|
171 |
resized_face = cv2.resize(face, (48, 48)) # Resize face to 48x48
|
@@ -176,8 +167,9 @@ def detect_faces_and_emotions(image):
|
|
176 |
# Predict the emotion
|
177 |
emotion_prediction = emotion_model.predict(reshaped_face)
|
178 |
emotion_label = np.argmax(emotion_prediction)
|
179 |
-
|
180 |
-
|
|
|
181 |
|
182 |
# UI for Emotion Detection
|
183 |
if st.sidebar.selectbox("Menu", ["Register Student", "Face Recognition and Emotion Detection", "View Attendance"]) == "Face Recognition and Emotion Detection":
|
@@ -189,9 +181,9 @@ if st.sidebar.selectbox("Menu", ["Register Student", "Face Recognition and Emoti
|
|
189 |
if uploaded_file:
|
190 |
img = Image.open(uploaded_file)
|
191 |
img_array = np.array(img)
|
192 |
-
|
193 |
-
if
|
194 |
-
st.success(f"
|
195 |
else:
|
196 |
st.warning("No face detected.")
|
197 |
|
@@ -201,8 +193,8 @@ if st.sidebar.selectbox("Menu", ["Register Student", "Face Recognition and Emoti
|
|
201 |
if camera_image:
|
202 |
img = Image.open(camera_image)
|
203 |
img_array = np.array(img)
|
204 |
-
|
205 |
-
if
|
206 |
-
st.success(f"
|
207 |
else:
|
208 |
st.warning("No face detected.")
|
|
|
43 |
# Database Functions
|
44 |
def initialize_database():
|
45 |
""" Initializes the SQLite database by creating the students table if it doesn't exist. """
|
46 |
+
with sqlite3.connect(DATABASE) as conn:
|
47 |
+
cursor = conn.cursor()
|
48 |
+
cursor.execute("""
|
49 |
+
CREATE TABLE IF NOT EXISTS students (
|
50 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
51 |
+
name TEXT NOT NULL,
|
52 |
+
roll_no TEXT NOT NULL UNIQUE,
|
53 |
+
image_path TEXT NOT NULL,
|
54 |
+
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
|
55 |
+
)
|
56 |
+
""")
|
57 |
+
conn.commit()
|
|
|
58 |
|
59 |
def save_to_database(name, roll_no, image_path):
|
60 |
""" Saves the student's data to the database. """
|
61 |
+
with sqlite3.connect(DATABASE) as conn:
|
62 |
+
cursor = conn.cursor()
|
63 |
+
try:
|
64 |
+
cursor.execute("""
|
65 |
+
INSERT INTO students (name, roll_no, image_path)
|
66 |
+
VALUES (?, ?, ?)
|
67 |
+
""", (name, roll_no, image_path))
|
68 |
+
conn.commit()
|
69 |
+
st.success("Data saved successfully!")
|
70 |
+
except sqlite3.IntegrityError:
|
71 |
+
st.error("Roll number already exists!")
|
|
|
|
|
72 |
|
73 |
def save_image_to_hugging_face(image, name, roll_no):
|
74 |
""" Saves the image locally to the KNOWN_FACES_DIR and uploads it to Hugging Face. """
|
|
|
|
|
|
|
|
|
|
|
75 |
filename = f"{name}_{roll_no}.jpg"
|
76 |
local_path = os.path.join(KNOWN_FACES_DIR, filename)
|
77 |
+
|
78 |
try:
|
79 |
# Convert image to RGB if necessary
|
80 |
if image.mode != "RGB":
|
|
|
82 |
|
83 |
# Save the image to the known_faces directory
|
84 |
image.save(local_path)
|
85 |
+
|
86 |
# Upload the saved file to Hugging Face
|
87 |
api.upload_file(
|
88 |
path_or_fileobj=local_path,
|
|
|
98 |
return local_path
|
99 |
|
100 |
|
|
|
101 |
# Initialize the database when the app starts
|
102 |
initialize_database()
|
103 |
|
|
|
139 |
|
140 |
# Display registered student data
|
141 |
if st.checkbox("Show registered students"):
|
142 |
+
with sqlite3.connect(DATABASE) as conn:
|
143 |
+
cursor = conn.cursor()
|
144 |
+
cursor.execute("SELECT name, roll_no, image_path, timestamp FROM students")
|
145 |
+
rows = cursor.fetchall()
|
|
|
146 |
|
147 |
st.write("### Registered Students")
|
148 |
for row in rows:
|
|
|
156 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
157 |
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.3, minNeighbors=5)
|
158 |
|
159 |
+
emotion_labels = []
|
160 |
for (x, y, w, h) in faces:
|
161 |
face = gray_image[y:y+h, x:x+w]
|
162 |
resized_face = cv2.resize(face, (48, 48)) # Resize face to 48x48
|
|
|
167 |
# Predict the emotion
|
168 |
emotion_prediction = emotion_model.predict(reshaped_face)
|
169 |
emotion_label = np.argmax(emotion_prediction)
|
170 |
+
emotion_labels.append(EMOTION_LABELS[emotion_label])
|
171 |
+
|
172 |
+
return emotion_labels if emotion_labels else None
|
173 |
|
174 |
# UI for Emotion Detection
|
175 |
if st.sidebar.selectbox("Menu", ["Register Student", "Face Recognition and Emotion Detection", "View Attendance"]) == "Face Recognition and Emotion Detection":
|
|
|
181 |
if uploaded_file:
|
182 |
img = Image.open(uploaded_file)
|
183 |
img_array = np.array(img)
|
184 |
+
emotion_labels = detect_faces_and_emotions(img_array)
|
185 |
+
if emotion_labels:
|
186 |
+
st.success(f"Emotions Detected: {', '.join(emotion_labels)}")
|
187 |
else:
|
188 |
st.warning("No face detected.")
|
189 |
|
|
|
193 |
if camera_image:
|
194 |
img = Image.open(camera_image)
|
195 |
img_array = np.array(img)
|
196 |
+
emotion_labels = detect_faces_and_emotions(img_array)
|
197 |
+
if emotion_labels:
|
198 |
+
st.success(f"Emotions Detected: {', '.join(emotion_labels)}")
|
199 |
else:
|
200 |
st.warning("No face detected.")
|