-
Notifications
You must be signed in to change notification settings - Fork 4
/
ClassUtils.py
182 lines (142 loc) · 5.79 KB
/
ClassUtils.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
import os, json, time, platform
import MyException, ClassCV
def getBasepath():
basepath = os.path.dirname(os.path.realpath(__file__))
return basepath
def loadConfig():
basepath = getBasepath()
configpath = os.path.join(basepath, 'Config.json')
with open(configpath, 'r', encoding='utf-8') as f:
config = json.load(f)
return config
def protectPersonName(name):
# big5 ╳
if isWindows():
return name[0] + '╳' + name[2:]
else:
return name[0] + '〇' + name[2:]
def protectPersonNameForTTS(name):
return name[0] + '圈' + name[2:]
def getFaceImagepath(faceid):
basepath = os.path.dirname(os.path.realpath(__file__))
#detectedFaceImagepath = basepath + "/tmp/faceId_" + faceid + ".jpg"
detectedFaceImagepath = os.path.join(basepath, 'tmp',
"faceId_" + faceid + ".png")
if not os.path.exists(os.path.dirname(detectedFaceImagepath)):
os.makedirs(os.path.dirname(detectedFaceImagepath))
return detectedFaceImagepath
def getTakePicturePath(personGroupId):
''' 取得拍照後要存檔的路徑。 '''
basepath = getBasepath()
jpgimagepath = os.path.join(
basepath, 'takepictures', personGroupId + "_" +
time.strftime("%Y%m%d_%H%M%S", time.localtime()) + ".jpg")
return jpgimagepath
def makedirsPath(path):
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
def isFaceAPIError(faceapijson):
if 'error' in faceapijson:
if faceapijson['error']['code'] == 'RateLimitExceeded':
raise MyException.RateLimitExceededError(
faceapijson['error']['code'])
elif faceapijson['error']['code'] == 'PersonGroupNotFound':
raise MyException.PersonGroupNotFoundError(
faceapijson['error']['code'])
elif faceapijson['error']['code'] == 'Unspecified':
raise MyException.UnspecifiedError(faceapijson['error']['code'])
elif faceapijson['error']['code'] == 'PersonGroupNotTrained':
raise MyException.PersonGroupNotTrainedError(faceapijson['error']['code'])
else:
print('CODE:', faceapijson['error']['code'])
print('MESSAGE:', faceapijson['error']['message'])
return True
return False
def tryFaceAPIError(faceapijson):
if 'error' in faceapijson:
print('CODE:', faceapijson['error']['code'])
print('MESSAGE:', faceapijson['error']['message'])
text = faceapijson['error']['code'] + ": " + faceapijson['error']['message']
ClassCV.cv_ImageText('存取發生錯誤!', text)
# def SigninSuccess(person, faceid):
# #ClassGTTS.play_gTTS(person['name'], '簽到成功')
# text = person['name'], '簽到成功'
# print(person['name'], '簽到成功')
# print(person)
# print(getFaceImagepath(faceid))
# ClassMessageBox.SuccessGUI('簽到成功', text, getFaceImagepath(faceid))
# def SigninSuccesses(successes):
# if isLinux():
# if len(successes) == 0:
# print('沒有人簽到')
# return
# for success in successes:
# name = protectPersonName(success['person']['name'])
# if isDarwin():
# #import ClassGTTS
# #ClassGTTS.play_gTTS(name, '簽到成功!')
# print(protectPersonName(name), '簽到成功!')
# else:
# print(protectPersonName(name), '簽到成功!')
# elif isWindows() or isDarwin():
# import ClassCamera
# #ClassMessageBox.SuccessesGUI(successes)
# ClassCamera.cv_Success(successes)
def textConfidence(name, confidence):
name = protectPersonName(name)
if confidence >= 0.9:
return name + '簽到成功!!!'
elif confidence >= 0.8:
return name + '簽到成功!!'
elif confidence >= 0.7:
return name + '簽到成功!'
else:
return name + '簽到成功'
def SigninIdentifyfaces(identifyfaces, picture=None):
if isLinux():
if len(identifyfaces) == 0:
print('照片裡沒有人!')
return
for identifyface in identifyfaces:
if 'person' in identifyface:
print("identifyface['confidence']=",identifyface['confidence'])
name = protectPersonName(identifyface['person']['name'])
textConfidence(name, identifyface['confidence'])
else:
print('你哪位?', identifyface)
elif isWindows() or isDarwin():
import ClassCV
ClassCV.cv_Identifyfaces(identifyfaces, picture)
def isLinux():
return 'Linux' == platform.system()
def isDarwin():
return 'Darwin' == platform.system()
def isWindows():
return isWindows7() or isWindows10()
def isWindows7():
return 'Windows' == platform.system() and '7' == platform.release()
def isWindows10():
return 'Windows' == platform.system() and '10' == platform.release()
def getSystemFont():
# macos: /Library/Fonts/Microsoft Sans Serif.ttf
# if ClassUtils.isDarwin():
# #ttf = '/Library/Fonts/Microsoft\\ Sans\\ Serif.ttf'
# #ttf = "/Library/Fonts/AppleMyungjo.ttf"
# #ttf = "/Library/Fonts/AppleGothic.ttf"
# ttf = "/Library/Fonts/Arial Unicode.ttf"
# elif ClassUtils.isWindows():
# ttf = "simhei.ttf"
# #ttf = "arial.ttf"
# else:
# ttf = "simhei.ttf"
if isDarwin():
ttf = "/Library/Fonts/Arial Unicode.ttf"
elif isWindows7():
ttf = "simhei.ttf"
elif isWindows10():
#ttf = "C:/Windows/Fonts/Arial.ttf" # 中文無法出現
ttf = "C:/Windows.old/Windows/Fonts/msjhbd.ttc" # 微軟正黑體
#tts = "C:/Windows.old/Windows/Fonts/kaiu.ttf"
else:
ttf = "C:/Windows.old/Windows/Fonts/msjhbd.ttc" # 微軟正黑體
return ttf