-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
186 lines (157 loc) · 7.01 KB
/
main.py
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
import os
import pickle
import numpy as np
import cv2
import face_recognition
import cvzone
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
from firebase_admin import storage
from datetime import datetime
from dotenv import load_dotenv
load_dotenv()
firebase_credentials_path = os.getenv('FIREBASE_CREDENTIALS_PATH')
database_url = os.getenv('DATABASE_URL')
storage_bucket = os.getenv('STORAGE_BUCKET')
# Initialize Firebase
cred = credentials.Certificate(firebase_credentials_path)
firebase_admin.initialize_app(cred, {
'databaseURL': database_url,
'storageBucket': storage_bucket
})
bucket = storage.bucket()
# Initialize VideoCapture
cap = cv2.VideoCapture(0) # Changed to 0, ensure the correct camera index
cap.set(3, 640)
cap.set(4, 480)
# Load background image
imgBackground = cv2.imread('Resources/background.png')
if imgBackground is None:
raise ValueError("Background image could not be loaded.")
# Load mode images
folderModePath = 'Resources/Modes'
modePathList = os.listdir(folderModePath)
imgModeList = []
for path in modePathList:
img = cv2.imread(os.path.join(folderModePath, path))
if img is not None:
imgModeList.append(img)
else:
print(f"Warning: {path} could not be loaded.")
# Load encoding file
print("Loading Encode File ...")
with open('EncodeFile.p', 'rb') as file:
encodeListKnownWithIds = pickle.load(file)
encodeListKnown, studentIds = encodeListKnownWithIds
print("Encode File Loaded")
modeType = 0
counter = 0
id = -1
imgStudent = []
while True:
success, img = cap.read()
if not success:
print("Failed to read from camera.")
continue
# Resize and convert image
imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
if imgS is None or imgS.size == 0:
print("Resized image is invalid.")
continue
# Convert to RGB
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
if imgS.dtype != np.uint8 or len(imgS.shape) != 3 or imgS.shape[2] != 3:
print("Image is not in the expected RGB format.")
continue
# Detect faces
try:
faceCurFrame = face_recognition.face_locations(imgS)
encodeCurFrame = face_recognition.face_encodings(imgS, faceCurFrame)
except RuntimeError as e:
print(f"RuntimeError: {e}")
continue
# Update background image
imgBackground[162:162 + 480, 55:55 + 640] = img
imgBackground[44:44 + 633, 808:808 + 414] = imgModeList[modeType]
if faceCurFrame:
for encodeFace, faceLoc in zip(encodeCurFrame, faceCurFrame):
matches = face_recognition.compare_faces(encodeListKnown, encodeFace)
faceDis = face_recognition.face_distance(encodeListKnown, encodeFace)
matchIndex = np.argmin(faceDis)
if matches[matchIndex]:
y1, x2, y2, x1 = faceLoc
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
bbox = 55 + x1, 162 + y1, x2 - x1, y2 - y1
imgBackground = cvzone.cornerRect(imgBackground, bbox, rt=0)
id = studentIds[matchIndex]
if counter == 0:
cvzone.putTextRect(imgBackground, "Loading", (275, 400))
cv2.imshow("Face Attendance", imgBackground)
cv2.waitKey(1)
counter = 1
modeType = 1
if counter != 0:
if counter == 1:
studentInfo = db.reference(f'Students/{id}').get()
print(studentInfo)
# Load student image from Firebase Storage
blob = bucket.get_blob(f'Images/{id}.png')
if blob is None:
print(f"Student image {id}.png not found in Firebase Storage.")
continue
array = np.frombuffer(blob.download_as_string(), np.uint8)
imgStudent = cv2.imdecode(array, cv2.IMREAD_COLOR)
if imgStudent is None:
print("Failed to decode student image.")
continue
# Update attendance
datetimeObject = datetime.strptime(studentInfo['last_attendance_time'], "%Y-%m-%d %H:%M:%S")
secondsElapsed = (datetime.now() - datetimeObject).total_seconds()
print(secondsElapsed)
if secondsElapsed > 30:
ref = db.reference(f'Students/{id}')
studentInfo['total_attendance'] += 1
ref.child('total_attendance').set(studentInfo['total_attendance'])
ref.child('last_attendance_time').set(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
else:
modeType = 3
counter = 0
imgBackground[44:44 + 633, 808:808 + 414] = imgModeList[modeType]
if modeType != 3:
if 10 < counter < 20:
modeType = 2
imgBackground[44:44 + 633, 808:808 + 414] = imgModeList[modeType]
if counter <= 10:
cv2.putText(imgBackground, str(studentInfo['total_attendance']), (861, 125),
cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 1)
cv2.putText(imgBackground, str(studentInfo['major']), (1006, 550),
cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
cv2.putText(imgBackground, str(id), (1006, 493),
cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
cv2.putText(imgBackground, str(studentInfo['standing']), (910, 625),
cv2.FONT_HERSHEY_COMPLEX, 0.6, (100, 100, 100), 1)
cv2.putText(imgBackground, str(studentInfo['year']), (1025, 625),
cv2.FONT_HERSHEY_COMPLEX, 0.6, (100, 100, 100), 1)
cv2.putText(imgBackground, str(studentInfo['starting_year']), (1125, 625),
cv2.FONT_HERSHEY_COMPLEX, 0.6, (100, 100, 100), 1)
(w, h), _ = cv2.getTextSize(studentInfo['name'], cv2.FONT_HERSHEY_COMPLEX, 1, 1)
offset = (414 - w) // 2
cv2.putText(imgBackground, str(studentInfo['name']), (808 + offset, 445),
cv2.FONT_HERSHEY_COMPLEX, 1, (50, 50, 50), 1)
imgBackground[175:175 + 216, 909:909 + 216] = imgStudent
counter += 1
if counter >= 20:
counter = 0
modeType = 0
studentInfo = []
imgStudent = []
imgBackground[44:44 + 633, 808:808 + 414] = imgModeList[modeType]
else:
modeType = 0
counter = 0
cv2.imshow("Face Attendance", imgBackground)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()