-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset_creator_test.py
201 lines (180 loc) · 8.75 KB
/
dataset_creator_test.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
import os
import sys
import cv2
import mysql.connector
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTimer
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QMessageBox
import numpy as np
from PIL import Image
class datasetCreator(object):
def __init__(self):
self.UserName = "khush"
self.image = None
self.image_version = 0
self.classifier_path = "classifier.xml"
self.recognizer = cv2.face.LBPHFaceRecognizer_create()
self.image_classifier = cv2.CascadeClassifier(self.classifier_path)
"""STRINGS"""
self.stop = "Stop"
self.failsafe_warning = "Sorry, no unregistered student found.\n\nIf this seems strange to you, " \
"please contact system administrator,\nand go back to Dashboard. "
self.success_message = "Dataset created!\nStudent face registered and is ready for attendance."
self.default_label = "To start live camera feed, click \"Capture\" button.\nClose the window when you're " \
"done training the model. "
self.button_dashboard_text = "Dashboard"
self.button_capture_text = "Capture"
def setupUi(self, form_dataset_creator):
self.timer = QTimer()
form_dataset_creator.setObjectName("form_dataset_creator")
form_dataset_creator.resize(720, 480)
form_dataset_creator.setFixedSize(720, 480)
form_dataset_creator.setWindowTitle("Dataset Creator")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/newPrefix/FaceAttend.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
form_dataset_creator.setWindowIcon(icon)
self.horizontalLayout = QtWidgets.QHBoxLayout(form_dataset_creator)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.comboBox = QtWidgets.QComboBox(form_dataset_creator)
self.comboBox.setLayoutDirection(QtCore.Qt.LeftToRight)
self.comboBox.setFrame(True)
self.comboBox.setObjectName("comboBox")
self.verticalLayout.addWidget(self.comboBox)
self.label_camera_feed = QtWidgets.QLabel(form_dataset_creator)
self.label_camera_feed.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.label_camera_feed.setFrameShadow(QtWidgets.QFrame.Raised)
self.label_camera_feed.setAlignment(QtCore.Qt.AlignCenter)
self.label_camera_feed.setObjectName("label_camera_feed")
self.verticalLayout.addWidget(self.label_camera_feed)
self.button_capture = QtWidgets.QPushButton(form_dataset_creator)
self.button_dashboard = QtWidgets.QPushButton(form_dataset_creator)
self.button_dashboard.setObjectName("button_dashboard")
self.button_capture.setObjectName("button_capture")
self.verticalLayout.addWidget(self.button_capture)
self.verticalLayout.addWidget(self.button_dashboard)
self.horizontalLayout.addLayout(self.verticalLayout)
self.label_camera_feed.setText(self.default_label)
self.button_dashboard.setText(self.button_dashboard_text)
self.button_capture.setText(self.button_capture_text)
self.comboBox.setCurrentIndex(-1)
QtCore.QMetaObject.connectSlotsByName(form_dataset_creator)
self.timer.timeout.connect(self.view_cam)
self.button_capture.clicked.connect(self.controlTimer)
self.connect_db()
self.load_subjects()
self.load_semester()
self.fetch_enroll()
def connect_db(self):
try:
self.connection = mysql.connector.connect(host="localhost", user="root", passwd="",
database="collegeattend")
self.db_cursor = self.connection.cursor()
except Exception as e:
messageBox = QMessageBox()
messageBox.setWindowTitle("Exception Caught!")
messageBox.setText(str(e))
messageBox.setIcon(QMessageBox.Critical)
def load_subjects(self):
self.subjects = []
self.db_cursor.execute("SELECT * FROM subjectteacher WHERE teacherid = %s", (self.UserName,))
subjects_result = self.db_cursor.fetchone()
if subjects_result is None:
print("Sorry! You can't register any faces, as you don't seem to have proper rights.\nPlease contact system administrator.")
else:
for row in subjects_result:
if row != self.UserName:
if row:
self.subjects.append(row)
self.array_length = len(self.subjects)
print("Total " + str(self.array_length) + " subjects found!")
for i in range(self.array_length):
print(self.subjects[i])
i += 1
def load_semester(self):
self.semesters = []
def fetch_enroll(self):
self.failsafe_query = "SELECT * FROM unreg_students"
self.db_cursor.execute(self.failsafe_query)
self.rows = self.db_cursor.fetchall()
if not self.rows:
self.label_camera_feed.setText(self.failsafe_warning)
self.comboBox.hide()
self.button_capture.hide()
else:
self.s_enroll_query = "SELECT s_enroll FROM unreg_students"
self.db_cursor.execute(self.s_enroll_query)
self.query_result = self.db_cursor.fetchall()
for row in self.query_result:
print(str(row[0]))
self.comboBox.addItem(str(row[0]))
def assure_path_exists(self, path):
self.directory = os.path.dirname(path)
if not os.path.exists(self.directory):
os.makedirs(self.directory)
def view_cam(self):
ret, self.image = self.camera_feed.read()
self.image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)
height, width, channel = self.image.shape
step = channel * width
q_img = QImage(self.image.data, width, height, step, QImage.Format_RGB888)
self.label_camera_feed.setPixmap(QPixmap.fromImage(q_img))
self.assure_path_exists("dataset/")
self.main_logic()
def controlTimer(self):
if not self.timer.isActive():
self.camera_feed = cv2.VideoCapture(0, cv2.CAP_DSHOW)
self.timer.start(1)
self.button_capture.setText(self.stop)
self.comboBox.hide()
self.button_dashboard.hide()
else:
self.timer.stop()
self.camera_feed.release()
self.label_camera_feed.setText(self.success_message)
self.button_dashboard.show()
self.button_capture.hide()
self.faces, self.ids = self.trainer_logic("dataset")
self.recognizer.train(self.faces, np.array(self.ids))
self.assure_path_exists("trainer/")
self.recognizer.save("trainer/trainer.yml")
delete_query = "DELETE FROM unreg_students WHERE s_enroll = " + self.comboBox.currentText()
self.db_cursor.execute(delete_query)
self.connection.commit()
def main_logic(self):
self.db_cursor.execute(
"SELECT enrollement FROM studentdetails WHERE enrollement = " + (str(self.comboBox.currentText())))
self.query_result1 = self.db_cursor.fetchall()
for row1 in self.query_result1:
self.s_roll = str(row1[0])
self.faces = self.image_classifier.detectMultiScale(self.image, 1.3, 5)
self.gray_image = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
for (x, y, w, h) in self.faces:
print(str(self.s_roll) + " " + str(self.image_version))
cv2.rectangle(self.image, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imwrite("dataset/" + str(self.s_roll) + "." + str(self.image_version) + ".jpg",
self.gray_image[y:y + h, x:x + w])
self.image_version = self.image_version + 1
def trainer_logic(self, path):
image_paths = [os.path.join(path, f) for f in os.listdir(path)]
list_face_samples = []
list_id = []
for image_path in image_paths:
img_pil = Image.open(image_path).convert('L')
img_numpy = np.array(img_pil, 'uint8')
image_id = int(os.path.split(image_path)[-1].split(".")[0])
print(image_path)
faces_in_images = self.image_classifier.detectMultiScale(img_numpy)
for (x, y, w, h) in faces_in_images:
list_face_samples.append(img_numpy[y:y + h, x:x + w])
list_id.append(image_id)
return list_face_samples, list_id
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dataset = QtWidgets.QWidget()
dataset_creator = datasetCreator()
dataset_creator.setupUi(dataset)
dataset.show()
sys.exit(app.exec_())