首页 > 解决方案 > 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]

现在速度更快了。

我将用更大的机器进行另一次测试。

标签: pythongoogle-cloud-pubsub

解决方案


推荐阅读