-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpython_server.py
More file actions
40 lines (32 loc) · 1.25 KB
/
python_server.py
File metadata and controls
40 lines (32 loc) · 1.25 KB
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
"""
python_server.py
"""
import socket
import subprocess
import time
import json
# Set the absolute path to FaceLandmarkVid
FACELANDMARKVID_PATH = "/Users/danielkaijzer/Desktop/Cornell_AI_Hackathon/Focus_App/external_libs/openFace/OpenFace/build/bin/FaceLandmarkVid"
def start_gaze_server():
"""Starts the OpenFace gaze tracking process and returns a socket connection."""
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("127.0.0.1", 5005))
server_socket.listen(1)
print("Python server is listening on port 5005...")
# Ensure server is ready before starting OpenFace
time.sleep(2)
process = subprocess.Popen([FACELANDMARKVID_PATH, "-device", "0", "-gaze", "-pose", "-verbose"])
# Accept connection from C++ OpenFace process
client_socket, addr = server_socket.accept()
print(f"Connected to {addr}")
return client_socket, process
def read_gaze_data(client_socket):
"""Reads and returns gaze data from OpenFace."""
try:
data = client_socket.recv(1024).decode()
if not data:
return None
return json.loads(data) # Convert JSON string to Python dictionary
except json.JSONDecodeError:
print("Error decoding JSON")
return None