Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import sqlite3
|
3 |
+
import cv2
|
4 |
+
import datetime
|
5 |
+
import pandas as pd
|
6 |
+
from PIL import Image
|
7 |
+
import io
|
8 |
+
|
9 |
+
def create_database():
|
10 |
+
"""Creates the attendance database."""
|
11 |
+
conn = sqlite3.connect('attendance.db')
|
12 |
+
c = conn.cursor()
|
13 |
+
c.execute('''CREATE TABLE IF NOT EXISTS attendance (
|
14 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
15 |
+
name TEXT,
|
16 |
+
timestamp TEXT
|
17 |
+
)''')
|
18 |
+
conn.commit()
|
19 |
+
conn.close()
|
20 |
+
|
21 |
+
def mark_attendance(name):
|
22 |
+
"""Marks attendance for the user."""
|
23 |
+
conn = sqlite3.connect('attendance.db')
|
24 |
+
c = conn.cursor()
|
25 |
+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
26 |
+
c.execute("INSERT INTO attendance (name, timestamp) VALUES (?, ?)", (name, timestamp))
|
27 |
+
conn.commit()
|
28 |
+
conn.close()
|
29 |
+
|
30 |
+
def get_attendance_records():
|
31 |
+
"""Fetches all attendance records."""
|
32 |
+
conn = sqlite3.connect('attendance.db')
|
33 |
+
c = conn.cursor()
|
34 |
+
c.execute("SELECT * FROM attendance")
|
35 |
+
records = c.fetchall()
|
36 |
+
conn.close()
|
37 |
+
return records
|
38 |
+
|
39 |
+
def capture_photo():
|
40 |
+
"""Captures a photo using the webcam."""
|
41 |
+
cap = cv2.VideoCapture(0)
|
42 |
+
st.info("Click 'Capture' to take a photo.")
|
43 |
+
while cap.isOpened():
|
44 |
+
ret, frame = cap.read()
|
45 |
+
if not ret:
|
46 |
+
st.error("Failed to capture image.")
|
47 |
+
break
|
48 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
49 |
+
st.image(frame, channels="RGB")
|
50 |
+
if st.button("Capture"):
|
51 |
+
cap.release()
|
52 |
+
cv2.destroyAllWindows()
|
53 |
+
return frame
|
54 |
+
if st.button("Cancel"):
|
55 |
+
cap.release()
|
56 |
+
cv2.destroyAllWindows()
|
57 |
+
return None
|
58 |
+
|
59 |
+
create_database()
|
60 |
+
st.title("Attendance System")
|
61 |
+
|
62 |
+
menu = st.sidebar.selectbox("Menu", ["Click Photo", "Database"])
|
63 |
+
|
64 |
+
if menu == "Click Photo":
|
65 |
+
st.header("Mark Attendance by Clicking Photo")
|
66 |
+
name = st.text_input("Enter your name:")
|
67 |
+
if name:
|
68 |
+
frame = capture_photo()
|
69 |
+
if frame is not None:
|
70 |
+
st.success(f"Attendance marked for {name}")
|
71 |
+
mark_attendance(name)
|
72 |
+
st.image(frame, caption="Captured Image", channels="RGB")
|
73 |
+
|
74 |
+
elif menu == "Database":
|
75 |
+
st.header("Attendance Records")
|
76 |
+
records = get_attendance_records()
|
77 |
+
df = pd.DataFrame(records, columns=["ID", "Name", "Timestamp"])
|
78 |
+
st.dataframe(df)
|