LovnishVerma commited on
Commit
c850a8d
·
verified ·
1 Parent(s): f318d47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -126
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
- DB_FILE = "students.db"
12
- KNOWN_FACES_DIR = "known_faces"
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}" # Replace with your Hugging Face username and repository name
19
 
20
- # Ensure Directories
21
  os.makedirs(KNOWN_FACES_DIR, exist_ok=True)
22
 
23
- # Load Hugging Face Token
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 Emotion 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
 
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):
 
 
 
 
 
 
 
 
 
 
53
  try:
54
- with sqlite3.connect(DB_FILE) as conn:
55
- conn.execute("INSERT INTO students (name, roll_number, image_path) VALUES (?, ?, ?)",
56
- (name, roll_number, image_path))
57
- conn.commit()
 
 
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")
64
- return cursor.fetchall()
65
-
66
- # Face and Emotion Detection
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- # Convert grayscale to RGB by duplicating the channel
76
- rgb_face = cv2.cvtColor(resized_face, cv2.COLOR_GRAY2RGB) # Convert grayscale to RGB
77
- normalized_face = rgb_face / 255.0 # Normalize pixel values to [0, 1]
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) # Get the index of the highest probability
83
  return EMOTION_LABELS[emotion_label]
84
  return None
85
 
86
- # UI Design
87
- st.title("Student Registration and Emotion Detection")
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
- action = st.radio("Choose Action", ["Upload Image", "Use Webcam"])
137
-
138
- if action == "Upload Image":
139
- uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
140
- if uploaded_file:
141
- try:
142
- img = Image.open(uploaded_file)
143
- img_array = np.array(img)
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:
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.")