-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.py
360 lines (312 loc) · 12.8 KB
/
gui.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
# -*- coding: utf-8 -*-
"""
@author: Sucre
@email: qian.dong.2018@gmail.com
"""
import warnings
warnings.filterwarnings("ignore")
import os
import cv2
import time
import tkinter
import numpy as np
from tkinter import *
import tensorflow as tf
def fig_reg():
'''
实时识别图片接口
'''
labels = ["上", "下", "左", "右", "张", "合"]
cap = cv2.VideoCapture(0) # 开启本地摄像头
# 设置每一帧的大小
width = 640
height = 480
cap.set(3, width)
cap.set(4, height)
a = list(time.localtime(time.time()))
a = [str(aa) for aa in a]
out_path = "./data/recognition/" + "_".join(a)
if not os.path.exists(out_path):
os.makedirs(out_path)
while True:
ret, frame = cap.read() # 从摄像头读取视频帧
frameCopy = np.copy(frame) # 复制一份用于画图
frameWidth = frame.shape[1]
frameHeight = frame.shape[0]
aspect_ratio = frameWidth / frameHeight
threshold = 0.2
# 处理用于输入网络提取特征点
inHeight = 368
inWidth = int(((aspect_ratio * inHeight) * 8) // 8)
inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight), (0, 0, 0), swapRB=False, crop=False)
net.setInput(inpBlob)
output = net.forward()
# Empty list to store the detected keypoints
points = []
cnt = 0
for i in range(nPoints):
# confidence map of corresponding body's part.
probMap = output[0, i, :, :]
probMap = cv2.resize(probMap, (frameWidth, frameHeight))
# Find global maxima of the probMap.
minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
if prob > threshold:
cv2.circle(frameCopy, (int(point[0]), int(point[1])), 8, (0, 255, 255), thickness=-1,
lineType=cv2.FILLED)
# Add the point to the list if the probability is greater than the threshold
points.append((int(point[0]), int(point[1])))
cnt += 1
else:
points.append((0, 0))
cv2.imshow('Output-Keypoints', frameCopy)
if cnt > 14: # 如果符合条件,就进行识别
# 读取预训练模型
PATH = "./fig_model"
sess = tf.Session(graph=tf.Graph())
tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.TRAINING], PATH)
output = sess.graph.get_tensor_by_name('predict:0')
x = sess.graph.get_tensor_by_name('x:0')
r = sess.run(output, feed_dict={x: np.array(points).reshape(-1, 44)})[0]
sess.close()
# 输出预测结果
print(labels[r])
cv2.imwrite('%s/fig.jpg' % out_path, frameCopy)
print('结果保存在目录:', out_path)
break
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
def gen_feature_simplify(frames):
# 提取视频特征
start = time.time()
threshold = 0.05 # 提取阈值
max_point = 0 # 视频中最多的关键点
max_fcnt = 15 # 视频中最多的关键点对应的帧数
a = list(time.localtime(time.time()))
a = [str(aa) for aa in a]
out_path = "./data/recognition/" + "_".join(a)
if not os.path.exists(out_path):
os.makedirs(out_path)
for i in range(15, 26, 2): # 从第15帧开始提取
f_cnt = i
frame = frames[f_cnt]
frameCopy = np.copy(frame)
frameWidth = frame.shape[1]
frameHeight = frame.shape[0]
aspect_ratio = frameWidth / frameHeight
# input image dimensions for the network
inHeight = 368
inWidth = int(((aspect_ratio * inHeight) * 8) // 8)
inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight), (0, 0, 0), swapRB=False, crop=False)
net.setInput(inpBlob)
output = net.forward()
# Empty list to store the detected keypoints
points = []
cnt = 0
for i in range(nPoints):
# confidence map of corresponding body's part.
probMap = output[0, i, :, :]
probMap = cv2.resize(probMap, (frameWidth, frameHeight))
# Find global maxima of the probMap.
minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
if prob > threshold:
cv2.circle(frameCopy, (int(point[0]), int(point[1])), 8, (0, 255, 255), thickness=-1,
lineType=cv2.FILLED)
# Add the point to the list if the probability is greater than the threshold
points.append((int(point[0]), int(point[1])))
cnt += 1
else:
points.append((0, 0))
feature = []
if cnt >= max_point: # 如果有更好的视频帧,则清空之前的特征
try:
feature = []
except:
pass
max_point = cnt
max_fcnt = f_cnt
feature.append(points)
if max_point >= 8: # 如果视频帧中有大于等于8个关键点则跳出搜索
break
cv2.imwrite('%s/start.jpg' % out_path, frameCopy)
f_cnt = max_fcnt + 10 # 提取最佳帧之后的第十帧作为动作结束帧
frame = frames[f_cnt]
frameCopy = np.copy(frame)
frameWidth = frame.shape[1]
frameHeight = frame.shape[0]
aspect_ratio = frameWidth / frameHeight
# input image dimensions for the network
inHeight = 368
inWidth = int(((aspect_ratio * inHeight) * 8) // 8)
inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight), (0, 0, 0), swapRB=False, crop=False)
net.setInput(inpBlob)
output = net.forward()
# Empty list to store the detected keypoints
points = []
cnt = 0
for i in range(nPoints):
# confidence map of corresponding body's part.
probMap = output[0, i, :, :]
probMap = cv2.resize(probMap, (frameWidth, frameHeight))
# Find global maxima of the probMap.
minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
if prob > threshold:
cv2.circle(frameCopy, (int(point[0]), int(point[1])), 8, (0, 255, 255), thickness=-1,
lineType=cv2.FILLED)
# Add the point to the list if the probability is greater than the threshold
points.append((int(point[0]), int(point[1])))
cnt += 1
else:
points.append((0, 0))
feature.append(points)
cv2.imwrite('%s/end.jpg' % out_path, frameCopy)
print(time.time() - start) # 输出提取特征时间
print('结果保存在目录:', out_path)
return np.array(feature).reshape(-1, 88) # 返回特征
def video_reg_v2():
# 识别定长视频
cap = cv2.VideoCapture(0)
width = 640
height = 480
cap.set(3, width)
cap.set(4, height)
frames = []
cnt = 0
while True:
ret, frame = cap.read()
frames.append(frame) # 将视频帧保存到列表frames
cv2.imshow('Output', frame)
cnt += 1
if cv2.waitKey(25) & 0xFF == ord('q') or cnt == 120:
break
cap.release()
cv2.destroyAllWindows()
ft = gen_feature_simplify(frames) # 提取视频的特征
# 识别视频的动作
PATH = "./model"
sess = tf.Session(graph=tf.Graph())
tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.TRAINING], PATH)
output = sess.graph.get_tensor_by_name('predict:0')
x = sess.graph.get_tensor_by_name('x:0')
r = sess.run(output, feed_dict={x: ft.reshape(-1, 88)})[0]
sess.close()
labels = ["上", "下", "左", "右", "张", "合"]
print(labels[r]) # 输出预测结果
def video_reg():
# 识别实时视频
cap = cv2.VideoCapture(0)
width = 640
height = 480
cap.set(3, width)
cap.set(4, height)
fps = cap.get(cv2.CAP_PROP_FPS)
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
cnt = 0
a = list(time.localtime(time.time()))
a = [str(aa) for aa in a]
out_path = "./data/recognition/" + "_".join(a)
if not os.path.exists(out_path):
os.makedirs(out_path)
labels = ["上", "下", "左", "右", "张", "合"]
while True:
ret, frame = cap.read()
frameCopy = np.copy(frame)
frameWidth = frame.shape[1]
frameHeight = frame.shape[0]
aspect_ratio = frameWidth / frameHeight
threshold = 0.2
# input image dimensions for the network
inHeight = 368
inWidth = int(((aspect_ratio * inHeight) * 8) // 8)
inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight), (0, 0, 0), swapRB=False, crop=False)
net.setInput(inpBlob)
output = net.forward()
# Empty list to store the detected keypoints
points = []
cnt = 0
for i in range(nPoints):
# confidence map of corresponding body's part.
probMap = output[0, i, :, :]
probMap = cv2.resize(probMap, (frameWidth, frameHeight))
# Find global maxima of the probMap.
minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
if prob > threshold:
cv2.circle(frameCopy, (int(point[0]), int(point[1])), 8, (0, 255, 255), thickness=-1,
lineType=cv2.FILLED)
# Add the point to the list if the probability is greater than the threshold
points.append((int(point[0]), int(point[1])))
cnt += 1
else:
points.append((0, 0))
cv2.imshow('Output-Keypoints', frameCopy)
ft = []
ft.append(points)
if cnt > 14:
# 如果满足条件,则开始提取动作结束帧的特征
cv2.imwrite('%s/start.jpg' % out_path, frameCopy)
ret, frame = cap.read()
frameCopy = np.copy(frame)
frameWidth = frame.shape[1]
frameHeight = frame.shape[0]
aspect_ratio = frameWidth / frameHeight
threshold = 0.1
# input image dimensions for the network
inHeight = 368
inWidth = int(((aspect_ratio * inHeight) * 8) // 8)
inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight), (0, 0, 0), swapRB=False, crop=False)
net.setInput(inpBlob)
output = net.forward()
# Empty list to store the detected keypoints
points = []
cnt = 0
for i in range(nPoints):
# confidence map of corresponding body's part.
probMap = output[0, i, :, :]
probMap = cv2.resize(probMap, (frameWidth, frameHeight))
# Find global maxima of the probMap.
minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
if prob > threshold:
cv2.circle(frameCopy, (int(point[0]), int(point[1])), 8, (0, 255, 255), thickness=-1,
lineType=cv2.FILLED)
# Add the point to the list if the probability is greater than the threshold
points.append((int(point[0]), int(point[1])))
cnt += 1
else:
points.append((0, 0))
cv2.imwrite('%s/end.jpg' % out_path, frameCopy)
ft.append(points)
ft = np.array(ft)
# 读取预训练模型进行预测
PATH = "./model"
sess = tf.Session(graph=tf.Graph())
tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.TRAINING], PATH) # PATH还是路径
output = sess.graph.get_tensor_by_name('predict:0')
x = sess.graph.get_tensor_by_name('x:0')
r = sess.run(output, feed_dict={x: ft.reshape(-1, 88)})[0]
sess.close()
print(labels[r]) # 打印预测结果
print('结果保存在目录:', out_path)
break
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
protoFile = "hand/pose_deploy.prototxt"
weightsFile = "hand/pose_iter_102000.caffemodel"
nPoints = 22
POSE_PAIRS = [[0, 1], [1, 2], [2, 3], [3, 4], [0, 5], [5, 6], [6, 7], [7, 8],
[0, 9], [9, 10], [10, 11], [11, 12],
[0, 13], [13, 14], [14, 15], [15, 16], [0, 17], [17, 18], [18, 19], [19, 20]]
net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)
root = tkinter.Tk(className="识别程序")
root.geometry('800x500+800+500')
fig_reg = Button(root, text="识别图片", command=fig_reg)
fig_reg.pack()
video_reg = Button(root, text="识别实时视频", command=video_reg)
video_reg.pack()
video_reg_2 = Button(root, text="识别定长视频", command=video_reg_v2)
video_reg_2.pack()
root.mainloop()