LovnishVerma commited on
Commit
6a0f76d
·
verified ·
1 Parent(s): a0162ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -62
app.py CHANGED
@@ -3,7 +3,6 @@ import cv2
3
  import numpy as np
4
  import os
5
  import sqlite3
6
- import time
7
  from PIL import Image
8
  from keras.models import load_model
9
  from huggingface_hub import HfApi
@@ -19,15 +18,15 @@ REPO_ID = "LovnishVerma/" + REPO_NAME
19
  IMG_SHAPE = 48
20
  hf_token = os.getenv("upload")
21
 
22
- # Initialize Hugging Face API
23
- api = HfApi()
24
-
25
  # Ensure the Hugging Face token is available
26
  if not hf_token:
27
  st.error("Hugging Face token not found. Please set the environment variable.")
28
  st.stop()
29
 
30
- # Initialize the Hugging Face repository
 
 
 
31
  def create_hugging_face_repo():
32
  try:
33
  api.create_repo(repo_id=REPO_ID, repo_type="space", space_sdk="streamlit", token=hf_token, exist_ok=True)
@@ -87,6 +86,9 @@ def save_image_to_hugging_face(image, name, roll_no):
87
  """
88
  Saves the captured image locally in the 'known_faces' directory and uploads it to Hugging Face.
89
  """
 
 
 
90
  filename = f"{name}_{roll_no}.jpg"
91
  local_path = os.path.join(KNOWN_FACES_DIR, filename)
92
  image.save(local_path)
@@ -183,65 +185,59 @@ def video_feed(video_source):
183
  def main():
184
  st.title("Student Registration with Face Recognition and Emotion Detection")
185
 
186
- name = st.text_input("Enter your name")
187
- roll_no = st.text_input("Enter your roll number")
188
-
189
- capture_mode = st.radio("Choose an option to upload your image", ["Use Webcam", "Upload File"])
190
-
191
- if capture_mode == "Use Webcam":
192
- picture = st.camera_input("Take a picture")
193
- else:
194
- picture = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
195
 
196
- if st.button("Register"):
197
- if not name or not roll_no:
198
- st.error("Please fill in both name and roll number.")
199
- elif not picture:
200
- st.error("Please upload or capture an image.")
 
 
 
201
  else:
202
- try:
203
- image = Image.open(picture)
204
- image_path = save_image_to_hugging_face(image, name, roll_no)
205
- save_to_database(name, roll_no, image_path)
206
- except Exception as e:
207
- st.error(f"An error occurred: {e}")
208
-
209
- if st.checkbox("Show registered students"):
210
- with sqlite3.connect(DATABASE) as conn:
211
- cursor = conn.cursor()
212
- cursor.execute("SELECT name, roll_no, image_path, timestamp FROM students")
213
- rows = cursor.fetchall()
214
-
215
- for row in rows:
216
- name, roll_no, image_path, timestamp = row
217
- st.write(f"**Name:** {name}, **Roll No:** {roll_no}, **Timestamp:** {timestamp}")
218
- st.image(image_path, caption=f"{name} ({roll_no})", use_column_width=True)
219
-
220
- upload_choice = st.sidebar.radio("Choose input source", ["Upload Image", "Upload Video", "Camera"])
221
-
222
- if upload_choice == "Camera":
223
- image = st.camera_input("Take a picture")
224
- if image:
225
- frame = np.array(Image.open(image))
226
- frame, result_text = process_frame(frame)
227
- st.image(frame, caption='Processed Image', use_column_width=True)
228
- st.markdown(f"<h3 style='text-align: center;'>{result_text}</h3>", unsafe_allow_html=True)
229
- elif upload_choice == "Upload Image":
230
- uploaded_image = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg", "gif"])
231
- if uploaded_image:
232
- image = Image.open(uploaded_image)
233
- frame = np.array(image)
234
- frame, result_text = process_frame(frame)
235
- st.image(frame, caption='Processed Image', use_column_width=True)
236
- st.markdown(f"<h3 style='text-align: center;'>{result_text}</h3>", unsafe_allow_html=True)
237
- elif upload_choice == "Upload Video":
238
- video_file = st.file_uploader("Upload Video", type=["mp4", "mov", "avi"])
239
- if video_file:
240
- temp_video_file = tempfile.NamedTemporaryFile(delete=False)
241
- temp_video_file.write(video_file.read())
242
- temp_video_file.close()
243
- video_source = cv2.VideoCapture(temp_video_file.name)
244
- video_feed(video_source)
245
 
