|
import os |
|
import sqlite3 |
|
import cv2 |
|
import streamlit as st |
|
from datetime import datetime |
|
from PIL import Image |
|
import numpy as np |
|
from keras.models import load_model |
|
from huggingface_hub import HfApi |
|
import time |
|
|
|
|
|
KNOWN_FACES_DIR = "known_faces" |
|
DATABASE = "students.db" |
|
|
|
|
|
os.makedirs(KNOWN_FACES_DIR, exist_ok=True) |
|
|
|
|
|
hf_token = os.getenv("upload") |
|
if not hf_token: |
|
raise ValueError("Hugging Face token not found. Ensure it's set as a secret in Hugging Face") |
|
api = HfApi() |
|
|
|
|
|
REPO_NAME = "face_and_emotion_detection" |
|
REPO_ID = "LovnishVerma/" + REPO_NAME |
|
REPO_TYPE = "space" |
|
|
|
|
|
model = load_model('CNN_Model_acc_75.h5') |
|
emotion_labels = ['angry', 'fear', 'happy', 'neutral', 'sad', 'surprise'] |
|
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') |
|
|
|
|
|
def initialize_database(): |
|
conn = sqlite3.connect(DATABASE) |
|
cursor = conn.cursor() |
|
cursor.execute(""" |
|
CREATE TABLE IF NOT EXISTS students ( |
|
id INTEGER PRIMARY KEY AUTOINCREMENT, |
|
name TEXT NOT NULL, |
|
roll_no TEXT NOT NULL UNIQUE, |
|
image_path TEXT NOT NULL, |
|
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP |
|
) |
|
""") |
|
conn.commit() |
|
conn.close() |
|
|
|
|
|
def save_to_database(name, roll_no, image_path): |
|
conn = sqlite3.connect(DATABASE) |
|
cursor = conn.cursor() |
|
try: |
|
cursor.execute(""" |
|
INSERT INTO students (name, roll_no, image_path) |
|
VALUES (?, ?, ?) |
|
""", (name, roll_no, image_path)) |
|
conn.commit() |
|
st.success("Data saved successfully!") |
|
except sqlite3.IntegrityError: |
|
st.error("Roll number already exists!") |
|
finally: |
|
conn.close() |
|
|
|
|
|
def save_image_to_hugging_face(image, name, roll_no): |
|
filename = f"{name}_{roll_no}.jpg" |
|
local_path = os.path.join(KNOWN_FACES_DIR, filename) |
|
image.save(local_path) |
|
|
|
try: |
|
api.upload_file( |
|
path_or_fileobj=local_path, |
|
path_in_repo=filename, |
|
repo_id=REPO_ID, |
|
repo_type=REPO_TYPE, |
|
token=hf_token |
|
) |
|
st.success(f"Image uploaded to Hugging Face: {filename}") |
|
except Exception as e: |
|
st.error(f"Error uploading image to Hugging Face: {e}") |
|
|
|
return local_path |
|
|
|
|
|
def process_frame(frame): |
|
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
|
faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) |
|
|
|
for (x, y, w, h) in faces: |
|
roi_gray = gray_frame[y:y+h, x:x+w] |
|
roi_color = frame[y:y+h, x:x+w] |
|
|
|
face_roi = cv2.resize(roi_color, (48, 48)) |
|
face_roi = np.expand_dims(face_roi, axis=0) |
|
face_roi = face_roi / float(48) |
|
predictions = model.predict(face_roi) |
|
emotion = emotion_labels[np.argmax(predictions[0])] |
|
|
|
|
|
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) |
|
cv2.putText(frame, emotion, (x, y+h), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) |
|
|
|
return frame |
|
|
|
|
|
def record_attendance(name, roll_no, emotion): |
|
conn = sqlite3.connect(DATABASE) |
|
cursor = conn.cursor() |
|
cursor.execute(""" |
|
INSERT INTO students (name, roll_no, image_path, timestamp) |
|
VALUES (?, ?, ?, ?) |
|
""", (name, roll_no, f"known_faces/{name}_{roll_no}.jpg", datetime.now())) |
|
conn.commit() |
|
conn.close() |
|
|
|
|
|
st.title("Student Registration and Attendance") |
|
|
|
|
|
capture_mode = st.radio("Choose an option to upload your image", ["Use Webcam", "Upload File"]) |
|
|
|
if capture_mode == "Use Webcam": |
|
picture = st.camera_input("Take a picture") |
|
elif capture_mode == "Upload File": |
|
picture = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) |
|
|
|
|
|
name = st.text_input("Enter your name") |
|
roll_no = st.text_input("Enter your roll number") |
|
|
|
|
|
if st.button("Register"): |
|
if not name or not roll_no: |
|
st.error("Please fill in both name and roll number.") |
|
elif not picture: |
|
st.error("Please upload or capture an image.") |
|
else: |
|
try: |
|
|
|
if capture_mode == "Use Webcam" and picture: |
|
image = Image.open(picture) |
|
elif capture_mode == "Upload File" and picture: |
|
image = Image.open(picture) |
|
|
|
|
|
image_path = save_image_to_hugging_face(image, name, roll_no) |
|
|
|
|
|
save_to_database(name, roll_no, image_path) |
|
|
|
|
|
cap = cv2.VideoCapture(0) |
|
while True: |
|
ret, frame = cap.read() |
|
if not ret: |
|
break |
|
|
|
frame = process_frame(frame) |
|
st.image(frame, channels="BGR", use_column_width=True) |
|
record_attendance(name, roll_no, emotion) |
|
break |
|
|
|
cap.release() |
|
|
|
except Exception as e: |
|
st.error(f"An error occurred: {e}") |
|
|
|
|
|
if st.checkbox("Show registered students"): |
|
conn = sqlite3.connect(DATABASE) |
|
cursor = conn.cursor() |
|
cursor.execute("SELECT name, roll_no, image_path, timestamp FROM students") |
|
rows = cursor.fetchall() |
|
conn.close() |
|
|
|
st.write("### Registered Students") |
|
for row in rows: |
|
name, roll_no, image_path, timestamp = row |
|
st.write(f"**Name:** {name}, **Roll No:** {roll_no}, **Timestamp:** {timestamp}") |
|
st.image(image_path, caption=f"{name} ({roll_no})", use_column_width=True) |
|
|