python - 服务器端每秒接收的问题帧
问题描述
我有以下代码客户端将帧发送到服务器。我需要计算每秒从客户端发送和在服务器上接收的帧数的差异。在我当前的代码中,我让客户端每秒发送帧,这工作正常,目前,客户端正在发送 16 帧,服务器正在接收每秒 15 帧。但在服务器端,它没有计算每秒接收到的正确帧数。如果我将任务大小发送到 15,但如果我将其设置为大于它将接收更多,并且在服务器端的大部分时间我将每秒接收 0.0 帧。注意如果我删除异步并选择线程,我每秒检索 1 帧,这非常慢。非常感谢帮助谢谢
客户端
FPS = 25
context = zmq.Context()
server = context.socket(zmq.PUSH)
server.connect(SERVER_A_ADDRESS)
destination = {"currentSocket": server}
running = True
frame_requests = 0
def data_rate():
global destination, running, frame_requests
while running:
before_sent = frame_requests
time.sleep(1)
after_sent = frame_requests
print("{} ( i ) : send frames: {} per second.".format(strftime('%H:%M:%S'),round((after_sent - before_sent) / 1, 2)))
def send_frame(frame, frame_requests):
global destination, running
try:
frame = cv2.resize(frame, (224, 224))
encoded, buffer = cv2.imencode('.jpg', frame)
jpg_as_text = base64.b64encode(buffer)
destination["currentSocket"].send(jpg_as_text)
except Exception as Error:
print(Error)
running = False
def main():
global destination, running, frame_requests
interval = 1 / FPS
while running:
for img in filenames:
frame = cv2.imread(img)
frame_requests += 1
threading.Thread(target=send_frame, args=(frame, frame_requests)).start()
time.sleep(interval)
running=False
destination["currentSocket"].close()
if __name__ == "__main__":
threading.Thread(target=data_rate).start()
main()
服务器端
context = zmq.Context()
socket = context.socket(zmq.PULL)
dest_socket = context.socket(zmq.PUSH)
socket.bind(SERVER_ADDRESS)
dest_socket.connect(DEST_SERVER_ADDRESS)
tasks=20
running = True
count=1
async def forward_data(frame, count):
global m1, m2
source = cv2.imdecode(np.fromstring(base64.b64decode(frame), dtype=np.uint8), 1)
image = img_to_array(source)
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
preds = m1.predict(preprocess_input(image))
serialized_preds = pickle.dumps(preds)
compressed_preds = zlib.compress(serialized_preds)
dest_socket.send(compressed_preds)
def data_rate():
global count,running
while running:
before_recv = count
time.sleep(1)
after_recv = count
print("{} ( i ) : sending frames: {} per second.".format(time.strftime('%H:%M:%S'),round((after_recv - before_recv) /1 , 2)))
async def main():
global dest_socket,count
tasks = []
while True:
try:
frame = socket.recv_string()
count += 1
print("{} ( + ) : Received frame {} from {} successfully.".format(time.strftime('%H:%M:%S'),count,CLIENT_TITLE))
task = asyncio.ensure_future(forward_data(frame, count))
tasks.append(task)
if len(tasks) >= 20:
await asyncio.gather(*tasks, return_exceptions=True)
tasks.clear()
except Exception as error:
print(Error)
break
socket.close()
if __name__ == "__main__":
threading.Thread(target=data_rate).start()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
输出
#########################
19:41:25 ( i ) :frames: 20.0 per second.
#########################
19:41:25 ( + ) : SERVER B received array 2 successfully.
19:41:25 ( i ) : Sending array 3 to SERVER B...
19:41:26 ( + ) : SERVER B received array 3 successfully.
19:41:26 ( i ) : Sending array 4 to SERVER B...
#########################
19:41:26 ( i ) : frames: 0.0 per second.
#########################
19:41:26 ( + ) : SERVER B received array 4 successfully.
19:41:27 ( i ) : Sending array 5 to SERVER B...
#########################
19:41:27 ( i ) : frames: 0.0 per second.
#########################
解决方案
推荐阅读
- angular - Angular/RxJS 取消订阅先前的请求
- vb.net - VB.NET 中的 EF Core、LINQ 运算符“=”
- jenkins - 禁用 Jenkins 项目/作业有什么好处吗?
- javascript - How do i render a react component passed in as a props with additional props?
- php - 尝试正确调用 Alt 和标题标签
- python - 如何在 Django 项目中构建模板库?
- erlang - How to send body params in elixir using hackney
- c99 - why segmentation fault error in recursive function
- react-native - 是否有用于绘制实时时间序列数据折线图的 React-native 包?
- angular - how can i reset the child attributes of a container using renderer?