Skip to content

Commit 2ca1923

Browse files
committed
Adding gitignore
1 parent 5ee56e4 commit 2ca1923

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
venv

main.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import os
2+
import pickle
3+
import cv2
4+
import face_recognition
5+
import cvzone
6+
import numpy as np
7+
from datetime import datetime
8+
9+
10+
cap = cv2.VideoCapture(0)
11+
cap.set(3, 640)
12+
cap.set(4, 480)
13+
14+
imgBackground = cv2.imread('Resources/background.png')
15+
16+
# Importing the mode images into a list
17+
folderModePath = 'Resources/Modes'
18+
modePathList = os.listdir(folderModePath)
19+
imgModeList = []
20+
for path in modePathList:
21+
imgModeList.append(cv2.imread(os.path.join(folderModePath, path)))
22+
# print(len(imgModeList))
23+
24+
# Load the encoding file
25+
print("Loading Encode File ...")
26+
file = open('EncodeFile.p', 'rb')
27+
encodeListKnownWithIds = pickle.load(file)
28+
file.close()
29+
encodeListKnown, studentIds = encodeListKnownWithIds
30+
# print(studentIds)
31+
print("Encode File Loaded")
32+
33+
modeType = 0
34+
counter = 0
35+
id = -1
36+
imgStudent = []
37+
38+
while True:
39+
success, img = cap.read()
40+
41+
imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
42+
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
43+
44+
faceCurFrame = face_recognition.face_locations(imgS)
45+
encodeCurFrame = face_recognition.face_encodings(imgS, faceCurFrame)
46+
47+
imgBackground[162:162 + 480, 55:55 + 640] = img
48+
imgBackground[44:44 + 633, 808:808 + 414] = imgModeList[modeType]
49+
50+
if faceCurFrame:
51+
for encodeFace, faceLoc in zip(encodeCurFrame, faceCurFrame):
52+
matches = face_recognition.compare_faces(encodeListKnown, encodeFace)
53+
faceDis = face_recognition.face_distance(encodeListKnown, encodeFace)
54+
# print("matches", matches)
55+
# print("faceDis", faceDis)
56+
57+
matchIndex = np.argmin(faceDis)
58+
# print("Match Index", matchIndex)
59+
60+
if matches[matchIndex]:
61+
# print("Known Face Detected")
62+
# print(studentIds[matchIndex])
63+
y1, x2, y2, x1 = faceLoc
64+
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
65+
bbox = 55 + x1, 162 + y1, x2 - x1, y2 - y1
66+
imgBackground = cvzone.cornerRect(imgBackground, bbox, rt=0)
67+
id = studentIds[matchIndex]
68+
if counter == 0:
69+
cvzone.putTextRect(imgBackground, "Loading", (275, 400))
70+
cv2.imshow("Face Attendance", imgBackground)
71+
cv2.waitKey(1)
72+
counter = 1
73+
modeType = 1
74+
75+
if counter != 0:
76+
77+
if counter == 1:
78+
# Get the Data
79+
studentInfo = db.reference(f'Students/{id}').get()
80+
print(studentInfo)
81+
# Get the Image from the storage
82+
blob = bucket.get_blob(f'Images/{id}.png')
83+
array = np.frombuffer(blob.download_as_string(), np.uint8)
84+
imgStudent = cv2.imdecode(array, cv2.COLOR_BGRA2BGR)
85+
# Update data of attendance
86+
datetimeObject = datetime.strptime(studentInfo['last_attendance_time'],
87+
"%Y-%m-%d %H:%M:%S")
88+
secondsElapsed = (datetime.now() - datetimeObject).total_seconds()
89+
print(secondsElapsed)
90+
if secondsElapsed > 30:
91+
ref = db.reference(f'Students/{id}')
92+
studentInfo['total_attendance'] += 1
93+
ref.child('total_attendance').set(studentInfo['total_attendance'])
94+
ref.child('last_attendance_time').set(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
95+
else:
96+
modeType = 3
97+
counter = 0
98+
imgBackground[44:44 + 633, 808:808 + 414] = imgModeList[modeType]
99+
100+
if modeType != 3:
101+
102+
if 10 < counter < 20:
103+
modeType = 2
104+
105+
imgBackground[44:44 + 633, 808:808 + 414] = imgModeList[modeType]
106+
107+
if counter <= 10:
108+
cv2.putText(imgBackground, str(studentInfo['total_attendance']), (861, 125),
109+
cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 1)
110+
cv2.putText(imgBackground, str(studentInfo['major']), (1006, 550),
111+
cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
112+
cv2.putText(imgBackground, str(id), (1006, 493),
113+
cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
114+
cv2.putText(imgBackground, str(studentInfo['standing']), (910, 625),
115+
cv2.FONT_HERSHEY_COMPLEX, 0.6, (100, 100, 100), 1)
116+
cv2.putText(imgBackground, str(studentInfo['year']), (1025, 625),
117+
cv2.FONT_HERSHEY_COMPLEX, 0.6, (100, 100, 100), 1)
118+
cv2.putText(imgBackground, str(studentInfo['starting_year']), (1125, 625),
119+
cv2.FONT_HERSHEY_COMPLEX, 0.6, (100, 100, 100), 1)
120+
121+
(w, h), _ = cv2.getTextSize(studentInfo['name'], cv2.FONT_HERSHEY_COMPLEX, 1, 1)
122+
offset = (414 - w) // 2
123+
cv2.putText(imgBackground, str(studentInfo['name']), (808 + offset, 445),
124+
cv2.FONT_HERSHEY_COMPLEX, 1, (50, 50, 50), 1)
125+
126+
imgBackground[175:175 + 216, 909:909 + 216] = imgStudent
127+
128+
counter += 1
129+
130+
if counter >= 20:
131+
counter = 0
132+
modeType = 0
133+
studentInfo = []
134+
imgStudent = []
135+
imgBackground[44:44 + 633, 808:808 + 414] = imgModeList[modeType]
136+
else:
137+
modeType = 0
138+
counter = 0
139+
# cv2.imshow("Webcam", img)
140+
cv2.imshow("Face Attendance", imgBackground)
141+
cv2.waitKey(1)

0 commit comments

Comments
 (0)