File size: 2,337 Bytes
5cc12ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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)