-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgaze_deviation.py
172 lines (103 loc) · 5.66 KB
/
gaze_deviation.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
'''
MIT License
Copyright (c) [2020] [Duin BAEK]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
import os
import ast
import math
import pickle
import numpy as np
import pandas as pd
def csv2pandas(csv_path):
data = pd.read_csv(csv_path, delimiter = ';')
data_columns = list(data.columns)
dictionarized_data = np.array([ast.literal_eval(line) for line in data[data_columns[0]] if not line in data_columns])
dictionary_keys = list(dictionarized_data[0].keys())
pandas_data = []
for one_dictionary in dictionarized_data:
line_data = []
for key in dictionary_keys:
line_data.append(one_dictionary[key])
pandas_data.append(line_data)
pandas_data = pd.DataFrame(pandas_data, columns = dictionary_keys)
return pandas_data
duration = 30
duration_units = [2, 5, 10]
csv_base_path = 'data/csv'
'''
IF YOU HAVE VIDEO SEGMENT DIRECTORY, YOU CAN THE FOLLOWING PATH TO GET Y_P_COMBO
video_base_path = 'video/segments/cube'
Y_P_combo = list(os.listdir(video_base_path))
'''
data_save_path = 'data/angle_difference'
Y_P_combo = pickle.load(open(os.path.join(data_save_path, 'y_p_combo.pkl'), 'rb'))
combo_data = []
print('Making Gaze Deviation Pickle Files: According to Video Contents(csv) and Duration')
for combo in Y_P_combo:
characters = [char for char in combo]
p_idx = np.where([item == 'P' for item in characters])[0][0]
yaw, pitch = int(combo[1:p_idx]), int(combo[p_idx+1:])
combo_data.append((yaw, pitch))
for duration_unit in duration_units:
num_videos = int(duration / duration_unit)
for csv_idx in range(10):
participant_list = []
duration_yaw_difference_list = []
duration_pitch_difference_list = []
for participant_name in os.listdir(csv_base_path):
participant_yaw_difference_list = []
participant_pitch_difference_list = []
participant_list.append(participant_name)
participant_csv_path = os.path.join(csv_base_path, participant_name)
csv_path = os.path.join(participant_csv_path, str(csv_idx) + '.csv')
pandas_data = csv2pandas(csv_path)
position_data = (pandas_data['position'] / 1000).astype(np.int32)
idx = 0
for yaw_value, pitch_value, position in zip(pandas_data['yaw'], pandas_data['pitch'], position_data):
if position == idx*duration_unit:
starting_idx = np.where(position_data == idx*duration_unit)[0][0]
yaw = pandas_data['yaw'][starting_idx]
pitch = pandas_data['pitch'][starting_idx]
#user yaw, pitch information
yaw_degree = (math.degrees(yaw) + 360) % 360
pitch_degree = -math.degrees(pitch)
radial_distance = [np.linalg.norm(np.array([Y, P]) - np.array([yaw_degree, pitch_degree])) for Y, P in combo_data]
front_view_yaw, front_view_pitch = combo_data[np.argmin(radial_distance)][0], combo_data[np.argmin(radial_distance)][1]
idx += 1
yaw_deg = (math.degrees(yaw_value) + 360) % 360
pitch_deg = -math.degrees(pitch_value)
yaw_differenece, pitch_difference = np.abs(front_view_yaw - yaw_deg), np.abs(front_view_pitch- pitch_deg)
if yaw_differenece > 180:
yaw_differenece = -(360 - yaw_differenece)
participant_yaw_difference_list.append(yaw_differenece)
participant_pitch_difference_list.append(pitch_difference)
participant_yaw_difference_list = np.array(participant_yaw_difference_list)
participant_pitch_difference_list = np.array(participant_pitch_difference_list)
duration_yaw_difference_list.append(participant_yaw_difference_list)
duration_pitch_difference_list.append(participant_pitch_difference_list)
participant_list = np.array(participant_list)
csv_write_path = os.path.join(data_save_path, str(csv_idx))
if not os.path.exists(csv_write_path):
os.makedirs(csv_write_path)
pickle.dump(participant_list, open(os.path.join(csv_write_path, str(duration_unit) + '_user_order.p'), 'wb'))
duration_yaw_difference_list = np.array(duration_yaw_difference_list)
duration_pitch_difference_list = np.array(duration_pitch_difference_list)
pickle.dump(duration_yaw_difference_list, open(os.path.join(csv_write_path, str(duration_unit) + '_yaw_difference.p'), 'wb'))
pickle.dump(duration_pitch_difference_list, open(os.path.join(csv_write_path, str(duration_unit) + '_pitch_difference.p'), 'wb'))
print('DONE! PLEASE CHECK \'data/angle_difference\' DIRECTORY!')
#end