-
Notifications
You must be signed in to change notification settings - Fork 2
/
gaze_server.py
77 lines (62 loc) · 2.33 KB
/
gaze_server.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
from gaze_detector_from_camera_stream import GazeDetectorStream
import json
import socket
from WebcamVideoStream import WebcamVideoStream
from FaceAndEyeDetectorStream import FaceAndEyeDetectorStream
from gaze import test_faces
from lib import current_time, smooth_outputs
from gaze_detector import extract_features_and_detect_gazes
import signal
import sys
def to_output_string(outputs):
output_string = None
if (len(outputs) > 0):
output_string = ""
for i, output in enumerate(outputs):
if output is not None:
output_string += str(output[0]) + ',' + str(output[1])
if (i < len(outputs) - 1):
output_string += '_'
# print("output string", output_string)
return output_string
HOST = '' # Symbolic name meaning all available interfaces
PORT = 4001 # Arbitrary non-privileged port
vs = FaceAndEyeDetectorStream(0).start()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
last_read = current_time()
set_to_gpu = False
previous_outputs = []
previous_frame_time = None
try:
# print('Connected by', addr)
while True:
img, faces, face_features, frame_time = vs.read()
# print(img.shape, faces, face_features)
if img is not None:
new_outputs = test_faces(img, faces, face_features)
if len(new_outputs) > 0:
outputs = smooth_outputs(new_outputs, frame_time, previous_outputs, previous_frame_time)
# print('original outputs', new_outputs)
# print('smoothed outputs', outputs)
previous_outputs = outputs
previous_frame_time = frame_time
# outputs.append([0, 0])
# outputs.append([1, 1])
output_string = to_output_string(outputs)
if output_string:
print('time since taken ', current_time() - frame_time)
conn.send((output_string + '\n').encode())
last_read = current_time()
# if not data: break
# do whatever you need to do with the data
finally:
vs.stop()
conn.close()
def signal_handler(signal, frame):
vs.stop()
conn.close()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)