-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollect_data_iams_csv.py
More file actions
235 lines (206 loc) · 11.9 KB
/
collect_data_iams_csv.py
File metadata and controls
235 lines (206 loc) · 11.9 KB
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
import cv2
import numpy as np
import mediapipe as mp
import os
from pathlib import Path
from numpy import savetxt
mp_hands = mp.solutions.hands # Holistic model
mp_drawing = mp.solutions.drawing_utils
mp_holistic = mp.solutions.holistic # Drawing utilities
current_working_directory = os.path.dirname(__file__)
ROOT_FOLDER = os.path.join(current_working_directory, 'data')
relative_data_path = 'data/csv_iamsMediapipe.iams'
absolute_iams_path = Path(os.path.join(current_working_directory, relative_data_path))
print(absolute_iams_path)
f = open(absolute_iams_path)
import json
def Detection_pipeline(image, holistic):
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # COLOR CONVERSION BGR 2 RGB
image.flags.writeable = False # Image is no longer writeable
coords = holistic.process(image) # Make prediction
image.flags.writeable = True # Image is now writeable
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # COLOR COVERSION RGB 2 BGR
return image, coords
class human_pose_data:
def __init__(self):
# self.states, self.pathdir, self.len_seq, self.len_fold = make_sequence_folder()
self.solutions = mp.solutions.holistic
self.draw = mp.solutions.drawing_utils
self.holistic = self.solutions.Holistic(min_detection_confidence=0.3, min_tracking_confidence=0.3)
self.hand_connect = self.solutions.HAND_CONNECTIONS
self.pose_connect = self.solutions.POSE_CONNECTIONS
self.face_connect = self.solutions.FACEMESH_TESSELATION
self.f_content = json.load(f)
self.ROOT_FOLDER = os.path.join(ROOT_FOLDER, self.f_content['Data_Directory'])
# self.hands = mp_hands
def draw_landmarks(self, image, coords):
self.draw.draw_landmarks(image, coords.left_hand_landmarks, self.hand_connect) # Draw left hand connections
self.draw.draw_landmarks(image, coords.right_hand_landmarks, self.hand_connect) # Draw right hand
self.draw.draw_landmarks(image, coords.face_landmarks, self.face_connect)
self.draw.draw_landmarks(image, coords.pose_landmarks, self.pose_connect)
def draw_only_hands(self):
camera = cv2.VideoCapture(0)
while True:
with mp_hands.Hands(max_num_hands=2, min_detection_confidence=0.5, min_tracking_confidence=0.5) as hands:
_, image = camera.read()
image = cv2.flip(image, 1)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
coords = hands.process(image)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if coords.multi_hand_landmarks:
for idx, hand_landmark in enumerate(coords.multi_hand_landmarks):
self.draw.draw_landmarks(image, hand_landmark, mp_hands.HAND_CONNECTIONS,
self.draw.DrawingSpec(color=(255, 0, 200), thickness=1,
circle_radius=3),
self.draw.DrawingSpec(color=(150, 120, 255), thickness=1,
circle_radius=2), )
for _, classification in enumerate(coords.multi_handedness):
if classification.classification[0].index == idx and classification.classification[
0].index == 1:
hand_label = classification.classification[0].label
coord = tuple(
np.multiply(np.array((hand_landmark.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP].x,
hand_landmark.landmark[
mp_hands.HandLandmark.MIDDLE_FINGER_TIP].y)),
[640, 480]).astype(int))
text = '{} {}'.format(hand_label, coord)
cv2.putText(image, text, coord, cv2.FONT_HERSHEY_TRIPLEX, 1, (255, 100, 200), 2,
cv2.LINE_AA)
elif classification.classification[0].index == idx:
hand_label = classification.classification[0].label
coord = tuple(
np.multiply(np.array((hand_landmark.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP].x,
hand_landmark.landmark[
mp_hands.HandLandmark.MIDDLE_FINGER_TIP].y)),
[640, 480]).astype(int))
text = '{} {}'.format(hand_label, coord)
cv2.putText(image, text, coord, cv2.FONT_HERSHEY_TRIPLEX, 1, (255, 100, 200), 2,
cv2.LINE_AA)
else:
# classification.classification[0].index == 1:
hand_label = classification.classification[0].label
coord = tuple(
np.multiply(np.array((hand_landmark.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP].x,
hand_landmark.landmark[
mp_hands.HandLandmark.MIDDLE_FINGER_TIP].y)),
[640, 480]).astype(int))
text = '{} {}'.format(hand_label, coord)
cv2.putText(image, text, coord, cv2.FONT_HERSHEY_TRIPLEX, 1, (255, 100, 200), 2,
cv2.LINE_AA)
cv2.imshow('Output Hand-Tracking', image)
if cv2.waitKey(10) != ord('q'):
continue
camera.release()
cv2.destroyAllWindows()
def _only_face(self):
camera = cv2.VideoCapture(0)
with self.holistic as holistic:
while True:
ret, image = camera.read()
image = cv2.flip(image, 1)
frame, coords = Detection_pipeline(image, holistic)
self.draw.draw_landmarks(image, coords.face_landmarks, self.face_connect)
cv2.imshow('Output_Feed Facial', frame)
if cv2.waitKey(10) != ord('q'):
continue
camera.release()
cv2.destroyAllWindows()
def _only_pose(self):
camera = cv2.VideoCapture(0)
with self.holistic as holistic:
while True:
ret, image = camera.read()
image = cv2.flip(image, 1)
frame, coords = Detection_pipeline(image, holistic)
self.draw.draw_landmarks(image, coords.pose_landmarks, self.pose_connect)
cv2.imshow('Output_Feed Facial', frame)
if cv2.waitKey(10) != ord('q'):
continue
camera.release()
cv2.destroyAllWindows()
@staticmethod
def extract_full_body(coords):
if coords:
"""Function to extract coordinates of keypoints on both hands"""
right_hand = np.array([[landmarked.x, landmarked.y, landmarked.z] for landmarked in
coords.right_hand_landmarks.landmark]).flatten() if coords.right_hand_landmarks else np.zeros(
21 * 3)
left_hand = np.array([[landmarked.x, landmarked.y, landmarked.z] for landmarked in
coords.left_hand_landmarks.landmark]).flatten() if coords.left_hand_landmarks else np.zeros(
21 * 3)
pose = np.array([[landmarked.x, landmarked.y, landmarked.z] for landmarked in
coords.pose_landmarks.landmark]).flatten() if coords.pose_landmarks else np.zeros(132)
facial = np.array([[landmarked.x, landmarked.y, landmarked.z] for landmarked in
coords.face_landmarks.landmark]).flatten() if coords.face_landmarks else np.zeros(1)
return np.concatenate([right_hand, left_hand, pose, facial])
@staticmethod
def extract_only_hands(coords):
"""this function extracts only coordinates of the hands"""
right_hand = np.array([[landmarked.x, landmarked.y, landmarked.z] for landmarked in
coords.right_hand_landmarks.landmark]).flatten() if coords.right_hand_landmarks else np.zeros(
21 * 3)
left_hand = np.array([[landmarked.x, landmarked.y, landmarked.z] for landmarked in
coords.left_hand_landmarks.landmark]).flatten() if coords.left_hand_landmarks else np.zeros(
21 * 3)
return np.concatenate([left_hand, right_hand])
@staticmethod
def extract_only_facial(coords):
return np.array([[landmarked.x, landmarked.y, landmarked.z] for landmarked in
coords.face_landmarks]).flatten() if coords.face_landmarks else np.zeros(1)
@staticmethod
def extract_only_pose(coords):
return np.array([[landmarked.x, landmarked.y, landmarked.z] for landmarked in
coords.pose_landmarks]).flatten() if coords.pose_landmarks else np.zeros(132)
def _whole_body(self):
camera = cv2.VideoCapture(0)
with self.holistic as holistic:
while True:
ret, image = camera.read()
image = cv2.flip(image, 1)
frame, coords = Detection_pipeline(image, holistic)
self.draw_landmarks(frame, coords)
cv2.imshow('Output_Feed Whole Body', frame)
if cv2.waitKey(10) != ord('q'):
continue
camera.release()
cv2.destroyAllWindows()
#
def collect_data_WHOLE(self):
camera = cv2.VideoCapture(0)
"""
Get current working directory
Get the list of actions
get the number of subfolders
iterate through them"""
cur_scr_dir = os.path.dirname(__file__) # This is the absolute directory the script is in
relative_data_path = "data/csv_iamsMediapipe.iams"
abs_iams_path = Path(os.path.join(cur_scr_dir, relative_data_path))
f = open(abs_iams_path)
f_content = json.load(f)
with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
for action in f_content['actions']:
# for folder in range(f_content['subfolder_length']):
# for img_ in range(f_content['video_length']):
_, image = camera.read()
image = cv2.flip(image, 1)
image, coords = Detection_pipeline(image, holistic)
self.draw_landmarks(image, coords)
if action == f_content['actions'][0]:
cv2.putText(image, 'Starting Data Collection into {}'.format(f_content['Data_Directory']),
(20, 20), cv2.FONT_HERSHEY_DUPLEX, 1, (120, 100, 150), 1, cv2.LINE_AA)
cv2.putText(image, 'Now Collecting Data for {}'.format(action),
(15, 12), cv2.FONT_HERSHEY_DUPLEX, 1, (120, 100, 150), 1, cv2.LINE_AA)
cv2.imshow('Data Collection Feed', image)
cv2.waitKey(1000)
else:
cv2.putText(image, 'Now Collecting Data for {}'.format(action),
(15, 12), cv2.FONT_HERSHEY_DUPLEX, 1, (120, 100, 150), 1, cv2.LINE_AA)
cv2.imshow('Data Collection Feed', image)
keypoints = self.extract_full_body(coords)
keypoint_path = Path(os.path.join(self.ROOT_FOLDER, action))
savetxt(keypoint_path, keypoints)
if cv2.waitKey(10) != ord('q'):
continue
camera.release()
cv2.destroyAllWindows()