-
Notifications
You must be signed in to change notification settings - Fork 0
/
modeling.py
343 lines (296 loc) · 12.9 KB
/
modeling.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
# plot 3D
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import os
import sys
import numpy as np
import json
import cv2
# Get the directory of the currently running script
script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
# Target 3D location, 1-4 from left to right
t4 = [-1.05, 0, -1.02]
t3 = [-.39, 0, -.72]
t1 = [1.05, 0, -1.02]
t2 = [.39, 0, -.72]
targets = [t1, t2, t3, t4]
old_targets = [[1.05, 0, -0.56],
[.39, 0, -.24],
[-.39, 0, -.24],
[-1.05, 0, -0.56]]
old_target_img = ["1_left_n_1", "1_left_y_1","2_left_n_1", "2_left_y_1",
"3_left_n_1", "3_left_y_1", "4_left_n_1", "4_left_y_1",
"4_right_n_1"]
# open and import data from json
ld = pd.read_json(script_dir+"/landmark_data.json")
vaid = pd.read_json(script_dir+"/vector_and_intersection_data.json")
# pose connection pair
pose_connection = [(0, 1), (1, 2), (2, 3), (3, 7), (0, 4), (4, 5),
(5, 6), (6, 8), (9, 10), (11, 12), (11, 13),
(13, 15), (15, 17), (15, 19), (15, 21), (17, 19),
(12, 14), (14, 16), (16, 18), (16, 20), (16, 22),
(18, 20), (11, 23), (12, 24), (23, 24), (23, 25),
(24, 26), (25, 27), (26, 28), (27, 29), (28, 30),
(29, 31), (30, 32), (27, 31), (28, 32)]
# vector_connection = [
# (2, 15), # left eye to left wrist -> 0
# (5, 16), # right eye to right wrist -> 1
# (0,15), # nose to left wrist -> 2
# (0,16), # nose to right wrist -> 3
# (11, 15), # left shoulder to left wrist -> 4
# (12, 16), # right shoulder to right wrist -> 5
# (13, 15), # left elbow to left wrist -> 6
# (14, 16), # right elbow to right wrist -> 7
# ]
# Color assignment
WHITE_COLOR = (224/255, 224/255, 224/255) # (shoulder to wrist)
BLACK_COLOR = (0, 0, 0)
RED_COLOR = (0, 0, 255/255) #(elbow to wrist)
GREEN_COLOR = (0, 128/255, 0) #(nose to wrist)
BLUE_COLOR = (255/255, 0, 0) #(eye to wrist)
GAZE_COLOR = (255/255,255/255,0) # yellow
# left eye left wrist
# right eye right wrist
# mid eye left wrist
# mid eye right wrist
# nose to left wrist
# nose to right wrist
# left shoulder left wrist
# right shoulder right wrist
# left elbow left wrist
# right elbow right wrist
color_list = {"left_eye": BLUE_COLOR, "right_eye": BLUE_COLOR, "mid_eye_left": 'm',
"mid_eye-right": 'm', "nose_to-left": GREEN_COLOR, "nose_to-right": GREEN_COLOR, "left_shoulder": BLACK_COLOR,
"right_shoulder": BLACK_COLOR, "left_elbow": RED_COLOR, "right_elbow": RED_COLOR, "gaze": GAZE_COLOR}
vector_list = ["eye", "eyes ave.", "nose","shoulder", "elbow"]
output = {"image" : []}
for i in range(len(ld["image"])):
# string var that indicates whether or not the left, right, or both pointing arm is used
if 'right' in ld["image"][i]['name'][0]:
point_arm = 'right'
elif 'left' in ld["image"][i]['name'][0]:
point_arm = 'left'
else:
point_arm = 'both'
image_output = {"name":[],
"target":[]}
img = ld["image"][i]
# # 3D plotting
# fig = plt.figure()
# ax1 = fig.add_subplot(111, projection='3d')
fig1 = plt.figure(figsize=(5, 8))
# fig2 = plt.figure(figsize=(10, 5))
ax1 = fig1.add_subplot(212, projection='3d')
ax2 = fig1.add_subplot(211)
# Get body coornidates for all joints
lmk = pd.DataFrame(img["landmark_3D"])
img_name = img["name"][0]
if img_name[0:len(img_name)-4] in old_target_img:
print(img_name)
targets = old_targets
target = int(img_name[0])
image_output['name'].append(img_name)
image_output['target'].append(target)
ground = pd.DataFrame(vaid["image"][i]["ground"])
vector = pd.DataFrame(vaid["image"][i]["vector"])
bad_cols = []
for column in ground.columns:
if point_arm not in column:
bad_cols.append(column)
ground = ground.drop(columns = bad_cols)
vector = vector.drop(columns = bad_cols)
# print("ground", ground)
# print("vector", vector)
offset = vaid["image"][i]["offset"]
# # plot joint coordinates, y, z flipped
# for name in lmk['landmark_name']:
# x = lmk.loc[lmk['landmark_name']==name,'x'].values[0]
# y = lmk.loc[lmk['landmark_name']==name,'y'].values[0] - offset
# z = lmk.loc[lmk['landmark_name']==name,'z'].values[0]
# ax1.scatter(x, z, y, c='b', marker = 'o')
# draw skeleton, y, z flipped
for (start, end) in pose_connection:
x = [lmk.iloc[start]["x"], lmk.iloc[end]["x"]]
y = [lmk.iloc[start]["y"]-offset, lmk.iloc[end]["y"]-offset]
z = [lmk.iloc[start]["z"], lmk.iloc[end]["z"]]
ax1.plot(x, z, y, color='blue')
# plot target position
for i in range(len(targets)):
t = targets[i]
x = t[0]
y = t[1]
z = t[2]
if i+1 == target:
c = 'green'
else:
c = 'grey'
ax1.scatter(x, z, y, marker = 's', color = c, alpha = 0.5, s = 100)
# plot ground intersection & vector
c_list = {"left_eye": BLUE_COLOR, "right_eye": BLUE_COLOR, "mid_eye_left": 'm',
"mid_eye-right": 'm', "nose_to-left": GREEN_COLOR, "nose_to-right": GREEN_COLOR, "left_shoulder": BLACK_COLOR,
"right_shoulder": BLACK_COLOR, "left_elbow": RED_COLOR, "right_elbow": RED_COLOR, "gaze": GAZE_COLOR}
for name in ground:
keywords = name.split("-")
component = keywords[0] # left_eye, right_eye, mid_eye, nose_to, left_shoulder, right_shoulder, left_elbow, right_elbow
x = ground[name][0]
y = ground[name][1]
z = ground[name][2]
# edge cases based on naming scheme
if component == 'mid_eye':
component = 'mid_eye_left'
elif component == 'nose_to':
component = 'nose_to-right'
c = c_list[component]
#v = vector_list[component]
ax1.scatter(x, z, y, color = c, marker = 'o')
# plot connection vector
# print(ground)
for name in ground:
keywords = name.split("-")
component = keywords[0] # left_eye, right_eye, mid_eye, nose_to, left_shoulder, right_shoulder, left_elbow, right_elbow
x = ground[name][0]
y = ground[name][1]
z = ground[name][2]
# edge cases based on naming scheme
if component == 'mid_eye':
component = 'mid_eye_left'
elif component == 'nose_to':
component = 'nose_to-right'
# edge cases
if 'mid' in name:
left = lmk.loc[lmk['landmark_name'] == 'left_eye']
right = lmk.loc[lmk['landmark_name'] == 'right_eye']
startX = (left["x"].item() + right["x"].item())/2
startY = (left["y"].item() + right["y"].item())/2
startZ = (left["z"].item() + right["z"].item())/2
end = name.split("-")[1]
end = lmk.loc[lmk['landmark_name'] == end]
x = [startX, end["x"].item()]
y = [startY-offset, end["y"].item()-offset]
z = [startZ, end["z"].item()]
to_ground_x = [end["x"].item(), ground[name][0]]
to_ground_y = [end["y"].item()-offset, ground[name][1]]
to_ground_z = [end["z"].item(), ground[name][2]]
else:
if 'nose' in name:
start = lmk.loc[lmk['landmark_name'] == 'nose']
else:
start = lmk.loc[lmk['landmark_name'] == component]
end = name.split("-")[1]
end = lmk.loc[lmk['landmark_name'] == end]
x = [start["x"].item(), end["x"].item()]
y = [start["y"].item()-offset, end["y"].item()-offset]
z = [start["z"].item(), end["z"].item()]
to_ground_x = [end["x"].item(), ground[name][0]]
to_ground_y = [end["y"].item()-offset, ground[name][1]]
to_ground_z = [end["z"].item(), ground[name][2]]
c = color_list[component]
ax1.plot(x, z, y, color=c)
ax1.plot(to_ground_x, to_ground_z, to_ground_y, color=c, linestyle='dashed', label = component)
ax1.legend()
# plot pointing vectors
# draw gaze information (vector perpendicular to head plane, with origin from nose)
gaze_direction = vector['perpendicular_to_face'].tolist()
nose = lmk[lmk['landmark_name'] == 'nose'].values.flatten().tolist()[1:]
# vec_x = (nose[0], nose[0]+100*gaze_direction[0])
# vec_y = (nose[1]-offset, nose[1]+100*gaze_direction[1]-offset)
# vec_z = (nose[2], nose[2]+100*gaze_direction[2])
# ax1.plot(vec_x, vec_z, vec_y, color=GAZE_COLOR, linestyle='dashed', linewidth = 3) # without reflection
reflec_x = (nose[0], nose[0]-100*gaze_direction[0])
reflec_y = (nose[1]-offset, nose[1]-100*gaze_direction[1]+offset)
reflec_z = (nose[2], nose[2]-100*gaze_direction[2])
ax1.plot(reflec_x, reflec_z, reflec_y, color=GAZE_COLOR, linestyle='dashed', linewidth = 3, label = "gaze") # with reflection
# output target distance using different vectors
# Set ax1is labels
ax1.set_xlabel('X')
ax1.set_ylabel('Z')
ax1.set_zlabel('Y')
ax1.set_xlim(-2, 2)
ax1.set_zlim( -0.1, 1.8)
ax1.set_ylim(-2, 2)
# # Make the ax1is planes solid gray
# ax1.w_xaxis.pane.fill = True # Disable filling the x-ax1is plane
# ax1.w_yaxis.pane.fill = True # Disable filling the y-ax1is plane
# ax1.w_zaxis.pane.fill = True # Disable filling the z-ax1is plane
# Set the view to have y as vertical and z pointing out
ax1.view_init(azim=-50, elev=20)
# Set plot title
ax1.set_title('3D Plot of %s'%img_name)
# 2D distance plotting
# ax2 = fig2.add_subplot(121)
# ax3 = fig2.add_subplot(122)
# ax2.grid(True)
# ax2.set_axisbelow(True)
# ax3.grid(True)
# ax3.set_axisbelow(True)
# plot target position
# for i in range(len(targets)):
# t = targets[i]
# x = t[0]
# y = t[2]
# if i+1 == target:
# c = 'green'
# else:
# c = 'grey'
# ax2.scatter(x, y, marker = 's', color = c, s = 100, label = str(i+1))
# plot ground intersection & vector
c_list = {"left_eye": BLUE_COLOR, "right_eye": BLUE_COLOR, "mid_eye_left": 'm',
"mid_eye-right": 'm', "nose_to-left": GREEN_COLOR, "nose_to-right": GREEN_COLOR, "left_shoulder": BLACK_COLOR,
"right_shoulder": BLACK_COLOR, "left_elbow": RED_COLOR, "right_elbow": RED_COLOR}
x_values = []
y_values = []
dist_values = []
for name in ground:
keywords = name.split("-")
component = keywords[0] # left_eye, right_eye, mid_eye, nose_to, left_shoulder, right_shoulder, left_elbow, right_elbow
x = ground[name][0]
y = ground[name][1]
z = ground[name][2]
# edge cases based on naming scheme
if component == 'mid_eye':
component = 'mid_eye_left'
elif component == 'nose_to':
component = 'nose_to-right'
tx = targets[target-1][0]
ty = targets[target-1][2]
x = ground[name][0]
y = ground[name][2]
c = c_list[component]
#v = vector_list[i_temp]
# ax2.scatter(x, y, color = c, marker = 'o')
x_values.append(abs(tx-x))
y_values.append(abs(ty-x))
dist_values.append(np.sqrt((tx-x)**2 + (ty-y)**2))
image_output['vector_name'] = vector_list
image_output['x_diff'] = x_values
image_output['y_diff'] = y_values
image_output['dist'] = dist_values
print(image_output)
img = cv2.imread(script_dir+'/data/'+img_name)
ax2.imshow(cv2.cvtColor(img, cv2.COLOR_RGB2BGR))
# ax2.set_title("x-y plane visualization")
# ax2.set_xlabel("X")
# ax2.set_ylabel("Y")
categories = vector_list
# categories = ["eye", "eye(ave.)", "nose", "shoulder", "elbow"]
# # Create a bar graph
# bar_width = 0.24
# x = np.arange(len(categories))
# ax3.bar(x - bar_width, x_values, bar_width, label='x_diff', color='blue')
# ax3.bar(x, y_values, bar_width, label='y_diff', color='green')
# ax3.bar(x + bar_width, dist_values, bar_width, label='Distance', color='red')
# ax3.set_xticks(x, categories, rotation=90)
# ax3.set_title('distance distribution of vectors to target%i'%target)
# ax3.set_ylabel('distance to target[m]')
# # Show the plot
# plt.subplots_adjust(bottom = 0.2)
fig1.savefig(script_dir+'/plot/%s_3D.png'%img_name[0:len(img_name)-4])
# fig2.savefig(script_dir+'/plot/%s_target_%i.png'%(img_name[0:len(img_name)-4],target))
plt.legend()
plt.show()
# save output to json
output["image"].append(image_output)
with open(script_dir+'/dist_data.json', 'w') as json_file:
json.dump(output, json_file, indent=4)
print("Finished exporting landmark data.")