【问题标题】:Write numpy array to buffer as jpeg将 numpy 数组作为 jpeg 写入缓冲区
【发布时间】:2020-09-25 20:11:55
【问题描述】:

我有一个返回 np.uin8 数组流的程序。我现在想将这些广播到由该计算机托管的网站。

我计划通过将camera.start_recording(output, format='mjpeg') 行替换为output.write(<numpy_array_but_jpeg>) 来注入this 文档中的代码。 start_recording 的文档指出,如果存在 write() 方法,它将以请求的格式将数据写入该缓冲区。 我可以在网上找到很多关于如何将np.uint8 保存为 jpeg 的内容,但在我的情况下,我想将该数据写入内存中的缓冲区,并且我不想将图像保存到文件,然后将该文件读入缓冲区。

很遗憾,不能在流的前面更改np.uint8 的输出格式。

感谢您的帮助。为简单起见,我复制了下面的重要代码部分

class StreamingOutput(object):
def __init__(self):
    self.frame = None
    self.buffer = io.BytesIO()
    self.condition = Condition()

def write(self, buf):
    if buf.startswith(b'\xff\xd8'):
        # New frame, copy the existing buffer's content and notify all
        # clients it's available
        self.buffer.truncate()
        with self.condition:
            self.frame = self.buffer.getvalue()
            self.condition.notify_all()
        self.buffer.seek(0)
    return self.buffer.write(buf)

with picamera.PiCamera(resolution='640x480', framerate=24) as camera:
    output = StreamingOutput()
    camera.start_recording(output, format='mjpeg')

【问题讨论】:

  • 你的问题很难理解。您要“向网站广播”
  • 我正计划使用 PubSub 系统,其中程序的这一部分将是发布者,广播图像。反正我有解决办法。

标签: numpy raspberry-pi video-streaming streaming mjpeg


【解决方案1】:

OpenCV 具有执行此操作的功能

retval, buf = cv.imencode(ext,img[, params])

允许您将数组写入内存缓冲区。

这里的example 显示了我所说的基本实现。

【讨论】:

    猜你喜欢
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    相关资源
    最近更新 更多