This repository has been archived by the owner on Dec 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
141 lines (94 loc) · 3.69 KB
/
main.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
#!/usr/bin/env python
#author: Sourav R S
#Github: https://github.com/souravrs999
#General Imports
import cv2
import time
import socket
import numpy as np
from UnityGaze import GazeEstimation
from scipy.spatial import distance as dist
from UnityGaze.VideoCapture import WebcamVideoStream
# Get the screen resolution
def get_screen_res():
import tkinter as tk
root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
return screen_width, screen_height
def get_gaze_coords(cam_frame):
left_pupil = gaze.pupil_left_coords()
right_pupil = gaze.pupil_right_coords()
coord_coll = gaze.eye_roi_bb()
if coord_coll is not None:
left_coords = coord_coll[0]
right_coords = coord_coll[1]
left_x1 = left_coords[0]
left_x2 = left_coords[1]
left_y1 = left_coords[2]
left_y2 = left_coords[3]
right_x1 = right_coords[0]
right_x2 = right_coords[1]
right_y1 = right_coords[2]
right_y2 = right_coords[3]
''' Average of the extreme cordinates of
left pupil and right pupil '''
avg_x1 = int((left_x1 + right_x1)/2 + 5)
avg_x2 = int((left_x2 + right_x2)/2 - 5)
avg_y1 = int((left_y1 + right_y1)/2 - 2)
avg_y2 = int((left_y2 + right_y2)/2 + 2)
'''Find the distance between the centre
points of the bounding box to get the width
and height of the box'''
avg_bbox_w = int(dist.euclidean((avg_x1,avg_y1), (avg_x2, avg_y1)))
avg_bbox_h = int(dist.euclidean((avg_x1,avg_y1), (avg_x1, avg_y2)))
if left_pupil or right_pupil is not None:
try:
''' Average pupil center '''
avg_pupil_x = int((right_pupil[0] + left_pupil[0])/2)
avg_pupil_y = int((right_pupil[1] + left_pupil[1])/2)
p_x_2_bbox = (avg_x1, avg_pupil_y)
p_y_2_bbox = (avg_pupil_x, avg_y1)
''' Distance of the gaze coordinate from the left
and top of the bounding box '''
p_2_bbox_w_d = int(dist.euclidean((p_x_2_bbox), (avg_pupil_x, avg_pupil_y)))
p_2_bbox_h_d = int(dist.euclidean((p_y_2_bbox), (avg_pupil_x, avg_pupil_y)))
''' Ratio of width and height of the gaze coordinate
with respect to the bounding box '''
p_x_2_bbox_w_r = avg_bbox_w/p_2_bbox_w_d
p_y_2_bbox_h_r = avg_bbox_h/p_2_bbox_h_d
except Exception:
pass
return p_x_2_bbox_w_r, p_y_2_bbox_h_r
if __name__ == "__main__":
gaze = GazeEstimation()
''' Monitor resolution '''
sc_width, sc_height = get_screen_res()
host, port = "127.0.0.1", 25001
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
cam = WebcamVideoStream(2).start()
while True:
frame = cam.read()
gaze.refresh(frame)
p_xy = get_gaze_coords(frame)
if p_xy is not None:
''' Estimating coordinates for the screen
with respect to its resolution '''
est_x = int((sc_width/p_xy[0]))
est_y = int((sc_height/p_xy[1]))
time.sleep(0.5)
if gaze.is_blinking:
blink = 1
else:
blink = 0
gaze_coord = [est_x, est_y, blink]
packet = ','.join(map(str, gaze_coord))
sock.sendall(packet.encode("UTF-8"))
frame = gaze.annotated_frame()
cv2.namedWindow('Demo',cv2.WINDOW_NORMAL)
cv2.imshow("Demo", frame)
if cv2.waitKey(1) == 27:
break
cam.stop()
cv2.destroyAllWindows()