Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,14 @@
|
|
1 |
import streamlit as st
|
2 |
-
import cv2
|
3 |
import os
|
4 |
-
import numpy as np
|
5 |
-
from keras.models import load_model
|
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
|
20 |
os.makedirs(KNOWN_FACES_DIR, exist_ok=True)
|
21 |
|
22 |
# Retrieve Hugging Face token from environment variable
|
@@ -27,54 +19,17 @@ if not hf_token:
|
|
27 |
|
28 |
# Initialize Hugging Face API
|
29 |
api = HfApi()
|
30 |
-
try:
|
31 |
-
api.create_repo(repo_id=REPO_ID, repo_type="space", space_sdk="streamlit", token=hf_token, exist_ok=True)
|
32 |
-
st.success(f"Repository '{REPO_NAME}' is ready on Hugging Face!")
|
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 |
-
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,7 +37,7 @@ def save_image_to_hugging_face(image, name, roll_no):
|
|
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,
|
@@ -97,11 +52,7 @@ def save_image_to_hugging_face(image, name, roll_no):
|
|
97 |
|
98 |
return local_path
|
99 |
|
100 |
-
|
101 |
-
# Initialize the database when the app starts
|
102 |
-
initialize_database()
|
103 |
-
|
104 |
-
# Streamlit user interface (UI)
|
105 |
st.title("Student Registration with Hugging Face Image Upload")
|
106 |
|
107 |
# Input fields for student details
|
@@ -133,68 +84,5 @@ if st.button("Register"):
|
|
133 |
|
134 |
# Save the image locally and upload it to Hugging Face
|
135 |
image_path = save_image_to_hugging_face(image, name, roll_no)
|
136 |
-
save_to_database(name, roll_no, image_path)
|
137 |
except Exception as e:
|
138 |
st.error(f"An error occurred: {e}")
|
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:
|
149 |
-
name, roll_no, image_path, timestamp = row
|
150 |
-
st.write(f"**Name:** {name}, **Roll No:** {roll_no}, **Timestamp:** {timestamp}")
|
151 |
-
st.image(image_path, caption=f"{name} ({roll_no})", use_column_width=True)
|
152 |
-
|
153 |
-
# Face and Emotion Detection Function
|
154 |
-
def detect_faces_and_emotions(image):
|
155 |
-
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
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
|
163 |
-
rgb_face = cv2.cvtColor(resized_face, cv2.COLOR_GRAY2RGB)
|
164 |
-
normalized_face = rgb_face / 255.0
|
165 |
-
reshaped_face = np.reshape(normalized_face, (1, 48, 48, 3))
|
166 |
-
|
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":
|
176 |
-
st.subheader("Recognize Faces and Detect Emotions")
|
177 |
-
action = st.radio("Choose Action", ["Upload Image", "Use Webcam"])
|
178 |
-
|
179 |
-
if action == "Upload Image":
|
180 |
-
uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
|
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 |
-
|
190 |
-
elif action == "Use Webcam":
|
191 |
-
st.info("Use the camera input widget to capture an image.")
|
192 |
-
camera_image = st.camera_input("Take a picture")
|
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.")
|
|
|
1 |
import streamlit as st
|
|
|
2 |
import os
|
|
|
|
|
3 |
from PIL import Image
|
|
|
4 |
from huggingface_hub import HfApi
|
|
|
5 |
|
6 |
# Constants
|
7 |
KNOWN_FACES_DIR = "known_faces" # Directory to save user images
|
|
|
|
|
|
|
8 |
REPO_NAME = "face_and_emotion_detection"
|
9 |
REPO_ID = f"LovnishVerma/{REPO_NAME}"
|
10 |
|
11 |
+
# Ensure the directory exists
|
12 |
os.makedirs(KNOWN_FACES_DIR, exist_ok=True)
|
13 |
|
14 |
# Retrieve Hugging Face token from environment variable
|
|
|
19 |
|
20 |
# Initialize Hugging Face API
|
21 |
api = HfApi()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
def save_image_to_hugging_face(image, name, roll_no):
|
24 |
""" Saves the image locally to the KNOWN_FACES_DIR and uploads it to Hugging Face. """
|
25 |
+
# Ensure the directory exists
|
26 |
+
if not os.path.exists(KNOWN_FACES_DIR):
|
27 |
+
os.makedirs(KNOWN_FACES_DIR)
|
28 |
+
|
29 |
+
# Construct the local file path
|
30 |
filename = f"{name}_{roll_no}.jpg"
|
31 |
local_path = os.path.join(KNOWN_FACES_DIR, filename)
|
32 |
+
|
33 |
try:
|
34 |
# Convert image to RGB if necessary
|
35 |
if image.mode != "RGB":
|
|
|
37 |
|
38 |
# Save the image to the known_faces directory
|
39 |
image.save(local_path)
|
40 |
+
|
41 |
# Upload the saved file to Hugging Face
|
42 |
api.upload_file(
|
43 |
path_or_fileobj=local_path,
|
|
|
52 |
|
53 |
return local_path
|
54 |
|
55 |
+
# Streamlit UI
|
|
|
|
|
|
|
|
|
56 |
st.title("Student Registration with Hugging Face Image Upload")
|
57 |
|
58 |
# Input fields for student details
|
|
|
84 |
|
85 |
# Save the image locally and upload it to Hugging Face
|
86 |
image_path = save_image_to_hugging_face(image, name, roll_no)
|
|
|
87 |
except Exception as e:
|
88 |
st.error(f"An error occurred: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|