-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_data_processor.py
More file actions
176 lines (145 loc) · 8.27 KB
/
Copy pathclass_data_processor.py
File metadata and controls
176 lines (145 loc) · 8.27 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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 29 16:49:25 2019
@author: wangz29
"""
import json
import math
class DataProcessor(object):
def process(self, input_path, output_file):
# Open the file and read the metadata into the container
with open(input_path, 'r') as json_file:
meta_data = json.load(json_file)
# Iterate through the metadata and compute the Euclidean distance between every two frames
# If either one of the frames does not have the meatadata, an empty list is inserted
processed = {}
pose_keypoints_2d_distance = []
face_keypoints_2d_distance = []
hand_left_keypoints_2d_distance = []
hand_right_keypoints_2d_distance = []
for prev, cur in zip(meta_data, meta_data[1:]):
prev_pose_keypoints_2d = []
prev_face_keypoints_2d = []
prev_hand_left_keypoints_2d = []
prev_hand_right_keypoints_2d = []
if 'pose_keypoints_2d' in prev:
prev_pose_keypoints_2d = prev['pose_keypoints_2d']
if 'face_keypoints_2d' in prev:
prev_face_keypoints_2d = prev['face_keypoints_2d']
if 'hand_left_keypoints_2d' in prev:
prev_hand_left_keypoints_2d = prev['hand_left_keypoints_2d']
if 'hand_right_keypoints_2d' in prev:
prev_hand_right_keypoints_2d = prev['hand_right_keypoints_2d']
cur_pose_keypoints_2d = []
cur_face_keypoints_2d = []
cur_hand_left_keypoints_2d = []
cur_hand_right_keypoints_2d = []
if 'pose_keypoints_2d' in cur:
cur_pose_keypoints_2d = cur['pose_keypoints_2d']
if 'face_keypoints_2d' in cur:
cur_face_keypoints_2d = cur['face_keypoints_2d']
if 'hand_left_keypoints_2d' in cur:
cur_hand_left_keypoints_2d = cur['hand_left_keypoints_2d']
if 'hand_right_keypoints_2d' in cur:
cur_hand_right_keypoints_2d = cur['hand_right_keypoints_2d']
# pose keypoints distance calculation
if len(prev_pose_keypoints_2d) > 0 and len(cur_pose_keypoints_2d) > 0:
all_distance = []
for i in range(len(prev_pose_keypoints_2d) // 3):
x1 = prev_pose_keypoints_2d[int(3 * i)]
y1 = prev_pose_keypoints_2d[int(3 * i + 1)]
c1 = prev_pose_keypoints_2d[int(3 * i + 2)]
x2 = cur_pose_keypoints_2d[int(3 * i)]
y2 = cur_pose_keypoints_2d[int(3 * i + 1)]
c2 = cur_pose_keypoints_2d[int(3 * i + 2)]
#print('x1: ', x1, 'y1: ', y1, 'x2: ', x2, 'y2: ', y2, 'c1: ', c1, 'c2: ', c2)
# compute the Euclidean distance
distance = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
all_distance.append(distance)
all_distance.append(c1)
all_distance.append(c2)
#print(all_distance)
pose_keypoints_2d_distance.append(all_distance)
else:
#print('no data')
pose_keypoints_2d_distance.append([])
# face keypoints distance calculation
if len(prev_face_keypoints_2d) > 0 and len(cur_face_keypoints_2d) > 0:
all_distance = []
for i in range(len(prev_face_keypoints_2d) // 3):
x1 = prev_face_keypoints_2d[int(3 * i)]
y1 = prev_face_keypoints_2d[int(3 * i + 1)]
c1 = prev_face_keypoints_2d[int(3 * i + 2)]
x2 = cur_face_keypoints_2d[int(3 * i)]
y2 = cur_face_keypoints_2d[int(3 * i + 1)]
c2 = cur_face_keypoints_2d[int(3 * i + 2)]
#print('x1: ', x1, 'y1: ', y1, 'x2: ', x2, 'y2: ', y2, 'c1: ', c1, 'c2: ', c2)
# compute the Euclidean distance
distance = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
all_distance.append(distance)
all_distance.append(c1)
all_distance.append(c2)
#print(all_distance)
face_keypoints_2d_distance.append(all_distance)
else:
#print('no data')
face_keypoints_2d_distance.append([])
# left hand keypoints distance calculation
if len(prev_hand_left_keypoints_2d) > 0 and len(cur_hand_left_keypoints_2d) > 0:
all_distance = []
for i in range(len(prev_hand_left_keypoints_2d) // 3):
x1 = prev_hand_left_keypoints_2d[int(3 * i)]
y1 = prev_hand_left_keypoints_2d[int(3 * i + 1)]
c1 = prev_hand_left_keypoints_2d[int(3 * i + 2)]
x2 = cur_hand_left_keypoints_2d[int(3 * i)]
y2 = cur_hand_left_keypoints_2d[int(3 * i + 1)]
c2 = cur_hand_left_keypoints_2d[int(3 * i + 2)]
#print('x1: ', x1, 'y1: ', y1, 'x2: ', x2, 'y2: ', y2, 'c1: ', c1, 'c2: ', c2)
# compute the Euclidean distance
distance = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
all_distance.append(distance)
all_distance.append(c1)
all_distance.append(c2)
#print(all_distance)
hand_left_keypoints_2d_distance.append(all_distance)
else:
#print('no data')
hand_left_keypoints_2d_distance.append([])
# right hand keypoints distance calculation
if len(prev_hand_right_keypoints_2d) > 0 and len(cur_hand_right_keypoints_2d) > 0:
all_distance = []
for i in range(len(prev_hand_right_keypoints_2d) // 3):
x1 = prev_hand_right_keypoints_2d[int(3 * i)]
y1 = prev_hand_right_keypoints_2d[int(3 * i + 1)]
c1 = prev_hand_right_keypoints_2d[int(3 * i + 2)]
x2 = cur_hand_right_keypoints_2d[int(3 * i)]
y2 = cur_hand_right_keypoints_2d[int(3 * i + 1)]
c2 = cur_hand_right_keypoints_2d[int(3 * i + 2)]
#print('x1: ', x1, 'y1: ', y1, 'x2: ', x2, 'y2: ', y2, 'c1: ', c1, 'c2: ', c2)
# compute the Euclidean distance
distance = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
all_distance.append(distance)
all_distance.append(c1)
all_distance.append(c2)
#print(all_distance)
hand_right_keypoints_2d_distance.append(all_distance)
else:
#print('no data')
hand_right_keypoints_2d_distance.append([])
#for i in range(len(pose_keypoints_2d_distance)):
# print(i, ' ', pose_keypoints_2d_distance[i])
#for i in range(len(face_keypoints_2d_distance)):
# print(i, ' ', face_keypoints_2d_distance[i])
#for i in range(len(hand_left_keypoints_2d_distance)):
# print(i, ' ', hand_left_keypoints_2d_distance[i])
#for i in range(len(hand_right_keypoints_2d_distance)):
# print(i, ' ', hand_right_keypoints_2d_distance[i])
processed = {'pose_keypoints_2d_distance' : pose_keypoints_2d_distance, 'face_keypoints_2d_distance' : face_keypoints_2d_distance, 'hand_left_keypoints_2d_distance' : hand_left_keypoints_2d_distance, 'hand_right_keypoints_2d_distance' : hand_right_keypoints_2d_distance}
with open(output_file, 'w') as json_file:
json.dump(processed, json_file)
print('Processing done')
return output_file
# input_path = 'F:\\Data\\Test_2_raw.json'
# output_file = 'F:\\Data\\Test_2_processed.json'
# myProcessor = DataProcessor()
# print(myProcessor.process(input_path, output_file))