-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
422 lines (345 loc) · 17.9 KB
/
data.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import pdb
#pdb.set_trace()
import numpy as np
class alphapose_dataloader():
def __init__(self, data_json):
'''
constructor for the dcpose_dataloader class, a class takes a json file of dcpose detections and creates an indexable object to easily access poses and keypoints.
Parameters: data: json object
json file of open pose detections. If data is None, then initalize self.data as an empty array
Returns: None
'''
self.average_height = None
if data_json is None:
self.data = []
return
#Thorax = neck
keypoint_array = ["Nose", "LEye", "REye", "LEar", "REar", "left_shoulder", "right_shoulder", "LElbow", "RElbow", "LWrist", "RWrist", "left_hip", "right_hip", "left_knee", "right_knee", "left_ankle", "right_ankle", "Head", "Neck", "Hip", "LBigToe", "RBigToe", "LSmallToe", "RSmallToe", "LHeel", "RHeel"]
data_obj = {}
for img in range(0, len(data_json)):
pose = {}
#if len(list(data_obj.keys())) == 100:
# break
'''
{0, "Nose"},
{1, "LEye"},
{2, "REye"},
{3, "LEar"},
{4, "REar"},
{5, "LShoulder"},
{6, "RShoulder"},
{7, "LElbow"},
{8, "RElbow"},
{9, "LWrist"},
{10, "RWrist"},
{11, "LHip"},
{12, "RHip"},
{13, "LKnee"},
{14, "Rknee"},
{15, "LAnkle"},
{16, "RAnkle"},
{17, "Head"},
{18, "Neck"},
{19, "Hip"},
{20, "LBigToe"},
{21, "RBigToe"},
{22, "LSmallToe"},
{23, "RSmallToe"},
{24, "LHeel"},
{25, "RHeel"},
'''
#fig, ax = plt.subplots()
#if int(data_json["Info"][img]["frame"]) > 30:
# break
#if int(data_json["Info"][img]["frame"]) != 154:
# continue
kp_name = 0
for i in range(0, 51, 3):
keypoint_u = (data_json[img]["keypoints"][i])
keypoint_v = (data_json[img]["keypoints"][i + 1])
confidence = (data_json[img]["keypoints"][i + 2])
pose[keypoint_array[kp_name]] = [keypoint_u, keypoint_v, confidence]
pose[keypoint_array[kp_name]] = [keypoint_u, keypoint_v, confidence]
#ax.scatter(keypoint_u, keypoint_v)
#ax.annotate(keypoint_array[i] + ' ' + str(i), (keypoint_u, keypoint_v))
kp_name = kp_name + 1
frame_name = data_json[img]["image_id"]
#print(frame_name, " THIS IS THE FRANE")
if int(frame_name.split(".")[0]) in data_obj:
data_obj[int(frame_name.split(".")[0])].append(pose)
else:
data_obj[int(frame_name.split(".")[0])] = [pose]
self.data = data_obj
def __len__(self):
'''
Returns the length of self.data
Parameters: None
Returns: output
Length of self.data
'''
return len(self.data)
def getitem(self, idx):
'''
Gets each poses keypoint detections
Parameters: idx: int
index of pose
Returns: output: python dictionary
dictionary of keypoints for each pose
'''
key = list(self.data.keys())[idx]
return self.data[key]
def getData(self):
return self.data
def writeData(self, data):
self.data = data
def write_height(self, height):
self.average_height = height
class vitpose_easy_dataloader():
def __init__(self, data_json):
'''
constructor for the dcpose_dataloader class, a class takes a json file of dcpose detections and creates an indexable object to easily access poses and keypoints.
Parameters: data: json object
json file of open pose detections. If data is None, then initalize self.data as an empty array
Returns: None
This is for a single frame
'''
self.average_height = None
if data_json is None:
self.data = []
return
#if not isinstance(data_json, list):
# data_json = [data_json]
#Middle is 'head_bottom'
#keypoint_array = ['nose', 'middle', 'head_top', 'box1', 'box2', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist', 'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle']
keypoint_dict = {"0": "nose", "1": "left_eye", "2": "right_eye", "3": "left_ear", "4": "right_ear", "5": "left_shoulder", "6": "right_shoulder", "7": "left_elbow", "8": "right_elbow", "9": "left_wrist", "10": "right_wrist", "11": "left_hip", "12": "right_hip", "13": "left_knee", "14": "right_knee", "15": "left_ankle", "16": "right_ankle", "17": "left_big_toe", "18": "left_small_toe", "19": "left_heel", "20": "right_big_toe", "21": "right_small_toe", "22": "right_heel", "23": "face-0", "24": "face-1", "25": "face-2", "26": "face-3", "27": "face-4", "28": "face-5", "29": "face-6", "30": "face-7", "31": "face-8", "32": "face-9", "33": "face-10", "34": "face-11", "35": "face-12", "36": "face-13", "37": "face-14", "38": "face-15", "39": "face-16", "40": "face-17", "41": "face-18", "42": "face-19", "43": "face-20", "44": "face-21", "45": "face-22", "46": "face-23", "47": "face-24", "48": "face-25", "49": "face-26", "50": "face-27", "51": "face-28", "52": "face-29", "53": "face-30", "54": "face-31", "55": "face-32", "56": "face-33", "57": "face-34", "58": "face-35", "59": "face-36", "60": "face-37", "61": "face-38", "62": "face-39", "63": "face-40", "64": "face-41", "65": "face-42", "66": "face-43", "67": "face-44", "68": "face-45", "69": "face-46", "70": "face-47", "71": "face-48", "72": "face-49", "73": "face-50", "74": "face-51", "75": "face-52", "76": "face-53", "77": "face-54", "78": "face-55", "79": "face-56", "80": "face-57", "81": "face-58", "82": "face-59", "83": "face-60", "84": "face-61", "85": "face-62", "86": "face-63", "87": "face-64", "88": "face-65", "89": "face-66", "90": "face-67", "91": "left_hand_root", "92": "left_thumb1", "93": "left_thumb2", "94": "left_thumb3", "95": "left_thumb4", "96": "left_forefinger1", "97": "left_forefinger2", "98": "left_forefinger3", "99": "left_forefinger4", "100": "left_middle_finger1", "101": "left_middle_finger2", "102": "left_middle_finger3", "103": "left_middle_finger4", "104": "left_ring_finger1", "105": "left_ring_finger2", "106": "left_ring_finger3", "107": "left_ring_finger4", "108": "left_pinky_finger1", "109": "left_pinky_finger2", "110": "left_pinky_finger3", "111": "left_pinky_finger4", "112": "right_hand_root", "113": "right_thumb1", "114": "right_thumb2", "115": "right_thumb3", "116": "right_thumb4", "117": "right_forefinger1", "118": "right_forefinger2", "119": "right_forefinger3", "120": "right_forefinger4", "121": "right_middle_finger1", "122": "right_middle_finger2", "123": "right_middle_finger3", "124": "right_middle_finger4", "125": "right_ring_finger1", "126": "right_ring_finger2", "127": "right_ring_finger3", "128": "right_ring_finger4", "129": "right_pinky_finger1", "130": "right_pinky_finger2", "131": "right_pinky_finger3", "132": "right_pinky_finger4"}
data_array = []
for frame in range(len(data_json['keypoints'])):
for ppl in data_json["keypoints"][frame].keys():
pose = {}
#frame_name = data_json["keypoints"][0][ppl]
for i in keypoint_dict.keys():
#print(data_json["keypoints"][frame][ppl])
keypoint_u = data_json["keypoints"][frame][ppl][int(i)][0]
keypoint_v = data_json["keypoints"][frame][ppl][int(i)][1]
confidence = data_json["keypoints"][frame][ppl][int(i)][2]
pose[keypoint_dict[i]] = [keypoint_u, keypoint_v, confidence, frame]
data_array.append(pose)
self.data = data_array
def __len__(self):
'''
Returns the length of self.data
Parameters: None
Returns: output
Length of self.data
'''
return len(self.data)
def getitem(self, idx):
'''
Gets each poses keypoint detections
Parameters: idx: int
index of pose
Returns: output: python dictionary
dictionary of keypoints for each pose
'''
return self.data[idx]
def remove(self, idx):
'''
Removes an index from self.data
Parameters: idx: int
index of pose
Returns: output: None
'''
self.data = self.data.pop(idx)
def new_data(self, data):
'''
Replaces self.data with a new array of keypoint dictionaries
Parameters: data: array of dictionaries
Returns: output: None
'''
self.data = data
def get_height(self):
return self.average_height
def write_height(self, height):
self.average_height = height
class dcpose_dataloader():
def __init__(self, data_json):
'''
constructor for the dcpose_dataloader class, a class takes a json file of dcpose detections and creates an indexable object to easily access poses and keypoints.
Parameters: data: json object
json file of open pose detections. If data is None, then initalize self.data as an empty array
Returns: None
'''
self.average_height = None
if data_json is None:
self.data = []
return
if not isinstance(data_json, list):
data_json = [data_json]
#Middle is 'head_bottom'
keypoint_array = ['nose', 'middle', 'head_top', 'box1', 'box2', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist', 'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle']
data_array = []
for data in data_json:
for ppl in range(len(data["Info"])):
pose = {}
frame_name = data["Info"][ppl]["image_path"]
for i in range(0, 17):
keypoint_u = data["Info"][ppl]["keypoints"][i][0]
keypoint_v = data["Info"][ppl]["keypoints"][i][1]
confidence = data["Info"][ppl]["keypoints"][i][2]
if keypoint_array[i] == 'box1' or keypoint_array[i] == 'box2':
continue
pose[keypoint_array[i]] = [keypoint_u, keypoint_v, confidence, frame_name]
data_array.append(pose)
self.data = data_array
def __len__(self):
'''
Returns the length of self.data
Parameters: None
Returns: output
Length of self.data
'''
return len(self.data)
def getitem(self, idx):
'''
Gets each poses keypoint detections
Parameters: idx: int
index of pose
Returns: output: python dictionary
dictionary of keypoints for each pose
'''
return self.data[idx]
def remove(self, idx):
'''
Removes an index from self.data
Parameters: idx: int
index of pose
Returns: output: None
'''
self.data = self.data.pop(idx)
def new_data(self, data):
'''
Replaces self.data with a new array of keypoint dictionaries
Parameters: data: array of dictionaries
Returns: output: None
'''
self.data = data
def get_height(self):
return self.average_height
def write_height(self, height):
self.average_height = height
class coco_mmpose_dataloader():
def __init__(self, data_json, scale_x = 1.0, scale_y = 1.0, bound_lower = 0, bound = None, random = None):
'''
constructor for the dcpose_dataloader class, a class takes a json file of dcpose detections and creates an indexable object to easily access poses and keypoints.
Parameters: data: json object
json file of open pose detections. If data is None, then initalize self.data as an empty array
Returns: None
'''
self.average_height = None
if data_json is None:
self.data = []
return
#Thorax = neck
keypoint_array = ['nose', 'left_eye', 'right_eye', 'left_ear', 'right_ear', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist', 'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle']
data_obj = {}
for img in range(0, len(data_json["Info"])):
pose = {}
#print(img, " THE IMG ")
#print(data_json["Info"][img]["frame"], " image id ")
if bound is not None:
if int(data_json["Info"][img]["frame"]) > bound:
continue
if int(data_json["Info"][img]["frame"]) < bound_lower:
continue
cond_array = []
#if len(list(data_obj.keys())) == 100:
# break
'''
joint_array = [[0,1], #Nose - Right Eye #0
[0,2], #Nose - Left Eye #1
[1,2], #Right Eye - Left Eye #2
[1,3], #Right Eye - Right Ear #3
[2,4], #Left Eye - Left Ear #4
[3,5], #Right Ear - Right Shoulder #5
[4,6], #Left Ear - Left Shoulder #6
[5,6], #Right Shoulder - Left Shoulder #7
[6,8], #Left Shoulder - Left Elbow #8
[5,7], #Right Shoulder - Right Elbow #9
[7,9], #Right Elbow - Right Wrist #10
[8,10], #Left Elbow - Left Wrist #11
[6,12], #Left Shoulder - Left Hip #12
[5,11], #Right Shoulder - Right Hip #13
[11,12],#Right Hip - Left Hip #14
[12,14],#Left Hip - Left Knee #15
[11,13],#Right Hip - Right Knee #16
[14,16],#Left Knee - Left Ankle #17
[13,15]]#Right Knee - Right Ankle #18
'''
#fig, ax = plt.subplots()
#if int(data_json["Info"][img]["frame"]) > 30:
# break
#if int(data_json["Info"][img]["frame"]) != 154:
# continue
for i in range(len(keypoint_array)):
s = 0
s1 = 0
if random is not None and random != 0:
s = np.random.normal(0, random, 1).item()
s1 = np.random.normal(0, random, 1).item()
keypoint_u = scale_x*(data_json["Info"][img]['keypoints'][i][0] + s)
keypoint_v = scale_y*(data_json["Info"][img]['keypoints'][i][1] + s1)
confidence = data_json["Info"][img]['keypoints'][i][2]
pose[keypoint_array[i]] = [keypoint_u, keypoint_v, confidence]
#ax.scatter(keypoint_u, keypoint_v)
#ax.annotate(keypoint_array[i] + ' ' + str(i), (keypoint_u, keypoint_v))
if keypoint_array[i] == 'left_ankle' or keypoint_array[i] == 'right_ankle' or keypoint_array[i] == 'neck':
cond_array.append(confidence)
'''
for i in range(len(joint_array)):
k1 = joint_array[i][0]
k2 = joint_array[i][1]
keypoint_u1 = data_json["Info"][img]['keypoints'][k1][0]
keypoint_v1 = data_json["Info"][img]['keypoints'][k1][1]
keypoint_u2 = data_json["Info"][img]['keypoints'][k2][0]
keypoint_v2 = data_json["Info"][img]['keypoints'][k2][1]
print(k1, k2, "K1 k2 !!")
print(keypoint_u1, keypoint_v1, " HII")
print(keypoint_u2, keypoint_v2, " HIIiii")
ax.plot([keypoint_u1, keypoint_u2], [keypoint_v1, keypoint_v2])
ax.axis('equal')
plt.show()
'''
frame_name = data_json["Info"][img]["frame"]
person_id = data_json["Info"][img]["track_id"]
pose['id'] = int(person_id)
pose["bbox"] = data_json["Info"][img]["bbox"]
#print(frame_name, " THIS IS THE FRANE")
if int(frame_name) in data_obj:
data_obj[int(frame_name)].append(pose)
else:
data_obj[int(frame_name)] = [pose]
self.data = data_obj
def __len__(self):
'''
Returns the length of self.data
Parameters: None
Returns: output
Length of self.data
'''
return len(self.data)
def getitem(self, idx):
'''
Gets each poses keypoint detections
Parameters: idx: int
index of pose
Returns: output: python dictionary
dictionary of keypoints for each pose
'''
key = list(self.data.keys())[idx]
return self.data[key]
def getData(self):
return self.data
def writeData(self, data):
self.data = data
def write_height(self, height):
self.average_height = height