Python 利用socket从Pi向计算机发送图像时面临延迟
下面是与基本实现的链接 从安装在机箱/机器人上的客户端(Raspberry Pi),通过GPU(服务器)将从PiCam捕获的图像连续发送到计算机,使用套接字连接进行通信,并根据需要修改上述链接中使用的方法。在服务器端,基于返回的参数很少,进行了一些处理。根据在客户端接收到的这些参数,bot将采取相应的操作,因此时间至关重要。然而,im在从客户端向服务器发送图像时面临延迟问题 客户端(Pi)代码的一部分Python 利用socket从Pi向计算机发送图像时面临延迟,python,sockets,networking,raspberry-pi,raspberry-pi3,Python,Sockets,Networking,Raspberry Pi,Raspberry Pi3,下面是与基本实现的链接 从安装在机箱/机器人上的客户端(Raspberry Pi),通过GPU(服务器)将从PiCam捕获的图像连续发送到计算机,使用套接字连接进行通信,并根据需要修改上述链接中使用的方法。在服务器端,基于返回的参数很少,进行了一些处理。根据在客户端接收到的这些参数,bot将采取相应的操作,因此时间至关重要。然而,im在从客户端向服务器发送图像时面临延迟问题 客户端(Pi)代码的一部分 这仅仅是4字节长的数据,在少数情况下,它的计时从15-20ms到大多数情况下的90-130ms
这仅仅是4字节长的数据,在少数情况下,它的计时从15-20ms到大多数情况下的90-130ms,造成延迟,而整个过程实际上需要100ms奇数(不包括这一部分),我试图弄清楚为什么会发生这种情况?实际的图像数据是降采样灰度图像(160x120),读取每个图像也只需3-5毫秒,但不知道为什么我在读取图像长度时会遇到延迟。我也不熟悉套接字编程 试着简化代码的两面,找出问题所在。在发送端,删除所有图像捕获,只需重复发送一个已知的常量缓冲区,然后查看错误是否仍然存在。在接收端,只需读取消息长度,打印并读取消息,然后打印前10个字节,丢弃其余字节。试着在通信的每一端使用
socat
,因为这是已知的、经过测试和调试的,所以要花时间编写一个虚拟发送方和一个虚拟接收方。祝你好运。谢谢,我本来打算这么做的
def run_online_single(self, ip, port):
client_socket = socket.socket()
client_socket.connect((ip, port))
connection = client_socket.makefile('wb')
first_start = True
with picamera.PiCamera() as camera:
camera.resolution = (160, 120)
camera.framerate = 30
time.sleep(1)
stream = io.BytesIO()
for _ in camera.capture_continuous(stream, 'jpeg', use_video_port=True):
# start_time = time.time()
self.send_images(connection, stream)
self.recv_parameters(client_socket)
if first_start:
self.contorller.start()
first_start = False
# print('processed img ' + str(self.cur_img_id), time.time() - start_time)
connection.write(struct.pack('<L', 0))
connection.close()
client_socket.close()
def send_images(self, connection, stream):
""" Send images. Single thread, will block.
Helper function for online mode.
"""
connection.write(struct.pack('<L', stream.tell()))
connection.flush()
stream.seek(0)
connection.write(stream.read())
stream.seek(0)
stream.truncate()
def listen(self):
print('server: listening ...')
# try:
image_id = 0
while True:
new_img = self.recv_images()
if (new_img is None):
break
new_img = img_process.standard_preprocess(new_img, f=False, binary=False)
packaged_parameters = self.predict_and_fit(image_id, new_img)
packaged_parameters_with_id = np.concatenate(([image_id], packaged_parameters))
s_packaged_parameters = packaged_parameters_with_id.tobytes()
self.s.sendall(s_packaged_parameters)
image_id = image_id + 1
def recv_images(self):
""" Get image from the server.
@returns
return `None` if no more images
"""
buffer_len = struct.calcsize('<L')
#Delay Problem
timer = time()
buffer = self.connection.read(buffer_len)
print('reading image length {:0.5f} s'.format(time()-timer))
image_len = struct.unpack('<L', buffer)[0]
if not image_len:
print('server: receive None')
return None
# Construct a stream to hold the image data and read the image data from the connection
image_stream = io.BytesIO()
image_stream.write(self.connection.read(image_len))
image_stream.seek(0)
file_bytes = np.asarray(bytearray(image_stream.read()), dtype=np.uint8)
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
return image
buffer = self.connection.read(buffer_len)