-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandGestureMediaPipe.py
243 lines (218 loc) · 10.2 KB
/
HandGestureMediaPipe.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
import cv2
import mediapipe as mp
import asyncio
import numpy as np
import time
import timeit
import pandas as pd
now = lambda: time.time()
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
# Read video
video_file='C:/Users/liangch/Desktop/V_20211210_113019_OC0.mp4'
video_file='C:/Users/liangch/Desktop/V_20211224_233134_OC0.mp4'
video_file='C:/Users/liangch/Desktop/V_20211225_094631_OC0.mp4'
video_file='C:/Users/liangch/Desktop/hand_gesture/1far_near_rgb.avi'
video_file='C:/Users/liangch/Desktop/hand_gesture/3left_right_rgb.avi'
video_file='C:/Users/liangch/Desktop/MotionStyleHandData/runJogging_rgb.avi'
video_file='C:/Users/chliang/Desktop/realsense_python/kickSidekick_rgb.avi'
# video_file='C:/Users/chliang/Desktop/realsense_python/kickFrontkick_rgb.avi'
# video_file='C:/Users/chliang/Desktop/realsense_python/walkCrossover_rgb.avi'
# video_file='C:/Users/chliang/Desktop/realsense_python/walkInjured_rgb.avi'
# video_file='C:/Users/chliang/Desktop/realsense_python/runJogging_rgb.avi'
# video_file='C:/Users/chliang/Desktop/realsense_python/runSprint_rgb.avi'
# video_file='C:/Users/chliang/Desktop/realsense_python/runStride_rgb.avi'
# video_file = 'C:/Users/john8/Downloads/newRecord_2022_9_12/frontKickNew_rgb.avi'
# video_file = 'C:/Users/john8/Downloads/newRecord_2022_9_12/sideKickNew_rgb.avi'
# video_file = 'C:/Users/john8/Downloads/newRecord_2022_9_12/walkNormal_rgb.avi'
# video_file = 'C:/Users/john8/Downloads/newRecord_2022_9_12/walkIInjured_rgb.avi'
# video_file = 'C:/Users/john8/Downloads/newRecord_2022_9_14/jumpHurdle_rgb.avi'
video_file = 'C:/Users/john8/Downloads/newRecord_2022_9_14/jumpJoy_rgb.avi'
video_file = 'C:/Users/liangch/Desktop/MotionStyleHandData/newRecord_2023_1_16/twoLegJump_rgb.avi'
video_file = 'C:/Users/liangch/Desktop/MotionStyleHandData/newRecord_2022_9_12/frontKickNew_rgb.avi'
video_file = 'C:/Users/liangch/Desktop/MotionStyleHandData/kickSidekick_rgb.avi'
video_file = 'C:/Users/liangch/Desktop/MotionStyleHandData/runSprint_rgb.avi'
video_file = 'C:/Users/liangch/Desktop/MotionStyleHandData/newRecord_2022_9_14/jumpJoy_rgb.avi'
video_file = 'C:/Users/liangch/Desktop/MotionStyleHandData/newRecord_2023_1_24/runSprint_leftToRight_rgb2.avi'
# video_file=1
tmp_counter = 0
tmp_land_mark = None
tmp_image = None
# async def print123func():
# tmp_num=0
# while tmp_num<=5:
# print(tmp_num)
# tmp_num+=1
# await asyncio.sleep(1)
# if __name__ == "__main__":
# start = now()
# tasks = [print123func() for i in range(5)]
# asyncio.run(asyncio.wait(tasks))
# print('TIME: ', now() - start)
class DetectHandLM():
def __init__(self) -> None:
self.curDetectLM = None
self.isCapturingLM = False
self.videoFile = None
def captureByMediaPipe(videoFile, testingStageFunc, forOutputLM, downFPS = False):
cap = cv2.VideoCapture(videoFile)
tmpTime = time.time()
with mp_hands.Hands(
model_complexity=1,
min_detection_confidence=0.5,
min_tracking_confidence=0.5,
max_num_hands=1) as hands:
while cap.isOpened():
if downFPS:
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
break
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
break
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(image)
# Draw the hand annotations on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# landmarks in a frame
_landMarks = []
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
image,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
if True:
# _landMarks.extend(hand_landmarks.landmark)
# print(_landMarks[0])
handLMPred = [{'x': data_point.x, 'y': data_point.y, 'z': data_point.z} for data_point in hand_landmarks.landmark]
print(handLMPred)
result = testingStageFunc(handLMPred)
result = [{
"time": 0,
"data": [{"x": dataArr[0, 0], "y": dataArr[0, 1], "z": dataArr[0, 2]} for dataArr in result]
}]
result = str(result)
result = result.replace('\'', '\"')
# 第0個位置擺估計的full body pose
forOutputLM[0] = result
# 第1個位置擺estimated hand pose
forOutputLM[1] = str([{
"time": 0,
"data": handLMPred
}]).replace('\'', '\"')
print(result)
# curTime = time.time()
# print('timeCost: ', curTime-tmpTime)
# tmpTime = curTime
print('-------')
# tmp_land_mark=hand_landmarks
# tmp_image=image
# Flip the image horizontally for a selfie-view display.
cv2.imshow('MediaPipe Hands', cv2.flip(image, 1))
if cv2.waitKey(5) & 0xFF == 27:
break
# print(len(_landMarks))
# print('-------')
cap.release()
return 'EndCapture'
# Save to file, and serialize to a json file
if __name__ == '__main__':
cap = cv2.VideoCapture(video_file)
# cap = cv2.VideoCapture(1) # webcam
detectLMs = []
timeLaps = []
with mp_hands.Hands(
model_complexity=1,
min_detection_confidence=0.5,
min_tracking_confidence=0.5,
max_num_hands=1) as hands:
while cap.isOpened():
## New: Read twice in a row is just due to the camera frame rate is 60,
## but the processing is just 30.
## Thus we try to decrease the frame rate by decrease the video frame rate to 30.
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
break
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
break
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(image)
# Draw the hand annotations on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# landmarks in a frame
_landMarks = []
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
image,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
if True:
detectLMs.append(
{
'time': time.time(),
'data': hand_landmarks.landmark
}
)
# print([(data_point.x, data_point.y, data_point.z) for data_point in hand_landmarks.landmark])
# print('-------')
# tmp_land_mark=hand_landmarks
# Flip the image horizontally for a selfie-view display.
cv2.imshow('MediaPipe Hands', cv2.flip(image, 1))
if cv2.waitKey(5) & 0xFF == 27:
break
print('-------')
print(image.shape)
# Identify computation time cost
timeLaps.append(timeit.default_timer())
# print(len(detectLMs))
# print(len(detectLMs[0]['data']))
# Serialize the hand landmarks in MediaPipe format. Serialize: [{'time': 0, 'data': [[1, 2, 3], ...]}, ...]
for i in range(len(detectLMs)):
detectLMs[i]['data'] = [{'x': j.x, 'y': j.y, 'z': j.z} for j in detectLMs[i]['data']]
import json
# with open('./complexModel/walkInjured.json', 'w') as WFile:
with open('./complexModel/newRecord/runSprint_leftToRight_rgb2.json', 'w') as WFile:
json.dump(detectLMs, WFile)
# print(json.dumps(detectLMs))
cap.release()
# Store computation time cost
# timeCostFilePath = 'timeConsume/jumpJoy/mediapipe.csv'
# timeLaps = np.array(timeLaps)
# computeTimeCost = timeLaps[1:] - timeLaps[:-1]
# timeCostDf = pd.DataFrame({
# 'mediapipe': computeTimeCost
# })
# timeCostDf.to_csv(timeCostFilePath, index=False)
# print('Mediapipe compute avg time: ', np.mean(computeTimeCost))
# print('Mediapipe compute time std: ', np.std(computeTimeCost))
# print('Mediapipe compute max time cost: ', np.max(computeTimeCost))
# print('Mediapipe compute min time cost: ', np.min(computeTimeCost))
# Save image with hand landmarks
#cv2.imwrite('image_w_lm.jpg', tmp_image)
# Plot hand landmarks alone
# mp_drawing.plot_landmarks(
# tmp_land_mark, mp_hands.HAND_CONNECTIONS, azimuth=5)