来自缓冲区优化的Python图像流

发布于 2025-02-12 07:14:21 字数 1280 浏览 0 评论 0原文

我正在使用有限的计算能力的嵌入式设备中的Python +烧瓶工作。

我想使用存储在设备上的像素阵列上的图像(RGBA)流式传输视频。 目前,我能够收到视频,但是总体性能不足以获得平稳的输出。

这是我已经进行的当前优化:

  • 通过读取内存区域
  • 删除颜色alpha,由于不使用颜色alpha。
  • 减少4个图像大小的
    from PIL import Image

    @bp.route("/stream")
    def stream():
        def loop():
            yield b"--frame\r\n"
            while True:
               raw_data = view[ frame_id * BUF_SIZE : (frame_id + 1) * BUF_SIZE ]
               image_np = np.frombuffer(np.asarray(raw_stream[::]),
                          dtype=np.uint8
                          ).reshape((600, 1024, 4))
               img_byte_arr = io.BytesIO()
               jpeg_img = Image.fromarray(image_np[::4, ::4, :3], mode="RGB")
               jpeg_img.save(img_byte_arr, format="JPEG")
               yield (
                  b"Content-Type: image/jpeg\r\n\r\n"
                  + img_byte_arr.getvalue()
                  + b"\r\n--frame\r\n"
               )
        return Response(loop(), mimetype="multipart/x-mixed-replace; boundary=frame")

基于某些测量的大小,似乎操作image.fromarray(image_np [:: 4,:: 4,:: 4,:3],mode =“ rgb”)是瓶颈由于执行(10fps)最高为0.2秒。

是否有另一种将数组转换为图像(JPEG或任何格式)可以加快过程的方法?也许是比PIL的另一个图书馆?

I am working on Python + flask in an embedded device with limited computing power.

I would like to stream a video, using images (RGBa) stored on the device as a pixels array.
Currently, I am able to receive the video but the overall performance is not sufficient to get a smooth output.

Here is the current optimization I already did :

  • reduce the number of copies by reading into the memory region
  • remove the color alpha since it is not used.
  • reduce by 4 the size of the image
    from PIL import Image

    @bp.route("/stream")
    def stream():
        def loop():
            yield b"--frame\r\n"
            while True:
               raw_data = view[ frame_id * BUF_SIZE : (frame_id + 1) * BUF_SIZE ]
               image_np = np.frombuffer(np.asarray(raw_stream[::]),
                          dtype=np.uint8
                          ).reshape((600, 1024, 4))
               img_byte_arr = io.BytesIO()
               jpeg_img = Image.fromarray(image_np[::4, ::4, :3], mode="RGB")
               jpeg_img.save(img_byte_arr, format="JPEG")
               yield (
                  b"Content-Type: image/jpeg\r\n\r\n"
                  + img_byte_arr.getvalue()
                  + b"\r\n--frame\r\n"
               )
        return Response(loop(), mimetype="multipart/x-mixed-replace; boundary=frame")

Based on some mesurement it seems that the operation Image.fromarray(image_np[::4, ::4, :3], mode="RGB") is the bottleneck since it tooks up to 0.2sec to be executed (10FPS).

Is there another way of converting an array to an image (JPEG or whatever format) that could speed up the process? Maybe another library than PIL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再见回来 2025-02-19 07:14:22

如果您可以访问GPU,我建议使用pytorch使用cuda保存和加载图像。您进行的转换(例如还原格式)在GPU上也并行化。加速可能很重要,如果仍然不够,则可以使用数据加载器来不保留所有图像。

If you have access to GPUs, I would recommend to use PyTorch using cuda to save and load your images. The transformations you make such as reducing format are also parallelized on GPU. The speedup may be significant, and if it is still not enough you can use a dataloader to not keep in memory all of your images.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文