forked from librahfacebook/Face
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
166 lines (149 loc) · 5.36 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
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
from UI.face import *
from UI.admain import *
from faceDector import *
from UserManage import *
from Timing import *
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMessageBox
import numpy as np
import sys, os, face_recognition, time
import matplotlib.pyplot as plt
dir='C:\\Users\\libra\\PycharmProjects\\Face\\face\\'
class AdmainWindow(QtWidgets.QDialog, Ui_admainDialog):
def __init__(self):
super(AdmainWindow, self).__init__()
self.setupUi(self)
def takePhoto(self):
name = self.nameEdit.text()
os.chdir(dir)
print(os.getcwd())
if name != "":
if not os.path.exists(dir):
os.mkdir(dir)
faceCapture(name)
self.imgEdit.setText(name + '.jpg')
else:
img_box = QMessageBox.information(self, "拍照", "请输入姓名")
def addUser(self):
dbHelper = DBHelper()
userName = self.nameEdit.text()
userClass = self.classEdit.text()
userImg = self.imgEdit.text()
if userName == "" or userClass == "" or userImg == "":
blank_box = QMessageBox.information(self, "注册", "不能输入空值")
else:
print('Name:{0}, Class:{1}, Image:{2}'.format(userName, userClass, userImg))
sql = "insert into users values (null,'{0}','{1}','{2}')".format(userName, userClass, userImg)
result = dbHelper.execute(sql, None)
sql = "insert into record values (null,'{}',0)".format(userName)
result1 = dbHelper.execute(sql, None)
if result and result1:
success_box = QMessageBox.information(self, "注册", "注册成功")
print("插入数据成功")
else:
fail_box = QMessageBox.information(self, "注册", "注册失败")
print("插入数据失败")
class Mywindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(Mywindow, self).__init__()
self.setupUi(self)
def showCamera(self):
if self.cameraButton.clicked:
os.chdir(dir)
faceCapture('None')
face = QtGui.QPixmap('None.jpg').scaled(115, 120)
self.faceView.setAlignment(Qt.AlignCenter)
self.faceView.setPixmap(face)
def showFace(self):
if self.ImageButton.clicked:
os.chdir(dir)
info = faceRecognize()
if info != False:
self.infoLabel.setText("打卡成功!" + info)
# 数据表中的打卡记录设置为True
addRecord(info)
else:
self.infoLabel.setText("识别错误,重新识别!")
def showAdmain(self):
if self.admainButton.clicked:
admainDialog = AdmainWindow()
admainDialog.show()
admainDialog.exec_()
def showStatis(self):
print("打卡统计")
drawPane()
def faceRecognize():
print(os.getcwd())
# 从数据库中获取已注册的用户脸部图片
dbHelper = DBHelper()
sql = 'select * from users';
all_result = dbHelper.fetchall(sql, None)
# 将jpg文件加载到numpy数组中
imgs = list()
labels = list()
known_encoding = list()
print(len(all_result))
for i in range(len(all_result)):
labels.append(all_result[i][1])
imgs.append(face_recognition.load_image_file(all_result[i][3]))
print("识别中...")
# 获取每个图像文件中每个面部的面部编码
encode = face_recognition.face_encodings(imgs[i])[0]
known_encoding.append(encode)
unknown_image = face_recognition.load_image_file('None.jpg')
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
try:
results = face_recognition.face_distance(known_encoding, unknown_encoding)
print(results)
print(np.min(results))
if np.min(results) > 0.30:
return False
# 找出最小值的下标
min = 1.0
minIndex = 0
for i in range(len(results)):
if results[i] < min:
min = results[i]
minIndex = i
print(labels[minIndex])
return labels[minIndex]
except:
return False
# 识别成功后数据表中的打卡记录设置为1
def addRecord(name):
print('添加记录')
dbHelper = DBHelper()
sql = "update record set success=1 where name=%s"
result = dbHelper.execute(sql, name)
if result:
print("打卡记录添加成功")
else:
print("打卡记录添加失败")
def drawPane():
print("绘制饼状图")
labels = ['Success', 'Failure']
colors = ['red', 'blue']
explode = [0.1, 0]
sizes = []
dh = DBHelper()
sql = "select count(*) from record where success=%s"
success = dh.fetchall(sql, 1)[0][0]
sizes.append(success)
print(success)
sql1 = "select count(*) from record where success=%s"
fail = dh.fetchall(sql1, 0)[0][0]
sizes.append(fail)
print(fail)
plt.axes(aspect=1)
plt.pie(x=sizes, labels=labels, explode=explode, autopct='%3.1f %%',
shadow=True, labeldistance=1.1, startangle=90, pctdistance=0.6)
plt.title("Card Record")
plt.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Mywindow()
window.show()
app.exec_()
# 每天零点定时更新数据库
timerRun(0, 0)
sys.exit(0)