-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlay.py
154 lines (113 loc) · 4.48 KB
/
overlay.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
'''
This script will be used to draw everything that the user will see on their screen
Including:
- Marker indicating where the user is looking
(that should be showing all the time so the user doesn't leave the program running in the background by accident)
Every other process should be carried out in another script
'''
from win32api import GetSystemMetrics
import threading
import tkinter as tk
import PIL.Image
import PIL.ImageTk
import numpy as np
import EyeTracker
import pyautogui
CENTER_OF_SCREEN = (GetSystemMetrics(0) / 2, GetSystemMetrics(1) / 2)
class Window():
def __init__(self, update_time=300):
self._normal_width = 100
self._normal_height = 100
self.XPOS = CENTER_OF_SCREEN[0]
self.YPOS = CENTER_OF_SCREEN[1]
self.WIDTH = self._normal_width
self.HEIGHT = self._normal_height
self.UPDATE_TIME = update_time
self.running = True
self.TIME_PER_POS = 4
self.start_time = 0
#for handling the mouse control
self.WINK_ACTIVATION_FRAME_NUM = 3
self.winkingFrames = 0
self.holdingMouse = False
#for color customization
self.color = 'white'
#setting up the window
self.root = tk.Tk()
self.root.lift()
self.root.config(bg='black')
self.root.overrideredirect(True)
self.root.wm_attributes("-transparentcolor", "black")
self.root.wm_attributes("-topmost", True)
self.temp = PIL.Image.open("Circle.png")
self.img_copy = self.temp.copy()
self.background = PIL.ImageTk.PhotoImage(self.temp)
self.image = tk.Label(self.root, image=self.background, bg='black')
self.image.pack(fill='both', expand='yes')
self.root.bind('<Configure>', self._resize_image)
def _resize_image(self, event):
new_width = event.width
new_height = event.height
self.temp = self.img_copy.resize((new_width, new_height))
#assign color to window (customization)
im = np.array(self.temp)
if self.color == 'red':
im[np.where((im==[255, 255, 255, 255]).all(axis=2))] = [255, 0, 0, 255]
elif self.color == 'green':
im[np.where((im==[255, 255, 255, 255]).all(axis=2))] = [0, 255, 0, 255]
elif self.color == 'blue':
im[np.where((im==[255, 255, 255, 255]).all(axis=2))] = [0, 0, 255, 255]
self.temp = PIL.Image.fromarray(im)
#apply image
self.background = PIL.ImageTk.PhotoImage(self.temp)
self.image.configure(image=self.background)
def close_window(self):
EyeTracker.running = False
self.eyeTrackingThread.join()
EyeTracker.smoother.stop() #stopping the smoothing thread
EyeTracker.stop()
self.running = False
def update(self):
position = (0,0) #default
self.WIDTH = self._normal_width
self.HEIGHT = self._normal_height
#self.image.pack() #show the image
position = list(EyeTracker.get_locations())
position[0] += self.WIDTH/2
position[1] -= self.HEIGHT/2
if self.running:
#handle mouse control
if EyeTracker.winking:
self.winkingFrames += 1
if self.winkingFrames >= self.WINK_ACTIVATION_FRAME_NUM:
#register the wink
pyautogui.moveTo(position[0] + (self._normal_width/2), position[1] + (self._normal_height/2))
if not self.holdingMouse:
pyautogui.mouseDown()
self.holdingMouse = True
self.WIDTH = 0 #hide the window
self.HEIGHT = 0
else:
self.winkingFrames = 0
if self.holdingMouse:
pyautogui.mouseUp()
self.holdingMouse = False
self.root.geometry(f'{self.WIDTH}x{self.HEIGHT}+{int(position[0])}+{int(position[1])}')
if self.running:
self.root.after(self.UPDATE_TIME, self.update)
else:
self.root.destroy()
def start(self):
#start eye tracking
EyeTracker.smoother.start()
EyeTracker.running = True
self.eyeTrackingThread = threading.Thread(target=EyeTracker.trackingThread)
self.eyeTrackingThread.start()
self.running = True
self.root.after(self.UPDATE_TIME, self.update)
self.root.mainloop()
win = Window(5)
def run():
win.start()
if __name__ == '__main__':
run()