-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
123 lines (103 loc) · 4.44 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
import cv2
import numpy as np
import os
import face_recognition
import pickle
import cvzone
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
from firebase_admin import storage
import numpy as np
from datetime import datetime
cred = credentials.Certificate("serviceAccountKey.json")
firebase_admin.initialize_app(cred, {
'databaseURL': "https://ai-face-recognition-system-default-rtdb.firebaseio.com/",
'storageBucket': "gs://ai-face-recognition-system.appspot.com"
})
bucket = storage.bucket()
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
imgBackground = cv2.imread('Resources/background.png')
# importing the mode images list from the folder
folderModePath = "Resources/Modes/"
modePathList = os.listdir(folderModePath)
imgModeList = []
for modePath in modePathList:
imgModeList.append(cv2.imread(os.path.join(folderModePath, modePath)))
# Load EncodeFile
print("Loading Encode File ...")
file = open('EncodeFile.p', 'rb')
encodeListKnownWithIds = pickle.load(file)
file.close()
encodeListKnown, studentIds = encodeListKnownWithIds
print("Encode File Loaded")
modeType = 0
counter = 0
id = -1
imgStudent = []
try:
while True:
success, img = cap.read()
imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
faceCurFrame = face_recognition.face_locations(imgS)
encodeCurFrame = face_recognition.face_encodings(imgS, faceCurFrame)
imgBackground[162:162 + 480, 55:55 + 640] = img
imgBackground[44:44 + 633, 808:808 + 414] = imgModeList[modeType]
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:
# Get the Data
studentInfo = db.reference(f'Students/{id}').get()
print(studentInfo)
# Get the Image from the storage
blob = bucket.get_blob(f'Images/{id}.png')
if blob is not None:
array = np.frombuffer(blob.download_as_string(), np.uint8)
imgStudent = cv2.imdecode(array, cv2.IMREAD_COLOR)
imgStudent = cv2.resize(imgStudent, (216, 216))
counter = 2
elif counter == 2:
# Add Student Image to Background
imgBackground[175:175 + 216, 909:909 + 216] = imgStudent
counter = 3
elif counter == 3:
# Add Text to the Image
cv2.putText(imgBackground, studentInfo['name'], (861, 95),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(imgBackground, str(studentInfo['total_attendance']), (861, 125),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
date_string = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
cv2.putText(imgBackground, date_string, (861, 210),
cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2, cv2.LINE_AA)
counter = 4
elif counter == 4:
# Save Attendance
today = datetime.now().strftime("%Y-%m-%d")
attendanceRef = db.reference(f'Students/{id}/attendance/{today}')
attendanceRef.set(True)
counter = 5
cv2.imshow("Face Attendance", imgBackground)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except KeyboardInterrupt:
pass
cap.release()
cv2.destroyAllWindows()