gazesim/code/recording/eye_world_correlation.py

86 lines
3.3 KiB
Python

'''
(*)~----------------------------------------------------------------------------------
author:
Julian Steil
Master Thesis (2014):
Discovery of eye movement patterns in long-term human visual behaviour using topic models
----------------------------------------------------------------------------------~(*)
'''
import sys,os
import numpy as np
PARTICIPANTS = ['p10', 'p16', 'p13', 'p24', 'p5', 'p14', 'p26', 'p12', 'p20', 'p7', 'p15', 'p11', 'p21', 'p25']
ROOT = '/home/mmbrian/HiWi/etra2016_mohsen/code/recording/data/participants'
timestamps_world_path = 'world_timestamps.npy'
timestamps_eye_path = 'eye0_timestamps.npy'
def main():
for p in os.listdir(ROOT):
if p in PARTICIPANTS:
print '> Correlating eye-world images for', p
d1 = os.path.join(ROOT, p)
d1 = os.path.join(d1, os.listdir(d1)[0]) # ../p_i/../
for d2 in os.listdir(d1):
path = os.path.join(d1, d2)
print '> Processing', path
process(path)
print 'Done.'
def process(root):
timestamps_world = list(np.load(os.path.join(root, timestamps_world_path)))
timestamps_eye = list(np.load(os.path.join(root, timestamps_eye_path)))
no_frames_eye = len(timestamps_eye)
no_frames_world = len(timestamps_world)
# Detection of Synchronization-Matchings to initialize the correlation-matrix
frame_idx_world = 0
frame_idx_eye = 0
while (frame_idx_world < no_frames_world):
# if the current world_frame is before the mean of the current eye frame timestamp and the next eyeframe timestamp
if timestamps_world[frame_idx_world] <= (timestamps_eye[frame_idx_eye]+timestamps_eye[frame_idx_eye+1])/2.:
frame_idx_world+=1
else:
if frame_idx_eye >= no_frames_eye-2:
break
frame_idx_eye+=1
no_of_matched_frames = frame_idx_eye
print "no_of_matched_frames: ", no_of_matched_frames
# Synchonizing eye and world cam
print no_frames_eye, no_frames_world
correlation = []
for i in xrange(no_frames_world):
correlation.append([])
for j in xrange(1):
correlation[i].append(float(0))
frame_idx_world = 0
frame_idx_eye = 0
while (frame_idx_world < no_frames_world):
# print frame_idx_world,frame_idx_eye
# if the current world_frame is before the mean of the current eye frame timestamp and the next eye timestamp
if timestamps_world[frame_idx_world] <= (timestamps_eye[frame_idx_eye]+timestamps_eye[frame_idx_eye+1])/2.:
correlation[frame_idx_world][0] = frame_idx_eye
frame_idx_world+=1
else:
if frame_idx_eye >= no_frames_eye-2:
frame_idx_eye += 1
while (frame_idx_world < no_frames_world):
correlation[frame_idx_world][1] = frame_idx_eye
frame_idx_world+=1
break
frame_idx_eye+=1
correlation_list_path = "eye_world_correlation.npy"
correlation_list_csv_path = "eye_world_correlation.csv"
np.save(os.path.join(root, correlation_list_path),np.asarray(correlation))
np.savetxt(os.path.join(root, correlation_list_csv_path),np.asarray(correlation), delimiter=",", fmt="%f")
if __name__ == '__main__':
main()