-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patht_server.py
More file actions
80 lines (71 loc) · 2.38 KB
/
t_server.py
File metadata and controls
80 lines (71 loc) · 2.38 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
# coding:utf8
import threading
import socket
import sys
import time
from modules.video.Processor import Processor
from modules.network.Communicator import Communicator
if int(sys.version[0]) > 2:
from queue import Queue
else:
from Queue import Queue
def communicate(communicator, frame):
while True:
# 1.为保证图像传输的实时性,每次通讯开始时清空图像帧队列
# frame.queue.clear()
# 1.发送超时验证用时间,编码长度为32
communicator.set_message(time.time()).encode(32).send()
# 2.获取主要数据长度
current_frame_len = communicator.recv(32)
# 3.获取主要数据
current_frame = communicator.recv(int(current_frame_len))
# 4.接收验证用时间
v_time = communicator.recv(32)
# 5.超时处理
delay = time.time() - float(v_time)
print('delay', delay)
if delay > 0.08:
communicator.set_message(0).encode(1).send()
else:
communicator.set_message(1).encode(1).send()
frame.put(current_frame)
communicator.set_message('ok').encode(1024).send()
def frame_process(frame, detector):
while True:
frame.queue.clear()
current_frame = frame.get()
current_frame = Processor.decode(current_frame)
import cv2
cv2.imshow('s', current_frame)
cv2.waitKey(10)
def thread_main():
# 监听地址
address = ('0.0.0.0', 1998)
#
detector = None
while True:
# 建立TCP/IP套接字
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(address)
# 最大挂起数量
sock.listen(5)
while True:
# 接受请求
conn, addr = sock.accept()
# 初始化通讯器
communicator = Communicator(addr, conn)
# 设置进程间通讯队列
# 图片队列
frame = Queue(maxsize=1)
# 收集线程
threads = [
# 通讯线程
threading.Thread(target=communicate, args=(communicator, frame,)),
# 图像帧处理线程
threading.Thread(target=frame_process, args=(frame, detector,)),
]
# 启动子线程
for thread in threads:
thread.start()
if __name__ == '__main__':
thread_main()