python - PubSub - 发布视频帧的最有效方式?
问题描述
我有一个从视频源生成多个帧的服务(使用 OpenCV)
我接下来将这些帧编码为字节字符串并通过 Pub/Sub 批量发布
# Encoding each (opencv) frame
_, buffer = cv2.imencode('.jpg', frame)
pub_sub.publish(buffer.tobytes())
# PubSub class
class PubSub(object):
def __init__(self, project_id='xxx', topic_name='xxx'):
self.publisher = pubsub_v1.PublisherClient(
pubsub_v1.types.BatchSettings(max_latency=1)
)
self.topic_path = self.publisher.topic_path(project_id, topic_name)
def publish(self, frame_as_bytes):
future = self.publisher.publish(self.topic_path, data=frame_as_bytes)
print(future.result())
print('Published messages with batch settings.')
我的字节帧 = 大约 300kb 数据
但是这里的延迟非常大,我该如何改进这些操作?
更新:
好吧,看来opencv编码操作代价高昂:
_, buffer = cv2.imencode('.jpg', frame, encode_param)
我在我的笔记本电脑上运行该程序,所以我相信这对于具有更多 CPU 的 VM 不会有问题。
我添加了这些行以在编码之前减少图像重量:
frame = frame_helper.rescale_frame(frame, scale_percent=40)
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 80]
现在速度更快了。
我将用更大的机器进行另一次测试。
解决方案
推荐阅读
- node.js - 如何将 npm 版本升级到最新版本?Ubuntu显示安装成功但不升级版本
- r - 基于列中不同名称的子集矩阵数据
- codenameone - cn1中的LayerLayout
- c# - 太多独特的状态对象?
- regression - 任何人都可以用代码段来解释训练贝叶斯网络模型以进行回归的吗
- android - 枚举是否保留静态值,如果是,我该如何创建新实例/重置它?
- shell - 将 awk 行的输出打印到文件中
- android - Android 缩放级别回到静态缩放级别,相机每秒回到当前位置
- android - 搜索栏不更新
- response - 将值传递给 Apache Httpclient 响应处理程序