LovnishVerma commited on
Commit
316712f
·
verified ·
1 Parent(s): 71e093b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -56
app.py CHANGED
@@ -1,18 +1,37 @@
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 datetime import datetime
 
9
 
10
  # Constants
11
- ROOT_DIR = os.getcwd() # Root directory of the project
12
- DATABASE = "students.db" # SQLite database file to store student information
13
- EMOTION_MODEL_FILE = "CNN_Model_acc_75.h5"
 
 
14
  EMOTION_LABELS = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  # Load the emotion detection model
17
  try:
18
  emotion_model = load_model(EMOTION_MODEL_FILE)
@@ -25,7 +44,7 @@ def initialize_database():
25
  """ Initializes the SQLite database by creating the students table if it doesn't exist. """
26
  conn = sqlite3.connect(DATABASE)
27
  cursor = conn.cursor()
28
- cursor.execute("""
29
  CREATE TABLE IF NOT EXISTS students (
30
  id INTEGER PRIMARY KEY AUTOINCREMENT,
31
  name TEXT NOT NULL,
@@ -53,22 +72,31 @@ def save_to_database(name, roll_no, image_path):
53
  finally:
54
  conn.close()
55
 
56
- def save_image_to_root_directory(image, name, roll_no):
57
- """ Saves the image locally in the root directory. """
58
  # Construct the local file path
59
- filename = f"{name}_{roll_no}.jpg"
60
- local_path = os.path.join(ROOT_DIR, filename)
61
 
62
  try:
63
  # Convert image to RGB if necessary
64
  if image.mode != "RGB":
65
  image = image.convert("RGB")
66
 
67
- # Save the image to the root directory
68
  image.save(local_path)
69
- st.success(f"Image saved to {local_path}.")
 
 
 
 
 
 
 
 
 
70
  except Exception as e:
71
- st.error(f"Error saving image: {e}")
72
 
73
  return local_path
74
 
@@ -76,7 +104,7 @@ def save_image_to_root_directory(image, name, roll_no):
76
  initialize_database()
77
 
78
  # Streamlit user interface (UI)
79
- st.title("Student Registration with Image Upload and Face Recognition")
80
 
81
  # Input fields for student details
82
  name = st.text_input("Enter your name")
@@ -105,12 +133,26 @@ if st.button("Register"):
105
  elif capture_mode == "Upload File" and picture:
106
  image = Image.open(picture)
107
 
108
- # Save the image locally in the root directory
109
- image_path = save_image_to_root_directory(image, name, roll_no)
110
  save_to_database(name, roll_no, image_path)
111
  except Exception as e:
112
  st.error(f"An error occurred: {e}")
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  # Face and Emotion Detection Function
115
  def detect_faces_and_emotions(image):
116
  gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
@@ -130,37 +172,8 @@ def detect_faces_and_emotions(image):
130
  return EMOTION_LABELS[emotion_label]
131
  return None
132
 
133
- # Face Recognition: Compare uploaded image with all images in the root directory
134
- def recognize_face(image_path):
135
- """ Compares the uploaded image with all images in the root directory """
136
- img = cv2.imread(image_path)
137
- gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
138
- face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
139
- faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.3, minNeighbors=5)
140
-
141
- recognized_name = None
142
- for (x, y, w, h) in faces:
143
- face = gray_image[y:y+h, x:x+w]
144
- for filename in os.listdir(ROOT_DIR):
145
- if filename.endswith(('.jpg', '.jpeg', '.png')):
146
- stored_image = cv2.imread(os.path.join(ROOT_DIR, filename))
147
- stored_gray = cv2.cvtColor(stored_image, cv2.COLOR_BGR2GRAY)
148
- stored_faces = face_cascade.detectMultiScale(stored_gray)
149
- for (sx, sy, sw, sh) in stored_faces:
150
- stored_face = stored_gray[sy:sy+sh, sx:sx+sw]
151
- resized_stored_face = cv2.resize(stored_face, (48, 48))
152
- rgb_stored_face = cv2.cvtColor(resized_stored_face, cv2.COLOR_GRAY2RGB)
153
- stored_normalized_face = rgb_stored_face / 255.0
154
- stored_reshaped_face = np.reshape(stored_normalized_face, (1, 48, 48, 3))
155
-
156
- # Compare the faces (you can use a more advanced method like facial embeddings, but for simplicity, this is just basic comparison)
157
- if np.allclose(stored_reshaped_face, face):
158
- recognized_name = filename.split('_')[0] # Extract the name from the file name
159
- break
160
- return recognized_name
161
-
162
- # UI for Emotion and Face Detection
163
- if st.sidebar.selectbox("Menu", ["Register Student", "Face Recognition and Emotion Detection"]) == "Face Recognition and Emotion Detection":
164
  st.subheader("Recognize Faces and Detect Emotions")
165
  action = st.radio("Choose Action", ["Upload Image", "Use Webcam"])
166
 
@@ -170,11 +183,8 @@ if st.sidebar.selectbox("Menu", ["Register Student", "Face Recognition and Emoti
170
  img = Image.open(uploaded_file)
171
  img_array = np.array(img)
172
  emotion_label = detect_faces_and_emotions(img_array)
173
- recognized_name = recognize_face(uploaded_file)
174
  if emotion_label:
175
  st.success(f"Emotion Detected: {emotion_label}")
176
- if recognized_name:
177
- st.success(f"Face Recognized: {recognized_name}")
178
  else:
179
  st.warning("No face detected.")
180
 
@@ -185,10 +195,7 @@ if st.sidebar.selectbox("Menu", ["Register Student", "Face Recognition and Emoti
185
  img = Image.open(camera_image)
186
  img_array = np.array(img)
187
  emotion_label = detect_faces_and_emotions(img_array)
188
- recognized_name = recognize_face(camera_image)
189
  if emotion_label:
190
  st.success(f"Emotion Detected: {emotion_label}")
191
- if recognized_name:
192
- st.success(f"Face Recognized: {recognized_name}")
193
  else:
194
  st.warning("No face detected.")
 
 
 
1
  import os
2
+ import streamlit as st
3
+ from huggingface_hub import HfApi
4
  from PIL import Image
5
  import sqlite3
6
+ import cv2
7
+ import numpy as np
8
 
9
  # Constants
10
+ HOME_DIR = os.getcwd() # Home directory to save images (root directory)
11
+ DATABASE = "students.db" # SQLite database to store student information
12
+ REPO_NAME = "face-and-emotion-detection"
13
+ REPO_ID = f"LovnishVerma/{REPO_NAME}" # Hugging Face Repo
14
+ EMOTION_MODEL_FILE = "CNN_Model_acc_75.h5" # Emotion detection model file
15
  EMOTION_LABELS = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
16
 
17
+ # Ensure home directory exists
18
+ if not os.path.exists(HOME_DIR):
19
+ os.makedirs(HOME_DIR)
20
+
21
+ # Retrieve Hugging Face token from environment variable
22
+ hf_token = os.getenv("upload")
23
+ if not hf_token:
24
+ st.error("Hugging Face token not found. Please set the environment variable.")
25
+ st.stop()
26
+
27
+ # Initialize Hugging Face API
28
+ api = HfApi()
29
+ try:
30
+ api.create_repo(repo_id=REPO_ID, repo_type="space", space_sdk="streamlit", token=hf_token, exist_ok=True)
31
+ st.success(f"Repository '{REPO_NAME}' is ready on Hugging Face!")
32
+ except Exception as e:
33
+ st.error(f"Error creating Hugging Face repository: {e}")
34
+
35
  # Load the emotion detection model
36
  try:
37
  emotion_model = load_model(EMOTION_MODEL_FILE)
 
44
  """ Initializes the SQLite database by creating the students table if it doesn't exist. """
45
  conn = sqlite3.connect(DATABASE)
46
  cursor = conn.cursor()
47
+ cursor.execute("""
48
  CREATE TABLE IF NOT EXISTS students (
49
  id INTEGER PRIMARY KEY AUTOINCREMENT,
50
  name TEXT NOT NULL,
 
72
  finally:
73
  conn.close()
74
 
75
+ def save_image_to_hugging_face(image, name, roll_no):
76
+ """ Saves the image locally to the HOME_DIR and uploads it to Hugging Face. """
77
  # Construct the local file path
78
+ filename = f"{name}_{roll_no}_{datetime.now().strftime('%Y%m%d%H%M%S')}.jpg"
79
+ local_path = os.path.join(HOME_DIR, filename)
80
 
81
  try:
82
  # Convert image to RGB if necessary
83
  if image.mode != "RGB":
84
  image = image.convert("RGB")
85
 
86
+ # Save the image to the home directory
87
  image.save(local_path)
88
+
89
+ # Upload the saved file to Hugging Face
90
+ api.upload_file(
91
+ path_or_fileobj=local_path,
92
+ path_in_repo=filename,
93
+ repo_id=REPO_ID,
94
+ repo_type="space",
95
+ token=hf_token,
96
+ )
97
+ st.success(f"Image saved to {HOME_DIR} and uploaded to Hugging Face as {filename}.")
98
  except Exception as e:
99
+ st.error(f"Error saving or uploading image: {e}")
100
 
101
  return local_path
102
 
 
104
  initialize_database()
105
 
106
  # Streamlit user interface (UI)
107
+ st.title("Student Registration with Hugging Face Image Upload")
108
 
109
  # Input fields for student details
110
  name = st.text_input("Enter your name")
 
133
  elif capture_mode == "Upload File" and picture:
134
  image = Image.open(picture)
135
 
136
+ # Save the image locally and upload it to Hugging Face
137
+ image_path = save_image_to_hugging_face(image, name, roll_no)
138
  save_to_database(name, roll_no, image_path)
139
  except Exception as e:
140
  st.error(f"An error occurred: {e}")
141
 
142
+ # Display registered student data
143
+ if st.checkbox("Show registered students"):
144
+ conn = sqlite3.connect(DATABASE)
145
+ cursor = conn.cursor()
146
+ cursor.execute("SELECT name, roll_no, image_path, timestamp FROM students")
147
+ rows = cursor.fetchall()
148
+ conn.close()
149
+
150
+ st.write("### Registered Students")
151
+ for row in rows:
152
+ name, roll_no, image_path, timestamp = row
153
+ st.write(f"**Name:** {name}, **Roll No:** {roll_no}, **Timestamp:** {timestamp}")
154
+ st.image(image_path, caption=f"{name} ({roll_no})", use_column_width=True)
155
+
156
  # Face and Emotion Detection Function
157
  def detect_faces_and_emotions(image):
158
  gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 
172
  return EMOTION_LABELS[emotion_label]
173
  return None
174
 
175
+ # UI for Emotion Detection
176
+ if st.sidebar.selectbox("Menu", ["Register Student", "Face Recognition and Emotion Detection", "View Attendance"]) == "Face Recognition and Emotion Detection":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  st.subheader("Recognize Faces and Detect Emotions")
178
  action = st.radio("Choose Action", ["Upload Image", "Use Webcam"])
179
 
 
183
  img = Image.open(uploaded_file)
184
  img_array = np.array(img)
185
  emotion_label = detect_faces_and_emotions(img_array)
 
186
  if emotion_label:
187
  st.success(f"Emotion Detected: {emotion_label}")
 
 
188
  else:
189
  st.warning("No face detected.")
190
 
 
195
  img = Image.open(camera_image)
196
  img_array = np.array(img)
197
  emotion_label = detect_faces_and_emotions(img_array)
 
198
  if emotion_label:
199
  st.success(f"Emotion Detected: {emotion_label}")
 
 
200
  else:
201
  st.warning("No face detected.")