Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,10 @@ 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."""
|
|
@@ -12,18 +16,27 @@ def create_database():
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
conn.commit()
|
| 28 |
conn.close()
|
| 29 |
|
|
@@ -31,7 +44,7 @@ def get_attendance_records():
|
|
| 31 |
"""Fetches all attendance records."""
|
| 32 |
conn = sqlite3.connect('attendance.db')
|
| 33 |
c = conn.cursor()
|
| 34 |
-
c.execute("SELECT
|
| 35 |
records = c.fetchall()
|
| 36 |
conn.close()
|
| 37 |
return records
|
|
@@ -56,23 +69,36 @@ def capture_photo():
|
|
| 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 |
-
|
| 67 |
-
|
|
|
|
| 68 |
frame = capture_photo()
|
| 69 |
if frame is not None:
|
| 70 |
-
|
| 71 |
-
|
| 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 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
from PIL import Image
|
| 7 |
import io
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Create a directory for storing photos
|
| 11 |
+
os.makedirs("photos", exist_ok=True)
|
| 12 |
|
| 13 |
def create_database():
|
| 14 |
"""Creates the attendance database."""
|
|
|
|
| 16 |
c = conn.cursor()
|
| 17 |
c.execute('''CREATE TABLE IF NOT EXISTS attendance (
|
| 18 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 19 |
+
rollno TEXT,
|
| 20 |
name TEXT,
|
| 21 |
+
photo_path TEXT,
|
| 22 |
timestamp TEXT
|
| 23 |
)''')
|
| 24 |
conn.commit()
|
| 25 |
conn.close()
|
| 26 |
|
| 27 |
+
def mark_attendance(rollno, name, frame):
|
| 28 |
"""Marks attendance for the user."""
|
| 29 |
conn = sqlite3.connect('attendance.db')
|
| 30 |
c = conn.cursor()
|
| 31 |
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
+
|
| 33 |
+
# Save the captured photo
|
| 34 |
+
photo_path = f"photos/{rollno}_{name.replace(' ', '_')}.jpg"
|
| 35 |
+
photo = Image.fromarray(frame)
|
| 36 |
+
photo.save(photo_path)
|
| 37 |
+
|
| 38 |
+
# Insert record into the database
|
| 39 |
+
c.execute("INSERT INTO attendance (rollno, name, photo_path, timestamp) VALUES (?, ?, ?, ?)", (rollno, name, photo_path, timestamp))
|
| 40 |
conn.commit()
|
| 41 |
conn.close()
|
| 42 |
|
|
|
|
| 44 |
"""Fetches all attendance records."""
|
| 45 |
conn = sqlite3.connect('attendance.db')
|
| 46 |
c = conn.cursor()
|
| 47 |
+
c.execute("SELECT rollno, name, photo_path, timestamp FROM attendance")
|
| 48 |
records = c.fetchall()
|
| 49 |
conn.close()
|
| 50 |
return records
|
|
|
|
| 69 |
cv2.destroyAllWindows()
|
| 70 |
return None
|
| 71 |
|
| 72 |
+
# Initialize the database
|
| 73 |
create_database()
|
| 74 |
+
|
| 75 |
st.title("Attendance System")
|
| 76 |
|
| 77 |
menu = st.sidebar.selectbox("Menu", ["Click Photo", "Database"])
|
| 78 |
|
| 79 |
if menu == "Click Photo":
|
| 80 |
st.header("Mark Attendance by Clicking Photo")
|
| 81 |
+
rollno = st.text_input("Enter your Roll Number:")
|
| 82 |
+
name = st.text_input("Enter your Name:")
|
| 83 |
+
if rollno and name:
|
| 84 |
frame = capture_photo()
|
| 85 |
if frame is not None:
|
| 86 |
+
mark_attendance(rollno, name, frame)
|
| 87 |
+
st.success(f"Attendance marked for {name} (Roll No: {rollno})")
|
| 88 |
st.image(frame, caption="Captured Image", channels="RGB")
|
| 89 |
|
| 90 |
elif menu == "Database":
|
| 91 |
st.header("Attendance Records")
|
| 92 |
records = get_attendance_records()
|
| 93 |
+
data = []
|
| 94 |
+
for rollno, name, photo_path, timestamp in records:
|
| 95 |
+
photo = Image.open(photo_path) if os.path.exists(photo_path) else None
|
| 96 |
+
data.append((rollno, name, photo, timestamp))
|
| 97 |
+
|
| 98 |
+
df = pd.DataFrame(data, columns=["Roll No", "Name", "Photo", "Timestamp"])
|
| 99 |
+
st.write("### Attendance Records")
|
| 100 |
+
st.dataframe(df.drop(columns=["Photo"])) # Display text columns
|
| 101 |
+
|
| 102 |
+
for idx, row in df.iterrows():
|
| 103 |
+
if row["Photo"]:
|
| 104 |
+
st.image(row["Photo"], caption=f"Roll No: {row['Roll No']} - Name: {row['Name']}")
|