-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
116 lines (96 loc) · 2.94 KB
/
server.py
File metadata and controls
116 lines (96 loc) · 2.94 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
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
from __future__ import print_function, division
__import__('sys').dont_write_bytecode = True # prevent stupid pyc files
import socket as s
import time
import threading
from boltons.socketutils import BufferedSocket
import cv2
import json
from imageprocessor import process
from camera import Camera
from manualcontrol import ManualController
# Diagonal fov of webcam in degrees. X fov will be calculated with aspect ratio.
DIAG_FOV = 83
VISUALIZE_FEED = True
port = 4444
udp_port = 4445
serversocket = s.socket()
serversocket.bind(('0.0.0.0', port))
serversocket.listen(1)
def udp_listener_thread():
udp_sock = s.socket(s.AF_INET, s.SOCK_DGRAM)
udp_sock.bind(('0.0.0.0', udp_port))
print('Waiting for udp ping!')
msg, addr = udp_sock.recvfrom(256)
print('Recieved udp ping from {}!'.format(addr))
udp_sock.sendto(msg, addr)
def millis():
return round(time.time()*1000)
udp_thread = threading.Thread(target=udp_listener_thread, args=())
udp_thread.daemon = True
udp_thread.start()
print('Waiting for TCP connection...')
socket, addr = serversocket.accept()
print('Recieved TCP connection from {}!'.format(addr))
serversocket.close()
socket = BufferedSocket(socket)
class SimpleNamespace():
pass
last_frame = SimpleNamespace()
lock = threading.Lock()
def overwrite(message):
global last_message
try:
print('\r' + ''.join([' ']*len(last_message)), end='')
except:
pass
print('\r' + message, end='')
last_message = message
def communicate_thread():
try:
while True:
request = socket.recv_until(b'\n',timeout=None).decode('utf-8')
request = json.loads(request)
if request['id'] == 1:
lock.acquire()
response_json = json.dumps(last_frame.__dict__)
response = response_json + '\n'
lock.release()
if response != '{}\n':
overwrite(response_json)
socket.send(response.encode())
socket.flush()
elif request['id'] == -1:
break;
except:
pass
print('')
global running
running = False
vision = Camera(process, DIAG_FOV)
print('Camera resolution: %s' % str(vision.resolution()))
controller = ManualController()
img_thread = threading.Thread(target=communicate_thread)
img_thread.daemon = True
img_thread.start()
running = True
while True:
if not running:
break
if controller.manual_control():
lock.acquire()
last_frame = controller.get_frame()
lock.release()
else:
global img
frame, img = vision.get_vision_frame(visualize=VISUALIZE_FEED)
if frame is not None and img is not None:
lock.acquire()
last_frame = frame
lock.release()
if VISUALIZE_FEED:
cv2.imshow('Image',img)
cv2.waitKey(1)
vision.release()
controller.release()
cv2.destroyAllWindows()