246
  if __name__ == "__main__":
247
  main()
 
3
  import numpy as np
4
  import os
5
  import sqlite3
 
6
  from PIL import Image
7
  from keras.models import load_model
8
  from huggingface_hub import HfApi
 
18
  IMG_SHAPE = 48
19
  hf_token = os.getenv("upload")
20
 
 
 
 
21
  # Ensure the Hugging Face token is available
22
  if not hf_token:
23
  st.error("Hugging Face token not found. Please set the environment variable.")
24
  st.stop()
25
 
26
+ # Initialize Hugging Face API
27
+ api = HfApi()
28
+
29
+ # Create Hugging Face repository
30
  def create_hugging_face_repo():
31
  try:
32
  api.create_repo(repo_id=REPO_ID, repo_type="space", space_sdk="streamlit", token=hf_token, exist_ok=True)
 
86
  """
87
  Saves the captured image locally in the 'known_faces' directory and uploads it to Hugging Face.
88
  """
89
+ if not os.path.exists(KNOWN_FACES_DIR):
90
+ os.makedirs(KNOWN_FACES_DIR)
91
+
92
  filename = f"{name}_{roll_no}.jpg"
93
  local_path = os.path.join(KNOWN_FACES_DIR, filename)
94
  image.save(local_path)
 
185
  def main():
186
  st.title("Student Registration with Face Recognition and Emotion Detection")
187
 
188
+ # Step 1: Student Registration
189
+ registration_mode = st.sidebar.radio("Choose an option", ["Register Student", "Face and Emotion Recognition"])
 
 
 
 
 
 
 
190
 
191
+ if registration_mode == "Register Student":
192
+ name = st.text_input("Enter your name")
193
+ roll_no = st.text_input("Enter your roll number")
194
+
195
+ capture_mode = st.radio("Choose an option to upload your image", ["Use Webcam", "Upload File"])
196
+
197
+ if capture_mode == "Use Webcam":
198
+ picture = st.camera_input("Take a picture")
199
  else:
200
+ picture = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
201
+
202
+ if st.button("Register"):
203
+ if not name or not roll_no:
204
+ st.error("Please fill in both name and roll number.")
205
+ elif not picture:
206
+ st.error("Please upload or capture an image.")
207
+ else:
208
+ try:
209
+ image = Image.open(picture)
210
+ image_path = save_image_to_hugging_face(image, name, roll_no)
211
+ save_to_database(name, roll_no, image_path)
212
+ except Exception as e:
213
+ st.error(f"An error occurred: {e}")
214
+
215
+ elif registration_mode == "Face and Emotion Recognition":
216
+ upload_choice = st.radio("Choose input source", ["Upload Image", "Upload Video", "Camera"])
217
+
218
+ if upload_choice == "Camera":
219
+ image = st.camera_input("Take a picture")
220
+ if image:
221
+ frame = np.array(Image.open(image))
222
+ frame, result_text = process_frame(frame)
223
+ st.image(frame, caption='Processed Image', use_column_width=True)
224
+ st.markdown(f"<h3 style='text-align: center;'>{result_text}</h3>", unsafe_allow_html=True)
225
+ elif upload_choice == "Upload Image":
226
+ uploaded_image = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg", "gif"])
227
+ if uploaded_image:
228
+ image = Image.open(uploaded_image)
229
+ frame = np.array(image)
230
+ frame, result_text = process_frame(frame)
231
+ st.image(frame, caption='Processed Image', use_column_width=True)
232
+ st.markdown(f"<h3 style='text-align: center;'>{result_text}</h3>", unsafe_allow_html=True)
233
+ elif upload_choice == "Upload Video":
234
+ video_file = st.file_uploader("Upload Video", type=["mp4", "mov", "avi"])
235
+ if video_file:
236
+ temp_video_file = tempfile.NamedTemporaryFile(delete=False)
237
+ temp_video_file.write(video_file.read())
238
+ temp_video_file.close()
239
+ video_source = cv2.VideoCapture(temp_video_file.name)
240
+ video_feed(video_source)
 
 
241
 
242
  if __name__ == "__main__":
243
  main()