-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
213 lines (160 loc) · 5.85 KB
/
client.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import numpy as np
import socket
import config
import time
import datetime
import cv2
import argparse
import re
import rutil
from pynput.keyboard import Key, Listener, KeyCode
MAP_SIZE = config.GRID_MAP_SIZE
RGB_SIZE = (120, 160)
BYTES_MAP = MAP_SIZE[0] * MAP_SIZE[1] * 3
BYTES_HUM = MAP_SIZE[0] * MAP_SIZE[1] * 3
SCALE_FACTOR = 2
AUTO_SAVE_INTERVAL = 60
# Window names.
GRID_MAP_WINDOW = 'GRID_MAP_WINDOW'
HUMAN_MAP_WINDOW = 'HUMAN_MAP_WINDOW'
DRIVE_KEYS = ['w', 'a', 's', 'd']
input_cell = (2**32-1, 2**32-1)
input_key = 0
KEY_F1 = 190
KEY_F2 = 191
KEY_SPACE = 32
KEY_ESC = 27
def on_mouse(event, x, y, flags, param):
global input_cell
if event == cv2.EVENT_LBUTTONUP:
print('Clicked:', (y, x))
input_cell = (y // SCALE_FACTOR, x // SCALE_FACTOR)
def on_release(key):
global input_key
print('key:', key, type(key))
if type(key) == KeyCode:
if key.char in DRIVE_KEYS:
input_key = ord(key.char)
elif type(key) == Key:
if key == Key.space:
input_key = KEY_SPACE
elif key == Key.esc:
input_key = KEY_ESC
elif key == Key.f1:
input_key = KEY_F1
elif key == Key.f2:
input_key = KEY_F2
print('input key:', input_key)
if input_key == KEY_ESC:
return False
def save_map_image(map_image):
now = datetime.datetime.now()
year, month, day, hr, mn = now.year, now.month, now.day, now.hour,\
now.minute
filename = "./saves/{:04d}{:02d}{:02d}_{:02d}{:02d}.png".format(\
year, month, day, hr, mn)
img = cv2.cvtColor(map_image, cv2.COLOR_BGR2RGB)
cv2.imwrite(filename, img)
print(filename, 'saved.')
def on_exit():
print('Saving map data...')
np.save('./saves/' + rutil.now_file_name(postfix='.npy'), frame_array)
listener.join()
print('Exiting...')
##################################################
# Define arguments.
##################################################
parser = argparse.ArgumentParser(description='Client program for '\
'interacting with the on-line 2D map generated using SLAM.')
parser.add_argument('-a', '--ipv4', type=str,\
default='127.0.0.1', help='IPv4 address of the host.')
parser.add_argument('-p', '--port', type=int,\
default='9000', help='The port of the address to connect.')
parser.add_argument('-S', '--save-map-display', default=False,\
action='store_true',\
help='Save the map generation display during the current session as '\
'a numpy array.')
args = parser.parse_args()
##################################################
# Initialize.
##################################################
# Set the named window.
cv2.namedWindow(GRID_MAP_WINDOW)
cv2.setMouseCallback(GRID_MAP_WINDOW, on_mouse, None)
# Collect events.
listener = Listener(on_release=on_release)
# Save frames of the map display.
frame_array = []
##################################################
# Start.
##################################################
print('Starting listener...')
listener.start()
print('Waiting listener to get ready...')
listener.wait()
print('Listener is now ready.')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((args.ipv4, args.port))
f = config.MAP_SCALE_FACTOR
# cv2.imshow(GRID_MAP_WINDOW, np.full((30, 30), 0))
# cv2.waitKey(10)
try:
tstart = time.time()
is_conn = True
while True:
try:
data = b''
while len(data) != (BYTES_MAP + BYTES_HUM):
data += s.recv(4096)
# Receive array buffer containing the map images.
map_buf = np.frombuffer(data, np.uint8)[:BYTES_MAP]
map_img = map_buf.reshape(MAP_SIZE[1], MAP_SIZE[0], 3)
hum_buf = np.frombuffer(\
data, np.uint8)[BYTES_MAP:BYTES_MAP+BYTES_HUM]
hum_img = hum_buf.reshape(MAP_SIZE[1], MAP_SIZE[0], 3)
# Send input data.
snd = 0
if input_cell != 0:
y, x = input_cell
y_buff = y.to_bytes(4, byteorder='big')
x_buff = x.to_bytes(4, byteorder='big')
snd = y_buff + x_buff
else:
snd = bytes([255 for _ in range(8)])
snd += (input_key).to_bytes(4, byteorder='big')
s.sendall(snd)
if input_key == KEY_ESC:
break
if input_key == KEY_F1:
save_map_image(map_img)
if (time.time() - tstart) >= AUTO_SAVE_INTERVAL:
save_map_image(map_img)
tstart = time.time()
if args.save_map_display:
frame_array.append(map_img)
# Reset input.
input_cell = 0
input_key = 0
new_w = map_img.shape[1] * SCALE_FACTOR
new_h = map_img.shape[0] * SCALE_FACTOR
map_img = cv2.resize(map_img, (new_h, new_w),
interpolation=cv2.INTER_AREA)
hum_img = cv2.resize(hum_img, (new_h, new_w),
interpolation=cv2.INTER_AREA)
cv2.imshow(GRID_MAP_WINDOW, map_img)
# cv2.imshow(HUMAN_MAP_WINDOW, hum_img)
cv2.waitKey(100)
except socket.error:
print('Connection lost! Attempting to reconnect...')
is_conn = False
while not is_conn:
try:
s.connect((args.ipv4, args.port))
is_conn = True
print('Reconnection successful!')
except socket.error:
print('Trying again...')
time.sleep(2)
except KeyboardInterrupt:
pass
on_exit()