提问人:Yadu S 提问时间:11/14/2023 最后编辑:YnjxsjmhYadu S 更新时间:11/19/2023 访问量:38
如何使用TCP套接字提高数据传输速度
How to improve Data transfer speed using TCP sockets
问:
此代码适用于服务器
import socket, cv2, pickle,struct,imutils
# Socket Create
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_name = socket.gethostname()
host_ip ='192.168.xx.xxx' #socket.gethostbyname(host_name)
print('HOST IP:',host_ip)
port = 1234
socket_address = (host_ip,port)
source = 'video.mp4'
# Socket Bind
server_socket.bind(socket_address)
# Socket Listen
server_socket.listen(5)
print("LISTENING AT:",socket_address)
# Socket Accept
while True:
client_socket,addr = server_socket.accept()
print('GOT CONNECTION FROM:',addr)
if client_socket:
vid = cv2.VideoCapture(source)
while(vid.isOpened()):
img,frame = vid.read()
a = pickle.dumps(frame)
message = struct.pack("Q",len(a))+a
client_socket.sendall(message)
cv2.imshow('TRANSMITTING VIDEO',frame1)
key = cv2.waitKey(1) & 0xFF
if key ==ord('q'):
client_socket.close()
这是客户端代码
import socket,cv2, pickle,struct
# create socket
client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_ip = '192.168.xx.xxx' # paste your server ip address here
port = 1234
client_socket.connect((host_ip,port)) # a tuple
data = b""
payload_size = struct.calcsize("Q")
while True:
while len(data) < payload_size:
packet = client_socket.recv(4096*4096) # 4K
if not packet: break
data+=packet
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack("Q",packed_msg_size)[0]
while len(data) < msg_size:
data += client_socket.recv(4096*4096)
frame_data = data[:msg_size]
data = data[msg_size:]
frame = pickle.loads(frame_data)
cv2.imshow("RECEIVING VIDEO",frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
client_socket.close()
我对此很陌生,我从 pyshine.com 那里得到了一个示例代码,我修改了该代码以满足我的需求。如果我使用此代码发送 2k 视频,它会占用大量时间,而如果我发送任何分辨率较低的视频,它会运行得更快。谁能帮忙,我怎样才能摆脱这个问题?
我尝试将缓冲区大小从 4096*4096 更改为更高的值,但没有多大帮助。我实际上想将 2k 视频从我的系统流式传输到另一个系统。
注意:作为最终目标,我想使用每个接收到的帧进行一些使用 Yolo 进行一些处理。我有一个函数,我需要进行处理。当我控制它时,它甚至很慢runYolo()
答: 暂无答案
评论
ffmpeg