-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
375 lines (305 loc) · 11.7 KB
/
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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import socket
import struct
import pickle
import time
import signal
import sys
import threading
import queue
import move_servo
# 创建一个全局队列用于线程间通信
command_queue = queue.Queue()
def handle_exit(signum, frame):
print("\n正在清理资源并退出...")
if 'cap' in globals():
cap.release()
if 'server_socket' in globals():
server_socket.close()
if 'client_socket' in globals():
client_socket.close()
cv2.destroyAllWindows()
sys.exit(0)
def worker_thread():
global move_flag
move_flag = 0
print("工作线程启动")
while True:
try:
# 从队列中获取命令
if not command_queue.empty():
command = command_queue.get()
# 在这里处理命令或执行其他任务
# 根据命令执行不同的操作
if command == "0":
move_flag = 1
move_servo.nod()
command = "-1"
move_flag = 0
pass
elif command == "1":
move_flag = 1
move_servo.clip()
command = "-1"
move_flag = 0
pass
elif command == "2":
move_flag = 1
move_servo.wiggle()
command = "-1"
move_flag = 0
pass
elif command == "3":
move_flag = 1
move_servo.turned()
command = "-1"
move_flag = 0
pass
elif command == "4":
move_flag = 1
move_servo.rapper()
command = "-1"
move_flag = 0
pass
elif command == "5":
move_flag = 1
move_servo.bow()
command = "-1"
move_flag = 0
pass
time.sleep(0.1) # 防止CPU占用过高
except Exception as e:
print(f"工作线程错误: {e}")
def send_frame(client_socket, frame, max_retries=3):
# 调整图像大小为640x640
frame = cv2.resize(frame, (320, 320))
# 压缩图像以减少数据大小
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 60]
_, encoded_frame = cv2.imencode('.jpg', frame, encode_param)
data = pickle.dumps(encoded_frame)
try:
state_move = struct.pack("!Q", move_flag)
client_socket.sendall(state_move)
message_size = struct.pack("!Q", len(data))
client_socket.sendall(message_size)
chunk_size = 4096
for i in range(0, len(data), chunk_size):
chunk = data[i:i + chunk_size]
client_socket.sendall(chunk)
return True
except Exception as e:
print(f"发送失败: {e}")
return False
def send_video_stream(server_ip, server_port, retry_delay=5):
global server_socket, client_socket, cap, move_flag
# 启动工作线程
worker = threading.Thread(target=worker_thread, daemon=True)
worker.start()
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
server_socket.bind((server_ip, server_port))
server_socket.listen(2)
while True:
print(f"等待连接 {server_ip}:{server_port}")
client_socket, addr = server_socket.accept()
print(f"收到来自 {addr} 的连接")
try:
client_socket.settimeout(30.0)
cap = cv2.VideoCapture(1)
if not cap.isOpened():
raise Exception("无法打开视频捕获设备")
cap.set(cv2.CAP_PROP_FPS, 30)
params = pickle.dumps((320, 320))
param_size = struct.pack("!Q", len(params))
client_socket.sendall(param_size)
client_socket.sendall(params)
while True:
try:
client_socket.settimeout(0.001)
flag = client_socket.recv(1024).decode()
print(flag)
if flag and not move_flag:
# 将收到的标志放入队列,供工作线程处理
command_queue.put(flag)
# print(f"收到客户端标志: {flag.decode()}")
except socket.timeout:
pass
finally:
client_socket.settimeout(30.0)
ret, frame = cap.read()
if not ret:
print("无法获取视频帧")
break
if not send_frame(client_socket, frame):
break
time.sleep(1 / 20) # 30 FPS
except socket.timeout:
print("连接超时")
except ConnectionResetError:
print("连接被重置")
except BrokenPipeError:
print("连接断开")
except Exception as e:
print(f"发生错误: {e}")
finally:
if 'cap' in locals():
cap.release()
client_socket.close()
print(f"{retry_delay} 秒后重试...")
time.sleep(retry_delay)
finally:
server_socket.close()
if __name__ == "__main__":
# 注册信号处理
signal.signal(signal.SIGINT, handle_exit)
signal.signal(signal.SIGTERM, handle_exit)
SERVER_IP = "0.0.0.0" # 或指定 IP
SERVER_PORT = 11111 # 或指定端口
move_servo.servo_init()
try:
send_video_stream(SERVER_IP, SERVER_PORT)
except KeyboardInterrupt:
handle_exit(None, None)
if not command_queue.empty():
command = command_queue.get()
# 在这里处理命令或执行其他任务
# 根据命令执行不同的操作
if command == "0":
move_flag = 1
move_servo.nod()
command = "-1"
move_flag = 0
pass
elif command == "1":
move_flag = 1
move_servo.clip()
command = "-1"
move_flag = 0
pass
elif command == "2":
move_flag = 1
move_servo.wiggle()
command = "-1"
move_flag = 0
pass
elif command == "3":
move_flag = 1
move_servo.turned()
command = "-1"
move_flag = 0
pass
elif command == "4":
move_flag = 1
move_servo.rapper()
command = "-1"
move_flag = 0
pass
elif command == "5":
move_flag = 1
move_servo.bow()
command = "-1"
move_flag = 0
pass
time.sleep(0.1) # 防止CPU占用过高
except Exception as e:
print(f"工作线程错误: {e}")
def send_frame(client_socket, frame, max_retries=3):
# 调整图像大小为640x640
frame = cv2.resize(frame, (320, 320))
# 压缩图像以减少数据大小
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 60]
_, encoded_frame = cv2.imencode('.jpg', frame, encode_param)
data = pickle.dumps(encoded_frame)
# # 确保数据长度不超过预定义的最大值
# MAX_MESSAGE_SIZE = 256 * 256 # 1MB
# if len(data) > MAX_MESSAGE_SIZE:
# raise ValueError(f"数据大小 ({len(data)} bytes) 超过最大限制 ({MAX_MESSAGE_SIZE} bytes)")
try:
state_move = struct.pack("!Q", move_flag)
client_socket.sendall(state_move)
##################################################
for id in range(1, 7):
servo_data = struct.pack("!Q", move_servo.read_angle(id))
client_socket.sendall(servo_data)
####################################################
message_size = struct.pack("!Q", len(data))
client_socket.sendall(message_size)
chunk_size = 4096
for i in range(0, len(data), chunk_size):
chunk = data[i:i + chunk_size]
client_socket.sendall(chunk)
return True
except Exception as e:
print(f"发送失败: {e}")
return False
def send_video_stream(server_ip, server_port, retry_delay=5):
global server_socket, client_socket, cap, move_flag
# 启动工作线程
worker = threading.Thread(target=worker_thread, daemon=True)
worker.start()
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
server_socket.bind((server_ip, server_port))
server_socket.listen(2)
while True:
print(f"等待连接 {server_ip}:{server_port}")
client_socket, addr = server_socket.accept()
print(f"收到来自 {addr} 的连接")
try:
client_socket.settimeout(30.0)
cap = cv2.VideoCapture(0)
if not cap.isOpened():
raise Exception("无法打开视频捕获设备")
cap.set(cv2.CAP_PROP_FPS, 30)
params = pickle.dumps((320, 320))
param_size = struct.pack("!Q", len(params))
client_socket.sendall(param_size)
client_socket.sendall(params)
while True:
try:
client_socket.settimeout(0.001)
flag = client_socket.recv(1024).decode()
print(flag)
if flag and not move_flag:
# 将收到的标志放入队列,供工作线程处理
command_queue.put(flag)
# print(f"收到客户端标志: {flag.decode()}")
except socket.timeout:
pass
finally:
client_socket.settimeout(30.0)
ret, frame = cap.read()
if not ret:
print("无法获取视频帧")
break
if not send_frame(client_socket, frame):
break
time.sleep(1 / 20) # 30 FPS
except socket.timeout:
print("连接超时")
except ConnectionResetError:
print("连接被重置")
except BrokenPipeError:
print("连接断开")
except Exception as e:
print(f"发生错误: {e}")
finally:
if 'cap' in locals():
cap.release()
client_socket.close()
print(f"{retry_delay} 秒后重试...")
time.sleep(retry_delay)
finally:
server_socket.close()
if __name__ == "__main__":
# 注册信号处理
signal.signal(signal.SIGINT, handle_exit)
signal.signal(signal.SIGTERM, handle_exit)
SERVER_IP = "0.0.0.0" # 或指定 IP
SERVER_PORT = 11113 # 或指定端口
move_servo.servo_init()
try:
send_video_stream(SERVER_IP, SERVER_PORT)
except KeyboardInterrupt:
handle_exit(None, None)