forked from XiangGuo1992/ORCL_VR_EyeTracking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.PlotEyeTrackingOnImgs.py
More file actions
188 lines (131 loc) · 5.49 KB
/
Copy path2.PlotEyeTrackingOnImgs.py
File metadata and controls
188 lines (131 loc) · 5.49 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
177
178
179
180
181
182
183
184
185
186
187
188
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 25 16:15:43 2020
@author: Xiang Guo
"""
import numpy as np
import os
import cv2
import re
from matplotlib import pyplot as plt
import pandas as pd
from tqdm import tqdm
os.chdir('C:/github/ORCL_VR_EyeTracking/Data/Video/')
frame_dir = 'movie2020-08-2818h08m'
framelist = os.listdir(os.path.join('2.videos_frames',frame_dir))
frame_out_dir = os.path.join('3.video_frames_out_LR',frame_dir)
try:
os.mkdir(frame_out_dir)
except:
pass
#number of frame images
framelen=len(framelist)
# read the raw data
csv_dir = 'C:/github/ORCL_VR_EyeTracking/Data/EyeTrakcing/TobiiProUnity/'
csv_file = 'vr_data_20200828T180840.csv'
df = pd.read_csv(csv_dir + csv_file)
df_valid = df[df['CombinedGazeRayWorldValid'] == True]
#length of raw data
dflen = len(df)
img_path = os.getcwd() + '/2.videos_frames/' + frame_dir + '/' + framelist[0]
img =cv2.imread(img_path)
dimensions = img.shape
# height, width, number of channels in image
height = img.shape[0]
width = img.shape[1]
def closest(lst, K):
return lst[min(range(len(lst)), key = lambda i: abs(lst[i]-K))]
time_series = df['AdjustedTime']
valid_time_series = df_valid['AdjustedTime']
# Given the gaze vector, return the x/y position for the gaze point in open-cv video
def return_x_y(gaze):
pattern = "[" + '(' + ')' + "]"
new_gaze_string = re.sub(pattern, "", gaze)
xyz = new_gaze_string.split(',')
x= width/2*float(xyz[0])
y = height/2*float(xyz[1])
cv_x = round(width/2 + x)
cv_y = round(height/2 - y)
return cv_x,cv_y
for j in tqdm(framelist):
i = int(re.findall("\d+",j)[0])
img_path = os.getcwd() + '/2.videos_frames/' + frame_dir + '/' + j
# time from the video
t = i/30
# time in raw data
t1 = closest(list(time_series), t)
t_index = list(time_series).index(t1)
if df['CombinedGazeRayWorldValid'][t_index] == True:
#gaze_dir = df['CombinedGazeRayWorldDirection'][t_index]
gaze_l = df['LeftGazeDirection'][t_index]
gaze_r = df['RightGazeDirection'][t_index]
gaze_origin_l = df['LeftGazeOrigin'][t_index]
gaze_origin_r = df['RightGazeOrigin'][t_index]
#cv_x,cv_y = return_x_y(gaze_dir)
cv_x_l,cv_y_l = return_x_y(gaze_l)
cv_x_r,cv_y_r = return_x_y(gaze_r)
img =cv2.imread(img_path)
#cv2.circle(img,(cv_x, cv_y), 20, (0,0,255),-1)
cv2.circle(img,(cv_x_l, cv_y_l), 20, (0,255,0),-1)
cv2.circle(img,(cv_x_r,cv_y_r), 20, (255,0,0),-1)
#plt.imshow(img)
cv2.imwrite(frame_out_dir + '/' + re.findall("\d+",j)[0] +'.jpg',img)
else:
# Find the closest time in valid raw data
t_2 = closest(list(valid_time_series), t)
if abs(t_2 - t) <= 0.1:
t2_index = valid_time_series[valid_time_series == t_2].index[0]
#gaze_dir = df['CombinedGazeRayWorldDirection'][t2_index]
gaze_l = df['LeftGazeDirection'][t2_index]
gaze_r = df['RightGazeDirection'][t2_index]
#cv_x,cv_y = return_x_y(gaze_dir)
cv_x_l,cv_y_l = return_x_y(gaze_l)
cv_x_r,cv_y_r = return_x_y(gaze_r)
img =cv2.imread(img_path)
#cv2.circle(img,(cv_x, cv_y), 20, (0,0,255),-1)
cv2.circle(img,(cv_x_l, cv_y_l), 20, (0,255,0),-1)
cv2.circle(img,(cv_x_r,cv_y_r), 20, (255,0,0),-1)
#plt.imshow(img)
cv2.imwrite(frame_out_dir + '/' + re.findall("\d+",j)[0] +'.jpg',img)
else:
img =cv2.imread(img_path)
cv2.imwrite(frame_out_dir + '/' + re.findall("\d+",j)[0] +'.jpg',img)
'''
for j in tqdm(framelist):
i = int(re.findall("\d+",j)[0])
img_path = os.getcwd() + '\\videos_frames\\' + frame_dir + '\\' + j
# time from the video
t = i/30
# time in raw data
t1 = closest(list(time_series), t)
t_index = list(time_series).index(t1)
if df['CombinedGazeRayWorldValid'][t_index] == True:
gaze_dir = df['CombinedGazeRayWorldDirection'][t_index]
xyz = re.findall("\d+\.\d+", gaze_dir)
x= width/2*float(xyz[0])
y = height/2*float(xyz[1])
cv_x = round(width/2-x)
cv_y = round(height/2 -y)
img =cv2.imread(img_path)
cv2.circle(img,(cv_x, cv_y), 20, (0,255,0),-1)
#plt.imshow(img)
cv2.imwrite(frame_out_dir + '\\' + re.findall("\d+",j)[0] +'_output.jpg',img)
else:
# Find the closest time in valid raw data
t_2 = closest(list(valid_time_series), t)
if abs(t_2 - t) <= 0.3:
t2_index = valid_time_series[valid_time_series == t_2].index[0]
gaze_dir = df['CombinedGazeRayWorldDirection'][t2_index]
xyz = re.findall("\d+\.\d+", gaze_dir)
x= width/2*float(xyz[0])
y = height/2*float(xyz[1])
cv_x = round(height/2-y)
cv_y = round(width/2 - x)
img =cv2.imread(img_path)
cv2.circle(img,(cv_x, cv_y), 20, (0,255,0),-1)
#plt.imshow(img)
cv2.imwrite(frame_out_dir + '\\' + re.findall("\d+",j)[0] +'_output.jpg',img)
else:
img =cv2.imread(img_path)
cv2.imwrite(frame_out_dir + '\\' + re.findall("\d+",j)[0] +'_output.jpg',img)
'''