-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython.py
More file actions
238 lines (176 loc) · 6.69 KB
/
Copy pathPython.py
File metadata and controls
238 lines (176 loc) · 6.69 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import cv2
import os
import pandas as pd
harcascadePath = cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
detector = cv2.CascadeClassifier(harcascadePath)
if detector.empty():
print("[ERROR] Could not load Haar Cascade XML.")
exit()
cam = cv2.VideoCapture(0)
if not cam.isOpened():
print("[ERROR] Could not access the camera.")
exit()
# ====== INPUT DETAILS ======
name = input('\nEnter your Name: ')
roll_number = input('Enter your Roll Number: ')
face_id = input('Enter user ID (numeric): ')
print("\n[INFO] Initializing face capture. Look at camera...")
count = 0
os.makedirs("dataset", exist_ok=True)
# ====== CREATE STUDENT DATABASE FILE ======
student_file = "students.xlsx"
if not os.path.exists(student_file):
df = pd.DataFrame(columns=["ID", "Name", "Roll"])
df.to_excel(student_file, index=False)
df = pd.read_excel(student_file)
# Prevent duplicate ID
if int(face_id) in df["ID"].values:
print("ID already exists!")
else:
new_row = {"ID": int(face_id), "Name": name, "Roll": roll_number}
df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
df.to_excel(student_file, index=False)
print("Student details saved.")
# ====== FACE CAPTURE ======
while True:
ret, img = cam.read()
if not ret:
break
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)
count += 1
face_img = gray[y:y+h, x:x+w]
filename = f"dataset/User.{face_id}.{count}.jpg"
cv2.imwrite(filename, face_img)
cv2.imshow('image', img)
if cv2.waitKey(100) & 0xFF == ord('q'):
break
elif count >= 80:
break
print("\n[INFO] Face Capture Completed")
cam.release()
cv2.destroyAllWindows()
#3.1.7 PYTHON CODE:-
import cv2
import pandas as pd
import serial
import os
import time
from datetime import datetime
# ================= SERIAL =================
try:
ser = serial.Serial('COM6', 9600, timeout=1)
time.sleep(2)
print("ESP32 Connected")
except:
print("ESP32 Not Connected")
ser = None
# ================= LOAD MODEL =================
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read("trainer.yml")
faceCascade = cv2.CascadeClassifier(
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
)
students_df = pd.read_excel("students.xlsx")
attendance_file = "attendance.xlsx"
# ================= CREATE FILE IF NOT EXISTS =================
if not os.path.exists(attendance_file):
df = pd.DataFrame(columns=["ID","Name","Roll","Date","Period","Time"])
df.to_excel(attendance_file, index=False)
# =====================================================
def take_attendance():
period = input("Enter Period (1-6): ")
today = datetime.now().strftime("%Y-%m-%d")
cam = cv2.VideoCapture(0)
if not cam.isOpened():
print("Camera not working!")
return
print("\nAttendance Mode Started (Press Q to Exit)\n")
session_present = set()
while True:
ret, img = cam.read()
if not ret:
break
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
id, confidence = recognizer.predict(gray[y:y+h, x:x+w])
if confidence < 50:
student = students_df[students_df["ID"] == id]
if not student.empty:
name = student.iloc[0]["Name"]
roll = student.iloc[0]["Roll"]
current_time = datetime.now().strftime("%H:%M:%S")
if id not in session_present:
print(f"{name} is PRESENT")
session_present.add(id)
# 🔹 Send to ESP32 (LCD will show PRESENT)
if ser:
message = f"PRESENT,{id},{name},{period}\n"
ser.write(message.encode())
time.sleep(0.5)
# ===== SAVE TO EXCEL =====
df = pd.read_excel(attendance_file)
already_marked = df[
(df["ID"] == id) &
(df["Date"] == today) &
(df["Period"].astype(str) == str(period))
]
if already_marked.empty:
new_row = {
"ID": id,
"Name": name,
"Roll": roll,
"Date": today,
"Period": period,
"Time": current_time
}
df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
df.to_excel(attendance_file, index=False)
label = name
else:
label = "UNKNOWN"
else:
label = "UNKNOWN"
cv2.rectangle(img, (x, y), (x+w, y+h), (0,255,0), 2)
cv2.putText(img, label, (x, y-10),
cv2.FONT_HERSHEY_SIMPLEX,
1, (255,255,255), 2)
cv2.imshow("Attendance System", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
# ================= ABSENT CHECK =================
df = pd.read_excel(attendance_file)
present_ids = df[
(df["Date"] == today) &
(df["Period"].astype(str) == str(period))
]["ID"].tolist()
print("\n===== ABSENT STUDENTS =====")
for _, row in students_df.iterrows():
if row["ID"] not in present_ids:
print(f"{row['Name']} is ABSENT")
# 🔹 Send ABSENT to ESP32 (LCD + SMS trigger)
if ser:
message = f"ABSENT,{row['ID']},{row['Name']},{period}\n"
ser.write(message.encode())
time.sleep(2)
print("\nAttendance Mode Closed\n")
# =====================================================
def main():
while True:
print("========= FACE ATTENDANCE SYSTEM =========")
print("1 - Take Attendance")
print("2 - Exit")
choice = input("Enter choice: ")
if choice == '1':
take_attendance()
elif choice == '2':
print("System Closed")
break
else:
print("Invalid choice\n")
main()