Spaces:
Sleeping
Sleeping
import streamlit as st | |
import sqlite3 | |
import cv2 | |
import datetime | |
import pandas as pd | |
from PIL import Image | |
import io | |
def create_database(): | |
"""Creates the attendance database.""" | |
conn = sqlite3.connect('attendance.db') | |
c = conn.cursor() | |
c.execute('''CREATE TABLE IF NOT EXISTS attendance ( | |
id INTEGER PRIMARY KEY AUTOINCREMENT, | |
name TEXT, | |
timestamp TEXT | |
)''') | |
conn.commit() | |
conn.close() | |
def mark_attendance(name): | |
"""Marks attendance for the user.""" | |
conn = sqlite3.connect('attendance.db') | |
c = conn.cursor() | |
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
c.execute("INSERT INTO attendance (name, timestamp) VALUES (?, ?)", (name, timestamp)) | |
conn.commit() | |
conn.close() | |
def get_attendance_records(): | |
"""Fetches all attendance records.""" | |
conn = sqlite3.connect('attendance.db') | |
c = conn.cursor() | |
c.execute("SELECT * FROM attendance") | |
records = c.fetchall() | |
conn.close() | |
return records | |
def capture_photo(): | |
"""Captures a photo using the webcam.""" | |
cap = cv2.VideoCapture(0) | |
st.info("Click 'Capture' to take a photo.") | |
while cap.isOpened(): | |
ret, frame = cap.read() | |
if not ret: | |
st.error("Failed to capture image.") | |
break | |
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
st.image(frame, channels="RGB") | |
if st.button("Capture"): | |
cap.release() | |
cv2.destroyAllWindows() | |
return frame | |
if st.button("Cancel"): | |
cap.release() | |
cv2.destroyAllWindows() | |
return None | |
create_database() | |
st.title("Attendance System") | |
menu = st.sidebar.selectbox("Menu", ["Click Photo", "Database"]) | |
if menu == "Click Photo": | |
st.header("Mark Attendance by Clicking Photo") | |
name = st.text_input("Enter your name:") | |
if name: | |
frame = capture_photo() | |
if frame is not None: | |
st.success(f"Attendance marked for {name}") | |
mark_attendance(name) | |
st.image(frame, caption="Captured Image", channels="RGB") | |
elif menu == "Database": | |
st.header("Attendance Records") | |
records = get_attendance_records() | |
df = pd.DataFrame(records, columns=["ID", "Name", "Timestamp"]) | |
st.dataframe(df) | |