-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
246 lines (201 loc) · 9.42 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import sys
import cv2
# from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from src.facebase import FRFace, FDFace
# _fromUtf8 = QtCore.QString.fromUtf8
class ARCFaceUI(QWidget):
def __init__(self):
super(ARCFaceUI, self).__init__()
self.true_face = False
self.face_detected = False
self.det_box = None
self.freezeLogo = False # True-> freeze, False-> allowing changing logo
self.frameratio = 0.6
self.framesize = (800, 450)
self.crop_size = (224, 224)
self.logo_size = (112*2, 96*2)
self.initUI()
self.recog_face = FRFace() # face recognition thread class
self.detect_face = FDFace(self.frameratio, self.crop_size)# face detection class
self.camera = cv2.VideoCapture(0) # init camera
# self.through = Through()# 判断置信度,决定是否通过,并记录通过者的信息到list中
##### Timer #####
self.frametimer = QTimer(self)
self.frametimer.timeout.connect(self.updateFrame)
self.frametimer.start(20)
self.logotimer = QTimer(self)
self.logotimer.timeout.connect(self.unfreezeLogo)
self.logotimer.start(2000)
self.recogtimer = QTimer(self)
self.recogtimer.timeout.connect(self.startFR)
self.recogtimer.start(2000)
self.recog_face.recog_msg.connect(self.getResult)
self.frametimer.timeout.connect(self.updateFrame) # updateFrame(decide whether do recog)
# self.connect(self.timer, SIGNAL('timeout()'), self.updateFrame) # connect连接 定时器timer 每到20ms 就触发更新画面的函数
# self.through.pass_sign.connect(self.updateLogo)# 根据pass_sign信号(为一个str参数)决定是否需要更新UI界面上的logo
'''
# 与服务器交换数据
self.connect_sever = ConnectSever()# 连接到服务器,进行相应的图片同步操作
self.connect_sever.finished.connect(self.connect_sever.start) # 触发服务器与本地数据的同步,ConnectSever类里run一次sleep(60)再执行服务器同步
self.connect_sever.change.connect(self.arcface.getUpdateData)# 根据change信号(为一个list参数)决定是否需要更新数据到本地
self.connect_sever.start()# 程序一开始即执行服务器与本地数据同步
self.through.finished.connect(self.toSever)# through线程判断置信度之后,触发向服务器传图片的toSever函数
'''
def unfreezeLogo(self):
self.freezeLogo = False
def initUI(self):
# self.resize(600, 480)
# self.move(50, 50)
self.setGeometry(50,50, self.framesize[0], self.framesize[1]) # ax, ay, w, h
self.setWindowTitle('Intelligent Guard System')
# print(self.width(), self.height())
self.exit_button = QPushButton('Exit')
self.exit_button.clicked.connect(self.exit_ARC)
# 摄像机画面的标签
self.camera_label = QLabel(self)
self.camera_label.setText('')
self.camera_label.setObjectName('Camera')
self.camera_label.setScaledContents(True) # adaptively scale
# self.camera_label.adjustSize()
# self.camera_label.setGeometry(0, 0, 800, 600)
# 检测结果标签
self.detect_label = QLabel(self)
self.detect_label.setObjectName('detect_label')
self.detect_label.setText('Identity Name:')
self.name_text = QLineEdit()
self.name_text.setText(' ')
# 检测 结果的置信度
self.confidence_label = QLabel(self)
self.confidence_label.setObjectName('confidence')
self.confidence_label.setText('Confidence:')
self.confid_text = QLineEdit()
self.confid_text.setText(' ')
# 图标标签
self.icon_label = QLabel(self)
self.logo_png = QtGui.QPixmap('./src/logo.png')
self.logo_png = self.logo_png.scaled(self.logo_size[1], self.logo_size[0], aspectRatioMode=Qt.KeepAspectRatio)
# self.pass_png = QtGui.QPixmap('./src/pass.png')
self.icon_label.setPixmap(self.logo_png)
self.icon_label.resize(self.logo_size[1], self.logo_size[0])
self.setlayout()
QtCore.QMetaObject.connectSlotsByName(self) # automatic connect signal to slot on widget
def startFR(self):
if self.true_face and (self.det_box is not None):
self.recog_face.image = self.det_box
self.recog_face.start()
self.true_face = False
def exit_ARC(self):
self.recog_face.exit() # exit face recog thread
QCoreApplication.instance().quit() # exit window
print("=> Happy Ending...")
def getResult(self, msg):
self.result = self.recog_face.result_face
self.updateResult(msg)
# update result to GUI
def updateResult(self, msg):
##### Recog Result ######
if self.freezeLogo: # in freeze time, do not update any result
return
elif msg == "valid face": # and self.true_face:
p = self.result.p # confidence
faceName = self.result.faceName # result face name
self.confid_text.setText(str(p))
self.name_text.setText(faceName)
self.updateLogo(passthrough='yes')
else:
p = 0.0
faceName = 'Unknown'
self.confid_text.setText(str(p))
self.name_text.setText(faceName)
self.updateLogo(passthrough='no')
# self.confid_text.setText(_fromUtf8(str(p)))
# self.name_text.setText(_fromUtf8(faceName))
def updateLogo(self, passthrough='no'):
##### update logo with database corresponding face #####
if passthrough == 'yes':
qtimg = QtGui.QPixmap(self.result.facePath)
# qtimg = qtimg.scaled(300, 400, aspectRatioMode=Qt.KeepAspectRatio)
qtimg = qtimg.scaled(self.logo_size[1], self.logo_size[0], aspectRatioMode=Qt.KeepAspectRatio)
# print(qtimg.height(), qtimg.width())
self.icon_label.setPixmap(qtimg)
self.freezeLogo = True
self.logotimer.start(1000) # restart timer from freezeLogo set True
# self.icon_label.setScaledContents(True) # image adapt to label
else:
self.icon_label.setPixmap(self.logo_png)
def setlayout(self):
# hbox_button = QHBoxLayout()
# hbox_button.addStretch(1)
# hbox_button.addWidget(self.init_face_button)
# hbox_button.addWidget(self.exit_button)
# 创建水平布局
self.hbox = QHBoxLayout()
self.hbox.addStretch(1)
self.hbox_left = QHBoxLayout()
self.hbox_left.addStretch(1)
self.vbox_right = QVBoxLayout()
self.vbox_right.addStretch(1)
# self.vbox_ = QVBoxLayout()
# self.vbox_.addStretch(1)
# 创建网格布局
self.grid_right = QGridLayout()
self.grid_right.addWidget(self.detect_label, 0, 0) # 添加姓名标签
self.grid_right.addWidget(self.name_text, 0, 1)
self.grid_right.addWidget(self.confidence_label, 1, 0) # 置信度标签
self.grid_right.addWidget(self.confid_text, 1, 1)
# self.grid_right.addWidget(self.exit_button, 8, 0)
self.hbox_left.addWidget(self.camera_label) # 摄像头画面标签
self.vbox_right.addWidget(self.icon_label)
self.vbox_right.addLayout(self.grid_right)
self.vbox_right.addWidget(self.exit_button)
# self.hbox_left.setScaledContents()
self.hbox.addLayout(self.hbox_left)
self.hbox.addLayout(self.vbox_right)
# self.vbox_.addLayout(self.hbox)
# 应用布局
self.setLayout(self.hbox)
'''
# 把通过的识别到的人脸图片上传到服务器
def toSever(self):
self.updateLogo()#如果人脸通过,则更换logo图标
if len(self.through.through_lists) > 0:
self.connect_sever.inputUploadData(self.through.through_lists.pop())
'''
# 格式转化
def fromFrame2QImage(self, frame):
height, width, bytesPerComponent = frame.shape
bytesPerLine = bytesPerComponent * width
# 变换彩色空间顺序
cv2.cvtColor(frame, cv2.COLOR_BGR2RGB, frame)
# 转为QImage对象
image = QImage(frame.data, width, height, bytesPerLine, QImage.Format_RGB888)
return image
# 更新摄像头画面
def updateFrame(self):
ret, frame = self.camera.read()
frame_output, det_box, face_detected, true_face = self.detect_face.detectFace(frame)
self.true_face = true_face
self.face_detected = face_detected
self.det_box = det_box
if not self.true_face:
p = 0
faceName = 'Unknown'
self.confid_text.setText(str(p))
self.name_text.setText(faceName)
self.updateLogo(passthrough='no')
frame_Qimage = self.fromFrame2QImage(frame_output)
frame_Qimage = QtGui.QPixmap.fromImage(frame_Qimage)
frame_Qimage = frame_Qimage.scaled(self.framesize[0], self.framesize[1], aspectRatioMode=Qt.KeepAspectRatio)
# print(frame_Qimage.height(), frame_Qimage.width())
self.camera_label.setPixmap(frame_Qimage)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = ARCFaceUI()
print(window.size())
window.show()
sys.exit(app.exec_())
print("=> Happy Ending...")