-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
137 lines (102 loc) · 3.99 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
from tkinter import *
import cv2
import face_recognition
import numpy as np
import os
import time
from datetime import date
from datetime import datetime
today = date.today()
window = Tk()
window.title("Facial Recognition attendance")
window.minsize(width=1000, height=700)
def openNewWindow():
newWindow = Toplevel(window)
newWindow.title("new student registration")
newWindow.geometry("300x300")
def capture_image():
video = cv2.VideoCapture(0)
while True:
success, image = video.read()
image = np.fliplr(image)
cv2.imshow("image", image)
cv2.imwrite(f"student-images/{inputName.get(1.0, 'end-1c')}-{inputID.get(1.0, 'end-1c')}.png", image)
video.release()
cv2.waitKey(0)
def printInput():
input_name = inputName.get(1.0, "end-1c")
lblName.config(text="name: " + input_name)
input_id = inputID.get(1.0, "end-1c")
lblID.config(text="ID number: " + input_id)
nameLabel = Label(newWindow, text="Name")
nameLabel.pack()
inputName = Text(newWindow, height=1, width=20)
inputName.pack()
IDLabel = Label(newWindow, text="ID Number")
IDLabel.pack()
inputID = Text(newWindow, height=1, width=20)
inputID.pack()
printButton = Button(newWindow, text="Print", command=printInput)
printButton.pack()
lblName = Label(newWindow, text="")
lblName.pack()
lblID = Label(newWindow, text="")
lblID.pack()
button = Button(newWindow, text="capture image", command=capture_image)
button.pack()
def attendanceFunction():
path = "student-images"
images = []
classNames = []
myList = os.listdir(path)
for cl in myList:
currentImg = cv2.imread(f"{path}/{cl}")
images.append(currentImg)
classNames.append(os.path.splitext(cl)[0])
print(classNames)
def findEncodings(images):
encodList = []
for img in images:
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
encode = face_recognition.face_encodings(img)[0]
encodList.append(encode)
return encodList
def markAttendence(name):
with open(f"attendance-{today}.csv", "r+") as file:
myDataList = file.readlines()
nameList = []
for line in myDataList:
entry = line.split(",")
nameList.append(entry[0])
if name not in nameList:
now = datetime.now()
dtString = now.strftime("%H:%M:%S")
file.writelines(f"\n{name},{dtString}")
encodeListKnown = findEncodings(images)
print("Encoding complete")
cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
imgS = cv2.resize(img, (0,0), None, 0.25,0.25)
imgS = cv2.cvtColor(imgS, cv2.COLOR_RGB2BGR)
facesCurrentFrame = face_recognition.face_locations(imgS)
encodingCurrentFrame = face_recognition.face_encodings(imgS, facesCurrentFrame)
for encodeFce, faceLoc in zip(encodingCurrentFrame, facesCurrentFrame):
matches = face_recognition.compare_faces(encodeListKnown, encodeFce)
faceDistance = face_recognition.face_distance(encodeListKnown, encodeFce)
matchIndex = np.argmin(faceDistance)
if matches[matchIndex]:
name = classNames[matchIndex].upper()
y1, x2, y2, x1 = faceLoc
y1, x2, y2, x1 = y1*4, x2*4, y2*4, x1*4
cv2.rectangle(img, (x1,y1), (x2,y2), (0,255,0), 2)
cv2.rectangle(img, (x1,y2 - 35), (x2,y2), (0,255,0), cv2.FILLED)
cv2.putText(img, name, (x1+6, y2-6), cv2.FONT_HERSHEY_COMPLEX, 1, (255,255,255), 2)
markAttendence(name)
cv2.imshow("webcam", img)
cv2.waitKey(1)
newStudentBtn = Button(text="Register new student", command=openNewWindow)
newStudentBtn.pack()
attenceButton = Button(text="Mark the attendance", command=attendanceFunction)
attenceButton.pack()
window.mainloop()