Update app.py
Browse files
app.py
CHANGED
@@ -6,24 +6,24 @@ from keras.models import load_model
|
|
6 |
from PIL import Image
|
7 |
import sqlite3
|
8 |
from huggingface_hub import HfApi
|
|
|
9 |
|
10 |
# Constants
|
11 |
-
|
12 |
-
|
13 |
EMOTION_MODEL_FILE = "CNN_Model_acc_75.h5"
|
14 |
EMOTION_LABELS = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
|
15 |
-
|
16 |
-
# Hugging Face Repository Details
|
17 |
REPO_NAME = "face_and_emotion_detection"
|
18 |
-
REPO_ID = f"LovnishVerma/{REPO_NAME}"
|
19 |
|
20 |
-
# Ensure
|
21 |
os.makedirs(KNOWN_FACES_DIR, exist_ok=True)
|
22 |
|
23 |
-
#
|
24 |
-
hf_token = os.getenv("upload")
|
25 |
if not hf_token:
|
26 |
st.error("Hugging Face token not found. Please set the environment variable.")
|
|
|
27 |
|
28 |
# Initialize Hugging Face API
|
29 |
api = HfApi()
|
@@ -33,143 +33,138 @@ try:
|
|
33 |
except Exception as e:
|
34 |
st.error(f"Error creating Hugging Face repository: {e}")
|
35 |
|
36 |
-
# Load
|
37 |
try:
|
38 |
emotion_model = load_model(EMOTION_MODEL_FILE)
|
39 |
except Exception as e:
|
40 |
st.error(f"Error loading emotion model: {e}")
|
|
|
41 |
|
42 |
# Database Functions
|
43 |
-
def
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
try:
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
58 |
except sqlite3.IntegrityError:
|
59 |
-
st.
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
def detect_faces_and_emotions(image):
|
68 |
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
69 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
70 |
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.3, minNeighbors=5)
|
71 |
-
|
72 |
for (x, y, w, h) in faces:
|
73 |
face = gray_image[y:y+h, x:x+w]
|
74 |
resized_face = cv2.resize(face, (48, 48)) # Resize face to 48x48
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
reshaped_face = np.reshape(normalized_face, (1, 48, 48, 3)) # Ensure the input shape is (1, 48, 48, 3)
|
79 |
|
80 |
# Predict the emotion
|
81 |
emotion_prediction = emotion_model.predict(reshaped_face)
|
82 |
-
emotion_label = np.argmax(emotion_prediction)
|
83 |
return EMOTION_LABELS[emotion_label]
|
84 |
return None
|
85 |
|
86 |
-
# UI
|
87 |
-
st.
|
88 |
-
create_table()
|
89 |
-
|
90 |
-
menu = ["Register Student", "Face Recognition and Emotion Detection", "View Attendance"]
|
91 |
-
choice = st.sidebar.selectbox("Menu", menu)
|
92 |
-
|
93 |
-
if choice == "Register Student":
|
94 |
-
st.subheader("Register a New Student")
|
95 |
-
|
96 |
-
with st.form("register_form"):
|
97 |
-
name = st.text_input("Name")
|
98 |
-
roll_number = st.text_input("Roll Number")
|
99 |
-
image_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
|
100 |
-
use_webcam = st.checkbox("Use Webcam for Face Registration")
|
101 |
-
submitted = st.form_submit_button("Register")
|
102 |
-
|
103 |
-
if submitted:
|
104 |
-
if name and roll_number:
|
105 |
-
if use_webcam:
|
106 |
-
st.info("Use the camera input widget to capture an image.")
|
107 |
-
camera_image = st.camera_input("Capture Image")
|
108 |
-
if camera_image:
|
109 |
-
try:
|
110 |
-
img = Image.open(camera_image)
|
111 |
-
img = img.convert("RGB")
|
112 |
-
img_path = os.path.join(KNOWN_FACES_DIR, f"{roll_number}.png")
|
113 |
-
img.save(img_path)
|
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)
|
121 |
-
img = img.convert("RGB")
|
122 |
-
img_path = os.path.join(KNOWN_FACES_DIR, f"{roll_number}.png")
|
123 |
-
img.save(img_path)
|
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:
|
131 |
-
st.warning("Please fill in all fields.")
|
132 |
-
|
133 |
-
elif choice == "Face Recognition and Emotion Detection":
|
134 |
st.subheader("Recognize Faces and Detect Emotions")
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
if
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
st.success(f"Emotion Detected: {emotion_label}")
|
147 |
-
else:
|
148 |
-
st.warning("No face detected.")
|
149 |
-
except Exception as e:
|
150 |
-
st.error(f"Error: {e}")
|
151 |
-
|
152 |
-
elif action == "Use Webcam":
|
153 |
-
st.info("Use the camera input widget to capture an image.")
|
154 |
-
camera_image = st.camera_input("Take a picture")
|
155 |
-
if camera_image:
|
156 |
-
try:
|
157 |
-
img = Image.open(camera_image)
|
158 |
-
img_array = np.array(img)
|
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:
|
165 |
-
st.error(f"Error: {e}")
|
166 |
-
|
167 |
-
elif choice == "View Attendance":
|
168 |
-
st.subheader("View Registered Students (Attendance)")
|
169 |
-
|
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.")
|
|
|
6 |
from PIL import Image
|
7 |
import sqlite3
|
8 |
from huggingface_hub import HfApi
|
9 |
+
from datetime import datetime
|
10 |
|
11 |
# Constants
|
12 |
+
KNOWN_FACES_DIR = "known_faces" # Directory to save user images
|
13 |
+
DATABASE = "students.db" # SQLite database file to store student information
|
14 |
EMOTION_MODEL_FILE = "CNN_Model_acc_75.h5"
|
15 |
EMOTION_LABELS = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
|
|
|
|
|
16 |
REPO_NAME = "face_and_emotion_detection"
|
17 |
+
REPO_ID = f"LovnishVerma/{REPO_NAME}"
|
18 |
|
19 |
+
# Ensure the directories exist
|
20 |
os.makedirs(KNOWN_FACES_DIR, exist_ok=True)
|
21 |
|
22 |
+
# Retrieve Hugging Face token from environment variable
|
23 |
+
hf_token = os.getenv("upload") # Replace with your actual Hugging Face token
|
24 |
if not hf_token:
|
25 |
st.error("Hugging Face token not found. Please set the environment variable.")
|
26 |
+
st.stop()
|
27 |
|
28 |
# Initialize Hugging Face API
|
29 |
api = HfApi()
|
|
|
33 |
except Exception as e:
|
34 |
st.error(f"Error creating Hugging Face repository: {e}")
|
35 |
|
36 |
+
# Load the emotion detection model
|
37 |
try:
|
38 |
emotion_model = load_model(EMOTION_MODEL_FILE)
|
39 |
except Exception as e:
|
40 |
st.error(f"Error loading emotion model: {e}")
|
41 |
+
st.stop()
|
42 |
|
43 |
# Database Functions
|
44 |
+
def initialize_database():
|
45 |
+
""" Initializes the SQLite database by creating the students table if it doesn't exist. """
|
46 |
+
conn = sqlite3.connect(DATABASE)
|
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 |
+
conn.close()
|
59 |
+
|
60 |
+
def save_to_database(name, roll_no, image_path):
|
61 |
+
""" Saves the student's data to the database. """
|
62 |
+
conn = sqlite3.connect(DATABASE)
|
63 |
+
cursor = conn.cursor()
|
64 |
try:
|
65 |
+
cursor.execute("""
|
66 |
+
INSERT INTO students (name, roll_no, image_path)
|
67 |
+
VALUES (?, ?, ?)
|
68 |
+
""", (name, roll_no, image_path))
|
69 |
+
conn.commit()
|
70 |
+
st.success("Data saved successfully!")
|
71 |
except sqlite3.IntegrityError:
|
72 |
+
st.error("Roll number already exists!")
|
73 |
+
finally:
|
74 |
+
conn.close()
|
75 |
+
|
76 |
+
def save_image_to_hugging_face(image, name, roll_no):
|
77 |
+
""" Saves the image locally and uploads it to Hugging Face. """
|
78 |
+
filename = f"{name}_{roll_no}.jpg"
|
79 |
+
local_path = os.path.join(KNOWN_FACES_DIR, filename)
|
80 |
+
image.save(local_path)
|
81 |
+
|
82 |
+
try:
|
83 |
+
api.upload_file(path_or_fileobj=local_path, path_in_repo=filename, repo_id=REPO_ID, repo_type="space", token=hf_token)
|
84 |
+
st.success(f"Image uploaded to Hugging Face: {filename}")
|
85 |
+
except Exception as e:
|
86 |
+
st.error(f"Error uploading image to Hugging Face: {e}")
|
87 |
+
|
88 |
+
return local_path
|
89 |
+
|
90 |
+
# Initialize the database when the app starts
|
91 |
+
initialize_database()
|
92 |
+
|
93 |
+
# Streamlit user interface (UI)
|
94 |
+
st.title("Student Registration with Hugging Face Image Upload")
|
95 |
+
|
96 |
+
# Input fields for student details
|
97 |
+
name = st.text_input("Enter your name")
|
98 |
+
roll_no = st.text_input("Enter your roll number")
|
99 |
+
|
100 |
+
# Choose input method for the image (only webcam now)
|
101 |
+
capture_mode = "Use Webcam" # Only keep the webcam option now
|
102 |
+
|
103 |
+
# Handle webcam capture
|
104 |
+
picture = st.camera_input("Take a picture") # Capture image using webcam
|
105 |
+
|
106 |
+
# Save data and process image on button click
|
107 |
+
if st.button("Register"):
|
108 |
+
if not name or not roll_no:
|
109 |
+
st.error("Please fill in both name and roll number.")
|
110 |
+
elif not picture:
|
111 |
+
st.error("Please capture an image using the webcam.")
|
112 |
+
else:
|
113 |
+
try:
|
114 |
+
# Open the image based on capture mode
|
115 |
+
if picture:
|
116 |
+
image = Image.open(picture)
|
117 |
+
|
118 |
+
# Save the image locally and upload it to Hugging Face
|
119 |
+
image_path = save_image_to_hugging_face(image, name, roll_no)
|
120 |
+
save_to_database(name, roll_no, image_path)
|
121 |
+
except Exception as e:
|
122 |
+
st.error(f"An error occurred: {e}")
|
123 |
+
|
124 |
+
# Display registered student data
|
125 |
+
if st.checkbox("Show registered students"):
|
126 |
+
conn = sqlite3.connect(DATABASE)
|
127 |
+
cursor = conn.cursor()
|
128 |
+
cursor.execute("SELECT name, roll_no, image_path, timestamp FROM students")
|
129 |
+
rows = cursor.fetchall()
|
130 |
+
conn.close()
|
131 |
+
|
132 |
+
st.write("### Registered Students")
|
133 |
+
for row in rows:
|
134 |
+
name, roll_no, image_path, timestamp = row
|
135 |
+
st.write(f"**Name:** {name}, **Roll No:** {roll_no}, **Timestamp:** {timestamp}")
|
136 |
+
st.image(image_path, caption=f"{name} ({roll_no})", use_column_width=True)
|
137 |
+
|
138 |
+
# Face and Emotion Detection Function
|
139 |
def detect_faces_and_emotions(image):
|
140 |
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
141 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
142 |
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.3, minNeighbors=5)
|
143 |
+
|
144 |
for (x, y, w, h) in faces:
|
145 |
face = gray_image[y:y+h, x:x+w]
|
146 |
resized_face = cv2.resize(face, (48, 48)) # Resize face to 48x48
|
147 |
+
rgb_face = cv2.cvtColor(resized_face, cv2.COLOR_GRAY2RGB)
|
148 |
+
normalized_face = rgb_face / 255.0
|
149 |
+
reshaped_face = np.reshape(normalized_face, (1, 48, 48, 3))
|
|
|
150 |
|
151 |
# Predict the emotion
|
152 |
emotion_prediction = emotion_model.predict(reshaped_face)
|
153 |
+
emotion_label = np.argmax(emotion_prediction)
|
154 |
return EMOTION_LABELS[emotion_label]
|
155 |
return None
|
156 |
|
157 |
+
# UI for Emotion Detection (Only using webcam now)
|
158 |
+
if st.sidebar.selectbox("Menu", ["Register Student", "Face Recognition and Emotion Detection", "View Attendance"]) == "Face Recognition and Emotion Detection":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
st.subheader("Recognize Faces and Detect Emotions")
|
160 |
+
|
161 |
+
st.info("Use the camera input widget to capture an image.")
|
162 |
+
camera_image = st.camera_input("Take a picture")
|
163 |
+
if camera_image:
|
164 |
+
img = Image.open(camera_image)
|
165 |
+
img_array = np.array(img)
|
166 |
+
emotion_label = detect_faces_and_emotions(img_array)
|
167 |
+
if emotion_label:
|
168 |
+
st.success(f"Emotion Detected: {emotion_label}")
|
169 |
+
else:
|
170 |
+
st.warning("No face detected.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|