forked from MagicEcoSummer/JuicyBrosProto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
528 lines (408 loc) · 17.1 KB
/
client.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys, time
import bs4, requests, http.client, urllib, urllib.request
import cv2, json, threading
import numpy as np
import xz_rc
from os import path
from urllib.request import urlopen, Request
from PyQt5 import QtCore, uic, QtWidgets, QtGui
from PyQt5.QtCore import QByteArray, qFuzzyCompare, Qt, QTimer
from PyQt5.QtGui import QPalette, QPixmap
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
from PyQt5.QtWidgets import (QAction, QActionGroup, QApplication, QDialog, QMainWindow, QMessageBox)
# # # # # # # # # # # # # # #
from FaceAPI import * # MS Face API
from SpeechAPI import * # MS Speech API
from readMp3 import * # play mp3 file
from dht11_ import * # sensor
from max30100_ import * # sensor
from cfg_var import * # variables that can vary by user environment
# name of variables defined in cfg(msg)_var.py starts with 'cfg(msg)_'
from msg_var import * # KOR / ENG language is not switchable for now
# # # # # # # # # # # # # # #
# loading PyQt ui files
Main_Window="MainWindow2.ui"
Camera_Window="CameraWindow2.ui"
Login_Window="LoginWindow2.ui"
Join_Window="JoinWindow2.ui"
Measure_Window="MeasureWindow2.ui"
Recommend_Window="RecommendWindow3.ui"
Fail_Window="FailWindow2.ui"
Finish_Window="FinishWindow2.ui"
Ui_Main_Window, QtBaseClass =uic.loadUiType(Main_Window)
Ui_Camera_Window, QtBaseClass =uic.loadUiType(Camera_Window)
Ui_Login_Window, QtBaseClass =uic.loadUiType(Login_Window)
Ui_Join_Window, QtBaseClass =uic.loadUiType(Join_Window)
Ui_Measure_Window, QtBaseClass =uic.loadUiType(Measure_Window)
Ui_Recommend_Window, QtBaseClass =uic.loadUiType(Recommend_Window)
Ui_Fail_Window, QtBaseClass =uic.loadUiType(Fail_Window)
Ui_Finish_Window, QtBaseClass =uic.loadUiType(Finish_Window)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
login_flag=0 # 1 - login, 0 - join
nickname='' # JoinWindow
phone='' # JoinWindow
sex='' # JoinWindow
temp=0 # MeasureWindow
weather='' # MeasureWindow
faces=0 # FaceDetectionWidget
heart_rate=0 # sensor
SpO2=0 # sensor
store_temp=0 # sensor
store_humidity=0 # sensor
groupID=sys.argv[1] # MS Face Group ID
UserName = '' # used in Speech API
person_id = ''
faceinfo = {} # faceInfo parsed from Face API
sensor_result = False
recommend_drink = '당근주스'
message_CAMERA1 = msg_CAMERA1_KOR # CameraWindow
message_EMPTY1 = msg_EMPTY1_KOR # JoinWindow
message_EMPTY2 = msg_EMPTY2_KOR # JoinWindow
message_PHONE1 = msg_PHONE1_KOR # JoinWindow & FailWindow
# Get data from sensor
def collect_sensor_data ():
global heart_rate
global SpO2
global store_temp
global store_humidity
heart_rate = 0
store_temp,store_humidity=measure_tem_humi()
#print(store_temp,store_humidity)
heart_rate,SpO2=measure_pulse_O2()
#print(heart_rate,SpO2)
if heart_rate == 0:
return False
else:
return True
# return 1 : correct phone number, return 0 : wrong phone number
def checkPhoneNumber(phone):
if len(phone) == 11:
for i in range(11):
if phone[i] <'0' or phone[i]>'9':
return 0
return 1
elif len(phone)==13:
if phone[3]!='-' or phone[8]!='-':
return 0
for i in range(13):
if (i!=3 and i!=8) and (phone[i] <'0' or phone[i]>'9'):
return 0
return 1
else:
return 0
# Naver Weather Info Crawler
def collect_weather_info():
global temp
global weather
enc_location=urllib.parse.quote(cfg_LOCATION+'+날씨')
url='https://search.naver.com/search.naver?ie=utf8&query='+ enc_location
req=Request(url, headers={'User-Agent':'Mozilla/5.0'})
page=urlopen(req)
html=page.read()
soup=bs4.BeautifulSoup(html,'html.parser')
temp=soup.find('p',class_='info_temperature').find('span',class_='todaytemp').text
weather=soup.find('ul',class_='info_list').find('p',class_='cast_txt').text #ex) 구름조금, 어제보다 1도 높음.
weather=weather.split(',')[0]
class RecordVideo(QtCore.QObject):
image_data = QtCore.pyqtSignal(np.ndarray)
def __init__(self, camera_port=0, parent=None):
super().__init__(parent)
self.camera = cv2.VideoCapture(camera_port)
self.timer = QtCore.QBasicTimer()
def start_recording(self):
self.timer.start(0, self)
def stop_recording(self):
self.timer.stop()
self.camera.release()
self.camera=cv2.VideoCapture(0)
def timerEvent(self, event):
if (event.timerId() != self.timer.timerId()):
return
read, data = self.camera.read()
if read:
self.image_data.emit(data)
class FaceDetectionWidget(QtWidgets.QWidget):
def __init__(self, haar_cascade_filepath, parent=None):
super().__init__(parent)
self.classifier = cv2.CascadeClassifier(haar_cascade_filepath)
self.image = QtGui.QImage()
self._red = (0, 0, 255)
self._width = 2
self._min_size = (30, 30)
def detect_faces(self, image: np.ndarray):
# haarclassifiers work better in black and white
global faces
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray_image = cv2.equalizeHist(gray_image)
faces = self.classifier.detectMultiScale(gray_image,
scaleFactor=1.3,
minNeighbors=4,
flags=cv2.CASCADE_SCALE_IMAGE,
minSize=self._min_size)
return faces
def image_data_slot(self, image_data):
global faces
global UserName
global person_id
global faceinfo
faces = self.detect_faces(image_data)
# draw face rectangle
for (x, y, w, h) in faces:
cv2.rectangle(image_data,
(x, y),
(x+w, y+h),
self._red,
self._width)
self.image = self.get_qimage(image_data)
if self.image.size() != self.size():
self.setFixedSize(self.image.size())
# check if rectangle is bigger than certain size
if type(faces) is not tuple and faces[0][3] >250 :
cv2.imwrite('try.jpg',image_data)
CameraWindow1.record_video.stop_recording()
if login_flag==1: # login
api_result, faceinfo, UserName = face_api(True,nickname,groupID)
if api_result == False: # Face API Fail 2 or 3
# Fail 2 - No Face Detected
# Fail 3 - Nobody Matched
FailWindow1.showFullScreen()
else : # Face API Success 2
MeasureWindow1.showFullScreen()
MeasureWindow1.Measure()
else: # Join
UserName = nickname
api_result, faceinfo, person_id = face_api(False,nickname,groupID)
MeasureWindow1.showFullScreen()
MeasureWindow1.Measure()
CameraWindow1.close()
else:
self.update()
def get_qimage(self, image: np.ndarray):
height, width, colors = image.shape
bytesPerLine = 3 * width
QImage = QtGui.QImage
image = QImage(image.data,
width,
height,
bytesPerLine,
QImage.Format_RGB888)
image = image.rgbSwapped()
return image
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawImage(0, 0, self.image)
self.image = QtGui.QImage()
# Main Window with background image
# Touch input changes MainWindow to LoginWindow
class MainWindow(QtWidgets.QMainWindow, Ui_Main_Window):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
Ui_Main_Window.__init__(self)
self.setupUi(self)
self.pushButton_Main.clicked.connect(self.Main_button_clicked)
def Main_button_clicked(self):
LoginWindow1.showFullScreen()
self.close()
# Login button & Join button
# Login button touched -> CameraWindow
# Join button touched -> JoinWindow
class LoginWindow(QtWidgets.QMainWindow, Ui_Login_Window):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
Ui_Login_Window.__init__(self)
self.setupUi(self)
self.pushButton_login.clicked.connect(self.Login_login_button_clicked)
self.pushButton_join.clicked.connect(self.Login_join_button_clicked)
def Login_login_button_clicked(self):
global login_flag
login_flag=1
CameraWindow1.record_video.start_recording()
CameraWindow1.showFullScreen()
self.close()
def Login_join_button_clicked(self):
JoinWindow1.showFullScreen()
self.close()
# Camera method powered by OpenCV
# When detected face size exceeds certain value, FaceAPI is called
class CameraWindow(QtWidgets.QMainWindow, Ui_Camera_Window):
def __init__(self,haar_cascade_filepath):
super().__init__()
self.setupUI()
def setupUI(self):
self.setGeometry(0, 0, 800, 480)
self.centralwidget=QtWidgets.QWidget(self)
# Defines entire camera screen label
self.camera_main_label=QtWidgets.QLabel(self.centralwidget)
self.camera_main_label.setGeometry(QtCore.QRect(-1, -1, 800, 480))
self.camera_main_label.setStyleSheet("border-image: url(:/newPrefix/image/juicebro.jpg);")
self.camera_main_label.setObjectName("camera_main_label")
# Defines a text label above the screen
self.camera_title_label=QtWidgets.QLabel(self.centralwidget)
self.camera_title_label.setGeometry(QtCore.QRect(50, 40, 680, 71))
font = QtGui.QFont()
font.setFamily(cfg_FONTS)
font.setPointSize(22)
self.camera_title_label.setFont(font)
self.camera_title_label.setAlignment(QtCore.Qt.AlignCenter)
self.camera_title_label.setObjectName("camera_title_label")
# Camera widget
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(50, 90, 700, 400))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.layout = QtWidgets.QHBoxLayout(self.verticalLayoutWidget)
self.layout.setObjectName("layout")
fp = haar_cascade_filepath
self.face_detection_widget = FaceDetectionWidget(fp)
self.record_video = RecordVideo()
image_data_slot = self.face_detection_widget.image_data_slot
self.record_video.image_data.connect(image_data_slot)
self.layout.addWidget(self.face_detection_widget)
self.verticalLayoutWidget.setLayout(self.layout)
self.setCentralWidget(self.centralwidget)
self.retranslateUi(self)
def retranslateUi(self, CameraWindow1):
_translate = QtCore.QCoreApplication.translate
self.setWindowTitle(_translate("CameraWindow1", "MainWindow"))
self.camera_title_label.setText(_translate("CameraWindow1", message_CAMERA1))
class JoinWindow(QtWidgets.QMainWindow, Ui_Join_Window):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
Ui_Join_Window.__init__(self)
self.setupUi(self)
self.pushButton_join_next.clicked.connect(self.Join_next_button_clicked)
def Join_next_button_clicked(self):
global nickname
global phone
global sex
nickname=self.lineEdit_id.text()
phone=self.lineEdit_phone.text()
if nickname=="" or phone=="": # empty
QMessageBox.information(self, "Empty Field", message_EMPTY1)
return
if self.radioButton_man.isChecked():
sex="man"
elif self.radioButton_woman.isChecked():
sex="woman"
else:
QMessageBox.information(self, "Empty Field", message_EMPTY2)
return
if checkPhoneNumber(phone)==0:
QMessageBox.information(self, "Empty Field", message_PHONE1)
return
if len(phone)==13:
list=phone.split('-')
phone=list[0]+list[1]+list[2]
CameraWindow1.record_video.start_recording()
# Initialize text
self.lineEdit_id.clear()
self.lineEdit_phone.clear()
self.group=QtWidgets.QButtonGroup()
self.group.addButton(self.radioButton_man)
self.group.addButton(self.radioButton_woman)
self.group.setExclusive(False)
self.radioButton_man.setChecked(False)
self.radioButton_woman.setChecked(False)
self.group.setExclusive(True)
CameraWindow1.showFullScreen()
self.close()
# Measure Sensor data and change to RecommendWindow
class MeasureWindow(QtWidgets.QMainWindow, Ui_Measure_Window):
def __init__(self):
super().__init__()
QtWidgets.QMainWindow.__init__(self)
Ui_Measure_Window.__init__(self)
self.setupUi(self)
def Measure(self):
global sensor_result
sensor_result = collect_sensor_data()
if sensor_result == True :
sensor_result = False
RecommendWindow1.showFullScreen()
# Create TTS mp3 file that recommends beverage to user
# CAUTION : THIS WINDOW MUST BE IMPROVED AND REFINED
class RecommendWindow(QtWidgets.QMainWindow, Ui_Recommend_Window):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
Ui_Recommend_Window.__init__(self)
self.setupUi(self)
self.pushButton_apple.clicked.connect(self.Apple_button_clicked)
self.pushButton_carrot.clicked.connect(self.Carrot_button_clicked)
def Apple_button_clicked(self):
createMp3(UserName, '사과주스')
FinishWindow1.showFullScreen()
FinishWindow1.Recommend()
self.close()
def Carrot_button_clicked(self):
createMp3(UserName, '당근주스')
FinishWindow1.showFullScreen()
FinishWindow1.Recommend()
self.close()
# If the user tries to login but FaceAPI returns Fail
# Get userdata like phone number to identify who the user is
class FailWindow(QtWidgets.QMainWindow, Ui_Fail_Window):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
Ui_Fail_Window.__init__(self)
self.setupUi(self)
self.pushButton_fail_next.clicked.connect(self.Fail_next_button_clicked)
def Fail_next_button_clicked(self):
global phone
phone=self.lineEdit_fail_phone.text()
if phone=="":
QMessageBox.information(self, "Empty Field", message_EMPTY1)
return
if checkPhoneNumber(phone)==0:
QMessageBox.information(self, "Empty Field", message_PHONE1)
return
if len(phone)==13:
list=phone.split('-')
phone=list[0]+list[1]+list[2]
self.lineEdit_fail_phone.clear()
MeasureWindow1.showFullScreen()
MeasureWindow1.Measure()
self.close()
# play TTS mp3 created in RecommendWindow
# update weather info and go back to MainWindow
class FinishWindow(QtWidgets.QMainWindow, Ui_Finish_Window):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
Ui_Finish_Window.__init__(self)
self.setupUi(self)
def Recommend(self):
readMp3File()
global login_flag
global faces
collect_weather_info()
if login_flag==0: # Join
data={'id':nickname, 'phone':phone,'sex':sex,'weather':weather,'personid':person_id, 'storeid':cfg_LOCATION, 'temp':temp,'faceinfo':json.dumps(faceinfo)}
print(data)
#rep=requests.post(cfg_SERVER_URL,data=data) # REQUIRES SOME IMPROVEMENT
else:
login_flag=0
faces=0
MainWindow1.showFullScreen()
if __name__ == "__main__":
script_dir = path.dirname(path.realpath(__file__))
haar_cascade_filepath = path.join(script_dir,
'..',
'data',
#'C:/Users/new/Desktop/uic/haarcascade_frontalface_default.xml')
cfg_MAIN_DIR + '/haarcascade_frontalface_default.xml')
haar_cascade_filepath = path.abspath(haar_cascade_filepath)
app = QtWidgets.QApplication(sys.argv)
MainWindow1=MainWindow()
LoginWindow1=LoginWindow()
CameraWindow1=CameraWindow(haar_cascade_filepath)
JoinWindow1=JoinWindow()
MeasureWindow1=MeasureWindow()
RecommendWindow1=RecommendWindow()
FailWindow1=FailWindow()
FinishWindow1=FinishWindow()
collect_weather_info()
MeasureWindow1.showFullScreen()
FinishWindow1.showFullScreen()
MainWindow1.showFullScreen()
sys.exit(app.exec_())