-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcd_for_inet_circle.py
143 lines (111 loc) · 3.78 KB
/
gcd_for_inet_circle.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
import sys, os
import tobii_research as tr
import pandas as pd
import numpy as np
from PIL import Image, ImageDraw, ImageFilter
from psychopy import core, visual, gui, data, event
sys.path.append("../")
from tools import features
def gaze_data_callback(gaze_data):
# Print gaze points of left and right eye
# print("Left eye: ({gaze_left_eye}) \t Right eye: ({gaze_right_eye})".format(
# gaze_left_eye=gaze_data['left_gaze_point_on_display_area'],
# gaze_right_eye=gaze_data['right_gaze_point_on_display_area']))
gaze_left_eye = gaze_data[
"left_gaze_point_on_display_area"
] # (y_coordinate, x_coordinate)
gaze_right_eye = gaze_data[
"right_gaze_point_on_display_area"
] # (y_coordinate, x_coordinate)
out.append([gaze_left_eye, gaze_right_eye])
# Change working directory
if not os.path.isfile(os.path.basename(__file__)):
os.chdir("./experiments")
# Settings
found_eyetrackers = tr.find_all_eyetrackers()
my_eyetracker = found_eyetrackers[0]
# Variables
display_size = [1920, 1080]
img_original_path = (
"C:\\Users\\Coginf\\repos\\tobii_sdk\\imagenet_tree_renew\\128_128\\128_128.png"
)
img_resized_640_path = (
"C:\\Users\\CogInf\\repos\\tobii_sdk\\imagenet_tree_renew\\640_640\\640_640.png"
)
# Read Images
img_original = Image.open(img_original_path).resize(display_size)
img_size = img_original.size
img_resized_640 = Image.open(img_resized_640_path).resize(display_size)
# Output file
out = []
# Show eye tracker settings
features.show_eyetracker(my_eyetracker)
# Create and Show introduction
win, message1 = features.introduction(display_size)
# Start eye tracking
my_eyetracker.subscribe_to(
tr.EYETRACKER_GAZE_DATA, gaze_data_callback, as_dictionary=True
)
test_image = visual.ImageStim(
win,
image=img_original,
pos=[0, 0],
)
test_image.draw()
# circle = visual.Circle(
# win,
# units="norm", # [(-1.0, 1.0), (-1.0, 1.0)],
# size=(0.1 / (display_size[0] / display_size[1]), 0.1),
# lineColor=(0, 255, 255),
# )
win.flip(clearBuffer=True)
count = 0
x_before = 0
y_before = 0
while len(out) == 0:
continue
while True:
x, y = out[-1][0]
if np.isnan(x) or np.isnan(y):
x = x_before
y = y_before
# Trace subject's eye
gaze = (int(x * img_size[0]), int(y * img_size[1]))
# Create mask
tl_inner, br_inner = (gaze[0] - 200, gaze[1] - 200), (gaze[0] + 200, gaze[1] + 200)
mask_base = Image.new("L", img_size, 0)
mask = ImageDraw.Draw(mask_base)
mask.ellipse((tl_inner, br_inner), fill=255)
mask_blur = mask_base.filter(ImageFilter.GaussianBlur(10))
# Create modified image
test = Image.composite(img_original, img_resized_640, mask_blur)
# Create outer mask
# tl_outer, br_outer = (gaze[0] - 500, gaze[1] - 500), (gaze[0] + 500, gaze[1] + 500)
# mask.ellipse((tl_outer, br_outer), fill=255)
# mask_blur = mask_base.filter(ImageFilter.GaussianBlur(10))
# # Create modified image
# test = Image.composite(test, img_resized_640, mask_blur)
test_image.image = test
# Modify x, y that the origin becomes center of the display
# y_circle = -y
# x_circle, y_circle = 2 * (x - 0.5), 2 * (y_circle + 0.5)
# circle.pos = (x_circle, y_circle)
test_image.draw()
# circle.draw()
win.flip(clearBuffer=True)
count += 1
x_before = x
y_before = y
# Escape command
if "space" in event.getKeys():
break
# End eye tracking
my_eyetracker.unsubscribe_from(tr.EYETRACKER_GAZE_DATA, gaze_data_callback)
out = pd.DataFrame(out)
# Save outputs as a csv file
path = "./out.csv"
features.save_csv(out, path)
print(out.tail())
print(count)
win.close()
core.quit()