-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathout_window.py
221 lines (195 loc) · 9.51 KB
/
out_window.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
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
# Modified by Augmented Startups & Geeky Bee
# October 2020
# Facial Recognition Attendence GUI
# Full Course - https://augmentedstartups.info/yolov4release
# *-
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.uic import loadUi
from PyQt5.QtCore import pyqtSlot, QTimer, QDate, Qt
from PyQt5.QtWidgets import QDialog,QMessageBox
import cv2
import face_recognition
import numpy as np
import datetime
import os
import csv
class Ui_OutputDialog(QDialog):
def __init__(self):
super(Ui_OutputDialog, self).__init__()
loadUi("./outputwindow.ui", self)
#Update time
now = QDate.currentDate()
current_date = now.toString('ddd dd MMMM yyyy')
current_time = datetime.datetime.now().strftime("%I:%M %p")
self.Date_Label.setText(current_date)
self.Time_Label.setText(current_time)
self.image = None
@pyqtSlot()
def startVideo(self, camera_name):
"""
:param camera_name: link of camera or usb camera
:return:
"""
if len(camera_name) == 1:
self.capture = cv2.VideoCapture(int(camera_name))
else:
self.capture = cv2.VideoCapture(camera_name)
self.timer = QTimer(self) # Create Timer
path = 'ImagesAttendance'
if not os.path.exists(path):
os.mkdir(path)
# known face encoding and known face name list
images = []
self.class_names = []
self.encode_list = []
self.TimeList1 = []
self.TimeList2 = []
attendance_list = os.listdir(path)
# print(attendance_list)
for cl in attendance_list:
cur_img = cv2.imread(f'{path}/{cl}')
images.append(cur_img)
self.class_names.append(os.path.splitext(cl)[0])
for img in images:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
boxes = face_recognition.face_locations(img)
encodes_cur_frame = face_recognition.face_encodings(img, boxes)[0]
# encode = face_recognition.face_encodings(img)[0]
self.encode_list.append(encodes_cur_frame)
self.timer.timeout.connect(self.update_frame) # Connect timeout to the output function
self.timer.start(10) # emit the timeout() signal at x=40ms
print(self.class_names)
def face_rec_(self, frame, encode_list_known, class_names):
"""
:param frame: frame from camera
:param encode_list_known: known face encoding
:param class_names: known face names
:return:
"""
# csv
def mark_attendance(name):
"""
:param name: detected face known or unknown one
:return:
"""
#print("In Attendance")
if self.ClockInButton.isChecked():
self.ClockInButton.setEnabled(False)
with open('Attendance.csv', 'a') as f:
if (name != 'unknown'):
buttonReply = QMessageBox.question(self, 'Welcome ' + name, 'Are you Clocking In?' ,
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if buttonReply == QMessageBox.Yes:
date_time_string = datetime.datetime.now().strftime("%y/%m/%d %H:%M:%S")
f.writelines(f'\n{name},{date_time_string},Clock In')
self.ClockInButton.setChecked(False)
self.NameLabel.setText(name)
self.StatusLabel.setText('Clocked In')
self.HoursLabel.setText('Measuring')
self.MinLabel.setText('')
#self.CalculateElapse(name)
#print('Yes clicked and detected')
self.Time1 = datetime.datetime.now()
#print(self.Time1)
self.ClockInButton.setEnabled(True)
else:
print('Not clicked.')
self.ClockInButton.setEnabled(True)
elif self.ClockOutButton.isChecked():
self.ClockOutButton.setEnabled(False)
with open('Attendance.csv', 'a') as f:
if (name != 'unknown'):
buttonReply = QMessageBox.question(self, 'Cheers ' + name, 'Are you Clocking Out?',
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if buttonReply == QMessageBox.Yes:
date_time_string = datetime.datetime.now().strftime("%y/%m/%d %H:%M:%S")
f.writelines(f'\n{name},{date_time_string},Clock Out')
self.ClockOutButton.setChecked(False)
self.NameLabel.setText(name)
self.StatusLabel.setText('Clocked Out')
self.Time2 = datetime.datetime.now()
#print(self.Time2)
self.ElapseList(name)
self.TimeList2.append(datetime.datetime.now())
CheckInTime = self.TimeList1[-1]
CheckOutTime = self.TimeList2[-1]
self.ElapseHours = (CheckOutTime - CheckInTime)
self.MinLabel.setText("{:.0f}".format(abs(self.ElapseHours.total_seconds() / 60)%60) + 'm')
self.HoursLabel.setText("{:.0f}".format(abs(self.ElapseHours.total_seconds() / 60**2)) + 'h')
self.ClockOutButton.setEnabled(True)
else:
print('Not clicked.')
self.ClockOutButton.setEnabled(True)
# face recognition
faces_cur_frame = face_recognition.face_locations(frame)
encodes_cur_frame = face_recognition.face_encodings(frame, faces_cur_frame)
# count = 0
for encodeFace, faceLoc in zip(encodes_cur_frame, faces_cur_frame):
match = face_recognition.compare_faces(encode_list_known, encodeFace, tolerance=0.50)
face_dis = face_recognition.face_distance(encode_list_known, encodeFace)
name = "unknown"
best_match_index = np.argmin(face_dis)
# print("s",best_match_index)
if match[best_match_index]:
name = class_names[best_match_index].upper()
y1, x2, y2, x1 = faceLoc
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.rectangle(frame, (x1, y2 - 20), (x2, y2), (0, 255, 0), cv2.FILLED)
cv2.putText(frame, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
mark_attendance(name)
return frame
def showdialog(self):
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText("This is a message box")
msg.setInformativeText("This is additional information")
msg.setWindowTitle("MessageBox demo")
msg.setDetailedText("The details are as follows:")
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
def ElapseList(self,name):
with open('Attendance.csv', "r") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 2
Time1 = datetime.datetime.now()
Time2 = datetime.datetime.now()
for row in csv_reader:
for field in row:
if field in row:
if field == 'Clock In':
if row[0] == name:
#print(f'\t ROW 0 {row[0]} ROW 1 {row[1]} ROW2 {row[2]}.')
Time1 = (datetime.datetime.strptime(row[1], '%y/%m/%d %H:%M:%S'))
self.TimeList1.append(Time1)
if field == 'Clock Out':
if row[0] == name:
#print(f'\t ROW 0 {row[0]} ROW 1 {row[1]} ROW2 {row[2]}.')
Time2 = (datetime.datetime.strptime(row[1], '%y/%m/%d %H:%M:%S'))
self.TimeList2.append(Time2)
#print(Time2)
def update_frame(self):
ret, self.image = self.capture.read()
self.displayImage(self.image, self.encode_list, self.class_names, 1)
def displayImage(self, image, encode_list, class_names, window=1):
"""
:param image: frame from camera
:param encode_list: known face encoding list
:param class_names: known face names
:param window: number of window
:return:
"""
image = cv2.resize(image, (640, 480))
try:
image = self.face_rec_(image, encode_list, class_names)
except Exception as e:
print(e)
qformat = QImage.Format_Indexed8
if len(image.shape) == 3:
if image.shape[2] == 4:
qformat = QImage.Format_RGBA8888
else:
qformat = QImage.Format_RGB888
outImage = QImage(image, image.shape[1], image.shape[0], image.strides[0], qformat)
outImage = outImage.rgbSwapped()
if window == 1:
self.imgLabel.setPixmap(QPixmap.fromImage(outImage))
self.imgLabel.setScaledContents(True)