Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,6 @@ import numpy as np
|
|
5 |
from keras.models import load_model
|
6 |
from PIL import Image
|
7 |
import sqlite3
|
8 |
-
from datetime import datetime
|
9 |
from huggingface_hub import HfApi
|
10 |
|
11 |
# Constants
|
@@ -43,16 +42,11 @@ except Exception as e:
|
|
43 |
# Database Functions
|
44 |
def create_table():
|
45 |
with sqlite3.connect(DB_FILE) as conn:
|
46 |
-
conn.execute("""
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
image_path TEXT NOT NULL,
|
52 |
-
emotion TEXT,
|
53 |
-
timestamp TEXT
|
54 |
-
)
|
55 |
-
""")
|
56 |
conn.commit()
|
57 |
|
58 |
def insert_student(name, roll_number, image_path):
|
@@ -64,16 +58,6 @@ def insert_student(name, roll_number, image_path):
|
|
64 |
except sqlite3.IntegrityError:
|
65 |
st.warning("Roll number already exists!")
|
66 |
|
67 |
-
def insert_attendance(roll_number, emotion):
|
68 |
-
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
69 |
-
with sqlite3.connect(DB_FILE) as conn:
|
70 |
-
conn.execute("""
|
71 |
-
UPDATE students
|
72 |
-
SET emotion = ?, timestamp = ?
|
73 |
-
WHERE roll_number = ?
|
74 |
-
""", (emotion, timestamp, roll_number))
|
75 |
-
conn.commit()
|
76 |
-
|
77 |
def get_all_students():
|
78 |
with sqlite3.connect(DB_FILE) as conn:
|
79 |
cursor = conn.execute("SELECT * FROM students")
|
@@ -130,7 +114,7 @@ if choice == "Register Student":
|
|
130 |
insert_student(name, roll_number, img_path)
|
131 |
st.success(f"Student {name} Registered Successfully!")
|
132 |
except Exception as e:
|
133 |
-
st.error(f"Error: {e}")
|
134 |
elif image_file:
|
135 |
try:
|
136 |
img = Image.open(image_file)
|
@@ -140,7 +124,7 @@ if choice == "Register Student":
|
|
140 |
insert_student(name, roll_number, img_path)
|
141 |
st.success(f"Student {name} Registered Successfully!")
|
142 |
except Exception as e:
|
143 |
-
st.error(f"Error: {e}")
|
144 |
else:
|
145 |
st.warning("Please upload an image or use the webcam to register the face.")
|
146 |
else:
|
@@ -160,14 +144,6 @@ elif choice == "Face Recognition and Emotion Detection":
|
|
160 |
emotion_label = detect_faces_and_emotions(img_array)
|
161 |
if emotion_label:
|
162 |
st.success(f"Emotion Detected: {emotion_label}")
|
163 |
-
# Update the attendance
|
164 |
-
roll_number = st.text_input("Enter Roll Number for Attendance")
|
165 |
-
if st.button("Record Attendance"):
|
166 |
-
if roll_number:
|
167 |
-
insert_attendance(roll_number, emotion_label)
|
168 |
-
st.success(f"Attendance recorded for Roll No: {roll_number}")
|
169 |
-
else:
|
170 |
-
st.warning("Please enter the roll number.")
|
171 |
else:
|
172 |
st.warning("No face detected.")
|
173 |
except Exception as e:
|
@@ -183,14 +159,6 @@ elif choice == "Face Recognition and Emotion Detection":
|
|
183 |
emotion_label = detect_faces_and_emotions(img_array)
|
184 |
if emotion_label:
|
185 |
st.success(f"Emotion Detected: {emotion_label}")
|
186 |
-
# Update the attendance
|
187 |
-
roll_number = st.text_input("Enter Roll Number for Attendance")
|
188 |
-
if st.button("Record Attendance"):
|
189 |
-
if roll_number:
|
190 |
-
insert_attendance(roll_number, emotion_label)
|
191 |
-
st.success(f"Attendance recorded for Roll No: {roll_number}")
|
192 |
-
else:
|
193 |
-
st.warning("Please enter the roll number.")
|
194 |
else:
|
195 |
st.warning("No face detected.")
|
196 |
except Exception as e:
|
@@ -202,6 +170,6 @@ elif choice == "View Attendance":
|
|
202 |
students = get_all_students()
|
203 |
if students:
|
204 |
for student in students:
|
205 |
-
st.write(f"Name: {student[1]}, Roll Number: {student[2]}
|
206 |
else:
|
207 |
st.warning("No students registered.")
|
|
|
5 |
from keras.models import load_model
|
6 |
from PIL import Image
|
7 |
import sqlite3
|
|
|
8 |
from huggingface_hub import HfApi
|
9 |
|
10 |
# Constants
|
|
|
42 |
# Database Functions
|
43 |
def create_table():
|
44 |
with sqlite3.connect(DB_FILE) as conn:
|
45 |
+
conn.execute("""CREATE TABLE IF NOT EXISTS students (
|
46 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
47 |
+
name TEXT NOT NULL,
|
48 |
+
roll_number TEXT NOT NULL UNIQUE,
|
49 |
+
image_path TEXT NOT NULL)""")
|
|
|
|
|
|
|
|
|
|
|
50 |
conn.commit()
|
51 |
|
52 |
def insert_student(name, roll_number, image_path):
|
|
|
58 |
except sqlite3.IntegrityError:
|
59 |
st.warning("Roll number already exists!")
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
def get_all_students():
|
62 |
with sqlite3.connect(DB_FILE) as conn:
|
63 |
cursor = conn.execute("SELECT * FROM students")
|
|
|
114 |
insert_student(name, roll_number, img_path)
|
115 |
st.success(f"Student {name} Registered Successfully!")
|
116 |
except Exception as e:
|
117 |
+
st.error(f"Error saving webcam image: {e}")
|
118 |
elif image_file:
|
119 |
try:
|
120 |
img = Image.open(image_file)
|
|
|
124 |
insert_student(name, roll_number, img_path)
|
125 |
st.success(f"Student {name} Registered Successfully!")
|
126 |
except Exception as e:
|
127 |
+
st.error(f"Error saving uploaded image: {e}")
|
128 |
else:
|
129 |
st.warning("Please upload an image or use the webcam to register the face.")
|
130 |
else:
|
|
|
144 |
emotion_label = detect_faces_and_emotions(img_array)
|
145 |
if emotion_label:
|
146 |
st.success(f"Emotion Detected: {emotion_label}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
else:
|
148 |
st.warning("No face detected.")
|
149 |
except Exception as e:
|
|
|
159 |
emotion_label = detect_faces_and_emotions(img_array)
|
160 |
if emotion_label:
|
161 |
st.success(f"Emotion Detected: {emotion_label}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
else:
|
163 |
st.warning("No face detected.")
|
164 |
except Exception as e:
|
|
|
170 |
students = get_all_students()
|
171 |
if students:
|
172 |
for student in students:
|
173 |
+
st.write(f"Name: {student[1]}, Roll Number: {student[2]}")
|
174 |
else:
|
175 |
st.warning("No students registered.")
|