-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
224 lines (200 loc) · 9.1 KB
/
Copy pathapp.py
File metadata and controls
224 lines (200 loc) · 9.1 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import streamlit as st
import numpy as np
import cv2
import os
import joblib
from PIL import Image
import pandas as pd
import time
import db_manager
from datetime import datetime
# --- Page Configuration ---
st.set_page_config(page_title="FIST - Fingerprint Identification Student Terminal", layout="wide")
# --- Helper Functions ---
@st.cache_resource
def load_model():
try:
return joblib.load('svm_fingerprint_model.pkl')
except:
return None
def preprocess_image(image_bytes):
try:
file_bytes = np.asarray(bytearray(image_bytes.read()), dtype=np.uint8)
img = cv2.imdecode(file_bytes, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (312, 372))
_, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
return img.flatten().reshape(1, -1)
except Exception as e:
return None
# --- SIDEBAR ---
logo_path = os.path.join('assets', 'logo.png')
if os.path.exists(logo_path):
st.sidebar.image(logo_path, width=150)
st.sidebar.title("Navigation")
# IMPORTANT: Names must match the 'elif' logic below
page = st.sidebar.radio("Go to:", ["Dashboard", "Verify Attendance", "Security Portal", "User Management", "View Logs"])
# --- PAGE 1: DASHBOARD ---
if page == "Dashboard":
st.title("📊 System Dashboard")
db = db_manager.load_data()
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Total Students", len(db['students']))
with col2:
st.metric("Security Personnel", len(db['staff']))
with col3:
st.metric("Attendance Logs", len(db['attendance_logs']))
st.divider()
st.subheader("🛡️ Security Personnel")
if db['staff']:
df_staff = pd.DataFrame(db['staff'])
st.dataframe(df_staff, use_container_width=True, hide_index=True)
else:
st.info("No security personnel registered yet.")
# --- PAGE 2: VERIFY ATTENDANCE (FIXED INDENTATION) ---
elif page == "Verify Attendance":
# Header
col_text, col_img = st.columns([4, 1])
with col_text:
st.title("Fingerprint Verification")
with col_img:
fp_path = os.path.join('assets', 'fing.png')
if os.path.exists(fp_path):
st.image(fp_path, width=80)
# CHECK SYSTEM STATE (Moved OUTSIDE of image logic)
current_state = db_manager.get_system_state()
if current_state == 'Closed':
st.error("🔒 SYSTEM OFFLINE")
st.warning("The system is currently locked. Security Personnel must open the system to allow attendance verification.")
st.info("Please contact security or go to 'Security Portal' to open the system.")
else:
st.success("🔓 SYSTEM ONLINE")
st.write("Upload a fingerprint scan to verify student identity and log attendance.")
model = load_model()
if model is None:
st.error("Model not found. Train the model first.")
else:
col1, col2 = st.columns([2, 2])
with col1:
uploaded_file = st.file_uploader("Scan Fingerprint", type=['jpg', 'png', 'bmp'])
venue = st.selectbox("Venue", ["King Bhekuzulu Hall", "D3/4", "HP 1", "HP 2", "HP 3", "Library", "NE 10", "NE 20", "B422-3 CHAPEL"])
reason = st.selectbox("Reason", ["Lecture", "Exam", "Study", "Event"])
if st.button("Verify & Log Attendance", type="primary"):
if uploaded_file:
with st.spinner("Processing..."):
uploaded_file.seek(0)
features = preprocess_image(uploaded_file)
if features is not None:
pred_id = model.predict(features)[0]
prob = model.predict_proba(features).max()
st.write(f"Debug Info - Confidence: {prob*100:.2f}%")
if prob > 0.10:
db_manager.log_attendance(pred_id, venue, reason)
db = db_manager.load_data()
student_info = next((s for s in db['students'] if s['id'] == pred_id), None)
name = student_info['name'] if student_info else "Unknown"
st.success(f"✅ ACCESS GRANTED")
st.write(f"**Student:** {name} ({pred_id})")
else:
st.error(f"❌ ACCESS DENIED: Confidence too low ({prob*100:.2f}%).")
else:
st.warning("Please upload a scan.")
# --- PAGE: SECURITY PORTAL (Name matches Navigation) ---
elif page == "Security Portal":
st.title("🛡️ Security Personnel Portal")
current_state = db_manager.get_system_state()
if current_state == 'Open':
st.success(f"Current System Status: **OPEN** (Accessible)")
else:
st.error(f"Current System Status: **CLOSED** (Locked)")
st.divider()
col1, col2 = st.columns(2)
with col1:
st.subheader("System Control")
st.write("Security staff can open or close the system.")
staff_id_control = st.text_input("Enter Staff ID to Authorize", key="auth_id")
btn_col1, btn_col2 = st.columns(2)
with btn_col1:
if st.button("🔓 Open System", type="primary"):
if staff_id_control:
db = db_manager.load_data()
if any(s['id'] == staff_id_control for s in db['staff']):
db_manager.set_system_state('Open', staff_id_control)
st.rerun()
else:
st.error("Invalid Staff ID.")
else:
st.warning("Please enter Staff ID.")
with btn_col2:
if st.button("🔒 Close System"):
if staff_id_control:
db = db_manager.load_data()
if any(s['id'] == staff_id_control for s in db['staff']):
db_manager.set_system_state('Closed', staff_id_control)
st.rerun()
else:
st.error("Invalid Staff ID.")
else:
st.warning("Please enter Staff ID.")
with col2:
st.subheader("Shift Logging")
with st.form("shift_form"):
s_id = st.text_input("Staff ID")
venue = st.selectbox("Assigned Venue", ["Main Hall", "Block A", "Computer Lab", "Library", "HP Lab", "D Block", "Chapel"])
if st.form_submit_button("Log Shift Start"):
if s_id:
success, msg = db_manager.log_security_shift(s_id, venue)
if success:
st.success(f"Shift Logged at {venue}")
else:
st.error(msg)
else:
st.error("Enter Staff ID.")
# --- PAGE 3: USER MANAGEMENT ---
elif page == "User Management":
tab1, tab2 = st.tabs(["Register Student", "Register Staff"])
with tab1:
st.subheader("Register New Student")
with st.form("student_form"):
s_id = st.text_input("Student Number (9 digits)")
s_name = st.text_input("Full Name")
s_email = st.text_input("Student Email")
s_year = st.text_input("Year Registered")
if st.form_submit_button("Add Student"):
valid, msg = db_manager.validate_student(s_id, s_email)
if valid:
data = {"id": s_id, "name": s_name, "email": s_email, "year_registered": s_year}
db_manager.add_user('student', data)
st.success(f"Student {s_name} registered!")
else:
st.error(msg)
with tab2:
st.subheader("Register Security Staff")
with st.form("staff_form"):
st_id = st.text_input("Staff ID")
st_name = st.text_input("Full Name")
st_email = st.text_input("Staff Email (@unizulu.ac.za)")
st_year = st.text_input("Year Employed")
if st.form_submit_button("Add Staff"):
valid, msg = db_manager.validate_staff(st_email)
if valid:
data = {"id": st_id, "name": st_name, "email": st_email, "year_employed": st_year}
db_manager.add_user('staff', data)
st.success(f"Staff {st_name} added!")
else:
st.error(msg)
# --- PAGE 4: VIEW LOGS ---
elif page == "View Logs":
st.title("📜 Attendance & Security Logs")
db = db_manager.load_data()
tab1, tab2 = st.tabs(["Student Attendance", "Security Login Tracker"])
with tab1:
if db['attendance_logs']:
st.dataframe(pd.DataFrame(db['attendance_logs']), use_container_width=True)
else:
st.info("No attendance recorded yet.")
with tab2:
if db['security_logs']:
st.dataframe(pd.DataFrame(db['security_logs']), use_container_width=True)
else:
st.info("No security logs yet.")