-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCameraDemo.py
174 lines (147 loc) · 6.35 KB
/
CameraDemo.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
# coding:utf-8
from FaceAlignment import FaceAlignment
import numpy as np
import cv2
import utils
import globalvariable as gvar
import socket # 导入 socket 模块
# Change this to True if you want to use the DAN-Menpo-tracking.npz model,
# which is able to detect when face tracking is lost.
useTrackingModel = False
port = 12344
if useTrackingModel:
model = FaceAlignment(112, 112, 1, 1, True)
model.loadNetwork("model/DAN-Menpo-tracking.npz")
else:
model = FaceAlignment(112, 112, 1, 2)
model.loadNetwork("model/DAN-Menpo.npz")
vidIn = cv2.VideoCapture(0)
cascade = cv2.CascadeClassifier("data/haarcascade_frontalface_alt.xml")
neutral_face_flag = True
neutral_landmarks_std = []
reset = True
landmarks = None
print ("Press space to detect the face, press escape to exit")
while True:
vis = vidIn.read()[1]
if len(vis.shape) > 2:
img = np.mean(vis, axis=2).astype(np.uint8)
else:
img = vis.astype(np.uint8)
if reset:
rects = cascade.detectMultiScale(img, scaleFactor=1.2, minNeighbors=3, minSize=(50, 50))
if len(rects) > 0:
minX = rects[0][0]
maxX = rects[0][0] + rects[0][2]
minY = rects[0][1]
maxY = rects[0][1] + rects[0][3]
cv2.rectangle(vis, (minX, minY), (maxX, maxY), (255, 0, 0))
initLandmarks = utils.bestFitRect(None, model.initLandmarks, [minX, minY, maxX, maxY])
reset = False
if model.confidenceLayer:
landmarks, confidence = model.processImg(img[np.newaxis], initLandmarks)
if confidence < 0.1:
reset = True
else:
landmarks = model.processImg(img[np.newaxis], initLandmarks)
landmarks = landmarks.astype(np.int32)
for i in range(landmarks.shape[0]):
cv2.circle(vis, (landmarks[i, 0], landmarks[i, 1]), 2, (0, 255, 0))
# 以鼻子为原点,计算其余所有点的坐标
landmarks = landmarks.astype(np.float)
landmarks_std = landmarks - landmarks[30] # 所有坐标以鼻尖点为中心
if neutral_face_flag:
neutral_landmarks_std = landmarks_std
neutral_face_flag = False
else:
landmarks_std = landmarks_std - neutral_landmarks_std
# pos_nosetip = landmarks[30]
# pos_lefteye = (landmarks[36] + landmarks[39]) / 2
# pos_righteye = (landmarks[42] + landmarks[45]) / 2
bottom = max(landmarks_std[:, 1])
top = min(landmarks_std[:, 1])
left = min(landmarks_std[:, 0])
right = max(landmarks_std[:, 0])
landmarks_std[:, 0] = landmarks_std[:, 0] / (right - left)
landmarks_std[:, 1] = landmarks_std[:, 1] / (bottom - top)
g_fAU4 = 0.3 * (landmarks_std[17, 1] + 0.35) + \
0.3 * (landmarks_std[18, 1] + 0.4) + \
0.3 * (landmarks_std[19, 1] + 0.4) + \
0.3 * (landmarks_std[20, 1] + 0.4) + \
0.3 * (landmarks_std[21, 1] + 0.38)
if g_fAU4 < 0:
g_fAU4 = 0
if g_fAU4 > 1:
g_fAU4 = 1
gvar.set_g_fAU4(g_fAU4)
print(g_fAU4)
# create the string
info = "g_fAU4:" + str(g_fAU4)
# send the flag to client
s = socket.socket() # 创建 socket 对象
host = socket.gethostname() # 获取本地主机名
s.connect((host, port))
s.send(info)
s.close()
else:
initLandmarks = utils.bestFitRect(landmarks, model.initLandmarks)
if model.confidenceLayer:
landmarks, confidence = model.processImg(img[np.newaxis], initLandmarks)
if confidence < 0.1:
reset = True
else:
landmarks = model.processImg(img[np.newaxis], initLandmarks)
landmarks = np.round(landmarks).astype(np.int32)
for i in range(landmarks.shape[0]):
cv2.circle(vis, (landmarks[i, 0], landmarks[i, 1]), 2, (0, 255, 0))
# # # # # # #
pos_nosetip = landmarks[30]
pos_lefteye = (landmarks[36] + landmarks[39]) / 2
pos_righteye = (landmarks[42] + landmarks[45]) / 2
# 以鼻子为原点,计算其余所有点的坐标
landmarks = landmarks.astype(np.float)
landmarks_std = landmarks - landmarks[30] # 所有坐标以鼻尖点为中心
# landmarks_std[:, 1] = -landmarks_std[:, 1] # 转换为常用的标准四象限坐标
if neutral_face_flag:
neutral_landmarks_std = landmarks_std
neutral_face_flag = False
else:
# pos_nosetip = landmarks[30]
# pos_lefteye = (landmarks[36] + landmarks[39]) / 2
# pos_righteye = (landmarks[42] + landmarks[45]) / 2
bottom = max(landmarks_std[:, 1])
top = min(landmarks_std[:, 1])
left = min(landmarks_std[:, 0])
right = max(landmarks_std[:, 0])
landmarks_std[:, 0] = landmarks_std[:, 0] / (right - left)
landmarks_std[:, 1] = landmarks_std[:, 1] / (bottom - top)
# # 检验眉毛
# landmarks_std[[i for i in range(17, 22)]] # left
# landmarks_std[[i for i in range(22, 27)]] # right
#
g_fAU4 = 0.3 * (landmarks_std[17, 1] + 0.35) + \
0.3 * (landmarks_std[18, 1] + 0.4) + \
0.3 * (landmarks_std[19, 1] + 0.4) + \
0.3 * (landmarks_std[20, 1] + 0.4) + \
0.3 * (landmarks_std[21, 1] + 0.38)
if g_fAU4 < 0:
g_fAU4 = 0
if g_fAU4 > 1:
g_fAU4 = 1
gvar.set_g_fAU4(g_fAU4)
print(g_fAU4)
# create the string
info = "g_fAU4:" + str(g_fAU4)
# send the flag to client
s = socket.socket() # 创建 socket 对象
host = socket.gethostname() # 获取本地主机名
s.connect((host, port))
s.send(info)
s.close()
cv2.imshow("image", vis)
key = cv2.waitKey(1)
if key == 27:
break
if key == 32:
reset = True
s.close()