-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver_main.py
executable file
·90 lines (69 loc) · 2.76 KB
/
server_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
#!/usr/bin/python
import argparse
import logging
import cv2
from itracker.common import config, phone_config
from itracker.server.gaze_predictor import GazePredictor
from itracker.server import server
# TODO (daniel) Logging to a file eventually.
logging.basicConfig(format="%(asctime)s %(levelname)s: %(message)s",
level=logging.INFO)
def demo(port):
""" A demo version of the server that displays received images on-screen, and
sends fake results back.
Args:
port: The port to run the demo server on. """
my_server = server.Server(port)
my_server.wait_for_client()
while True:
frame, sequence_num = my_server.read_next_frame()
if frame is None:
# Connection closed, wait for another.
cv2.destroyAllWindows()
my_server.wait_for_client()
continue
logging.info("Sequence number: %d" % (sequence_num))
# Send a response for a fake prediction.
my_server.send_response((0.0, 0.0), sequence_num)
cv2.imshow("test", frame)
cv2.waitKey(1)
def predict_forever(model_file, port, phone, display_crops=False):
""" Runs the server process and prediction pipeline forever.
Args:
model_file: The model file to use for predictions.
port: The port for the server to listen on.
phone: The configuration for the phone we are using.
display_crops: If true, it will display the crops for the images it
receives. Useful for debugging. """
predictor = GazePredictor(config.NET_ARCH, model_file, phone,
display=display_crops)
my_server = server.Server(port)
while True:
# Wait for a client to connect.
my_server.wait_for_client()
# Start server-handling processes.
receive_process = server.ReceiveProcess(my_server, predictor)
send_process = server.SendProcess(my_server, predictor)
# Wait for the client to diconnect.
receive_process.wait_for_disconnect()
send_process.wait_for_disconnect()
def main():
parser = argparse.ArgumentParser( \
description="Run the gaze estimation server.")
parser.add_argument("phone_config",
help="Path to configuration for phone we are using.")
parser.add_argument("-d", "--demo", action="store_true",
help="Run a demo server that displays received images.")
parser.add_argument("-c", "--display_crops", action="store_true",
help="Displays crops for the images it receives.")
args = parser.parse_args()
# Load the phone configuration.
phone = phone_config.PhoneConfig(args.phone_config)
if args.demo:
# Run the demo.
demo(config.SERVER_PORT)
else:
predict_forever(config.MODEL_FILE, config.SERVER_PORT, phone,
display_crops=args.display_crops)
if __name__ == "__main__":
main()