Update app.py
Browse files
app.py
CHANGED
@@ -1,136 +1,188 @@
|
|
1 |
import streamlit as st
|
2 |
import cv2
|
|
|
3 |
import numpy as np
|
4 |
-
import time
|
5 |
from keras.models import load_model
|
6 |
from PIL import Image
|
7 |
-
|
8 |
-
import
|
9 |
-
import
|
10 |
-
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
st.
|
27 |
-
|
28 |
-
#
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
"
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
st.
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
"
|
|
|
|
|
|
|
113 |
try:
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
)
|
124 |
-
st.success("File uploaded successfully to Hugging Face!")
|
125 |
except Exception as e:
|
126 |
-
st.error(f"
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 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()
|
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 |
+
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 (webcam or file upload)
|
101 |
+
capture_mode = st.radio("Choose an option to upload your image", ["Use Webcam", "Upload File"])
|
102 |
+
|
103 |
+
# Handle webcam capture or file upload
|
104 |
+
if capture_mode == "Use Webcam":
|
105 |
+
picture = st.camera_input("Take a picture") # Capture image using webcam
|
106 |
+
elif capture_mode == "Upload File":
|
107 |
+
picture = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) # Upload image from file system
|
108 |
+
|
109 |
+
# Save data and process image on button click
|
110 |
+
if st.button("Register"):
|
111 |
+
if not name or not roll_no:
|
112 |
+
st.error("Please fill in both name and roll number.")
|
113 |
+
elif not picture:
|
114 |
+
st.error("Please upload or capture an image.")
|
115 |
+
else:
|
116 |
try:
|
117 |
+
# Open the image based on capture mode
|
118 |
+
if capture_mode == "Use Webcam" and picture:
|
119 |
+
image = Image.open(picture)
|
120 |
+
elif capture_mode == "Upload File" and picture:
|
121 |
+
image = Image.open(picture)
|
122 |
+
|
123 |
+
# Save the image locally and upload it to Hugging Face
|
124 |
+
image_path = save_image_to_hugging_face(image, name, roll_no)
|
125 |
+
save_to_database(name, roll_no, image_path)
|
|
|
|
|
126 |
except Exception as e:
|
127 |
+
st.error(f"An error occurred: {e}")
|
128 |
+
|
129 |
+
# Display registered student data
|
130 |
+
if st.checkbox("Show registered students"):
|
131 |
+
conn = sqlite3.connect(DATABASE)
|
132 |
+
cursor = conn.cursor()
|
133 |
+
cursor.execute("SELECT name, roll_no, image_path, timestamp FROM students")
|
134 |
+
rows = cursor.fetchall()
|
135 |
+
conn.close()
|
136 |
+
|
137 |
+
st.write("### Registered Students")
|
138 |
+
for row in rows:
|
139 |
+
name, roll_no, image_path, timestamp = row
|
140 |
+
st.write(f"**Name:** {name}, **Roll No:** {roll_no}, **Timestamp:** {timestamp}")
|
141 |
+
st.image(image_path, caption=f"{name} ({roll_no})", use_column_width=True)
|
142 |
+
|
143 |
+
# Face and Emotion Detection Function
|
144 |
+
def detect_faces_and_emotions(image):
|
145 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
146 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
147 |
+
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.3, minNeighbors=5)
|
148 |
+
|
149 |
+
for (x, y, w, h) in faces:
|
150 |
+
face = gray_image[y:y+h, x:x+w]
|
151 |
+
resized_face = cv2.resize(face, (48, 48)) # Resize face to 48x48
|
152 |
+
rgb_face = cv2.cvtColor(resized_face, cv2.COLOR_GRAY2RGB)
|
153 |
+
normalized_face = rgb_face / 255.0
|
154 |
+
reshaped_face = np.reshape(normalized_face, (1, 48, 48, 3))
|
155 |
+
|
156 |
+
# Predict the emotion
|
157 |
+
emotion_prediction = emotion_model.predict(reshaped_face)
|
158 |
+
emotion_label = np.argmax(emotion_prediction)
|
159 |
+
return EMOTION_LABELS[emotion_label]
|
160 |
+
return None
|
161 |
+
|
162 |
+
# UI for Emotion Detection
|
163 |
+
if st.sidebar.selectbox("Menu", ["Register Student", "Face Recognition and Emotion Detection", "View Attendance"]) == "Face Recognition and Emotion Detection":
|
164 |
+
st.subheader("Recognize Faces and Detect Emotions")
|
165 |
+
action = st.radio("Choose Action", ["Upload Image", "Use Webcam"])
|
166 |
+
|
167 |
+
if action == "Upload Image":
|
168 |
+
uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
|
169 |
+
if uploaded_file:
|
170 |
+
img = Image.open(uploaded_file)
|
171 |
+
img_array = np.array(img)
|
172 |
+
emotion_label = detect_faces_and_emotions(img_array)
|
173 |
+
if emotion_label:
|
174 |
+
st.success(f"Emotion Detected: {emotion_label}")
|
175 |
+
else:
|
176 |
+
st.warning("No face detected.")
|
177 |
+
|
178 |
+
elif action == "Use Webcam":
|
179 |
+
st.info("Use the camera input widget to capture an image.")
|
180 |
+
camera_image = st.camera_input("Take a picture")
|
181 |
+
if camera_image:
|
182 |
+
img = Image.open(camera_image)
|
183 |
+
img_array = np.array(img)
|
184 |
+
emotion_label = detect_faces_and_emotions(img_array)
|
185 |
+
if emotion_label:
|
186 |
+
st.success(f"Emotion Detected: {emotion_label}")
|
187 |
+
else:
|
188 |
+
st.warning("No face detected.")
|