-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
146 lines (122 loc) · 5.62 KB
/
test.py
File metadata and controls
146 lines (122 loc) · 5.62 KB
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
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]
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
def face_rec_(self, frame, encode_list_known, class_names):
def mark_attendance(name):
if self.Hadir.isChecked():
self.Hadir.setEnabled(False)
with open('Daftar_Hadir.csv', 'a') as f:
if (name != 'Tidak terdaftar'):
date_time_string = datetime.datetime.now().strftime("%y/%m/%d %H:%M:%S")
f.writelines(f'\n{name},{date_time_string},Presensi')
self.Hadir.setChecked(False)
self.NameLabel.setText(name)
self.StatusLabel.setText('Anda sudah melakukan absensi!')
self.MinLabel.setText('')
self.Time1 = datetime.datetime.now()
self.Hadir.setEnabled(True)
faces_cur_frame = face_recognition.face_locations(frame)
encodes_cur_frame = face_recognition.face_encodings(frame, faces_cur_frame)
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 = "Tidak terdaftar"
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('Daftar_Hadir.csv', "r") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
for field in row:
if field in row:
if field == 'Clock In':
if row[0] == name:
Time1 = (datetime.datetime.strptime(row[1], '%y/%m/%d %H:%M:%S'))
self.TimeList1.append(Time1)
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):
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)