Update app.py
Browse files
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
|
|
|
|
|
|
|
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 |
-
|
187 |
-
|
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
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
|
|
|
|
|
|
201 |
else:
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
st.
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
st.
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
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()
|