-
Notifications
You must be signed in to change notification settings - Fork 4
/
head_orientation_lib.py
135 lines (112 loc) · 4.41 KB
/
head_orientation_lib.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
import numpy as np
from pyquaternion import Quaternion
H = 90
W = 160
topic_info_dict = {'paris': ['paris.mp4', 244.06047, 3840, 2048], \
'timelapse': ['newyork.webm', 91.03333, 3840, 2048], \
'3': ['conan2.mp4', 172.5724, 2560, 1440], \
'1': ['skiing.mp4', 201.13426, 2560, 1440], \
'0': ['conan1.mp4', 164.1973, 2560, 1440], \
'venise': ['venise.webm', 175.04, 3840, 2160],\
'2': ['alien.mp4', 293.2333, 2560, 1440], \
'5': ['war.mp4', 655.0544, 2160, 1080], \
'4': ['surfing.mp4', 205.7055, 2560, 1440], \
'7': ['football.mp4', 164.8, 2560, 1440], \
'6': ['cooking.mp4', 451.12, 2560, 1440], \
'diving': ['ocean40.webm', 372.23853, 3840, 2048], \
'roller': ['roller65.webm', 69.0, 3840, 2048], \
'8': ['rhinos.mp4', 292.0584, 2560, 1440],\
'coaster2_': ['', 60.0, -1, -1], \
'coaster_': ['', 60.0, -1, -1], \
'diving': ['', 60.0, -1, -1], \
'drive': ['', 60.0, -1, -1], \
'game': ['', 60.0, -1, -1], \
'landscape': ['', 60.0, -1, -1], \
'pacman': ['', 60.0, -1, -1],\
'panel': ['', 60.0, -1, -1], \
'ride': ['', 60.0, -1, -1], \
'sport': ['', 60.0, -1, -1] }
#['coaster2_', 'coaster', 'diving', 'drive', 'game', 'landscape', 'pacman', 'panel', 'ride', 'sport']
def extract_direction_dataset1(q):
#q is quaternion
v0 = [1, 0, 0]
q = Quaternion([q[3], q[2], q[1], q[0]])
return q.rotate(v0)
def extract_direction_dataset2(q):
#q is quaternion
v0 = [0, 0, 1]
q = Quaternion([q[3], -q[2], q[1], -q[0]])
return q.rotate(v0)
def extract_direction_dataset3(q):
#q is quaternion
v0 = [0, 0, 1]
q = Quaternion([q[3], -q[2], q[1], -q[0]])
return q.rotate(v0)
def pixel_to_ang(_x, _y, _geo_h, _geo_w):
phi = geoy_to_phi(_x, _geo_h)
theta = -(_y * 1.0 / _geo_w) * 360
if theta < -180: theta = 360 + theta
return theta, phi
def geoy_to_phi(_geoy, _height):
d = (_height/2 - _geoy) * 1.0 / (_height/2)
s = -1 if d < 0 else 1
return s * np.arcsin(np.abs(d)) / np.pi * 180
def unit_vector(vector):
return vector / np.linalg.norm(vector)
def degree_distance(v1, v2):
v1_u = unit_vector(v1)
v2_u = unit_vector(v2)
return np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))/np.pi * 180
def angle_between(v1, v2):
v1_u = unit_vector(v1)
v2_u = unit_vector(v2)
return np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))/np.pi * 180
#lib to create fixation map
def vector_to_ang(_v):
#v = np.array(vector_ds[0][600][1])
#v = np.array([0, 0, 1])
_v = np.array(_v)
alpha = degree_distance(_v, [0, 1, 0])#degree between v and [0, 1, 0]
phi = 90.0 - alpha
proj1 = [0, np.cos(alpha/180.0 * np.pi), 0] #proj1 is the projection of v onto [0, 1, 0] axis
proj2 = _v - proj1#proj2 is the projection of v onto the plane([1, 0, 0], [0, 0, 1])
theta = degree_distance(proj2, [1, 0, 0])#theta = degree between project vector to plane and [1, 0, 0]
sign = -1.0 if degree_distance(_v, [0, 0, -1]) > 90 else 1.0
theta = sign * theta
return theta, phi
def ang_to_geoxy(_theta, _phi, _h, _w):
x = _h/2.0 - (_h/2.0) * np.sin(_phi/180.0 * np.pi)
temp = _theta
if temp < 0: temp = 180 + temp + 180
temp = 360 - temp
y = (temp * 1.0/360 * _w)
return x, y#wi, hi
def adjust_pixel_dataset3(hi, wi, H, W):
wi = W - wi
wi = wi - W/4
if wi < 0:
wi = wi + W
return hi, wi
def adjust_pixel_dataset2(hi, wi, H, W):
wi = W - wi
if wi < 0:
wi = wi + W
return hi, wi
def adjust_pixel_dataset1(hi, wi, H, W):
hi = H - hi
if hi < 0:
hi = hi + H
return hi, wi
def adjust_pixellist_dataset(dataset, pixel_list, H, W):
rhi_list = []
rwi_list = []
for hi, wi in pixel_list:
if dataset == 1:
hi, wi = adjust_pixel_dataset1(hi, wi, H, W)
elif dataset == 2:
hi, wi = adjust_pixel_dataset2(hi, wi, H, W)
elif dataset == 3:
hi, wi = adjust_pixel_dataset3(hi, wi, H, W)
rhi_list.append(hi)
rwi_list.append(wi)
return zip(rhi_list, rwi_list)