【问题标题】:Convert Blob data from javascript to read using opencv in python在python中使用opencv将Blob数据从javascript转换为读取
【发布时间】:2019-04-12 21:48:31
【问题描述】:

我正在使用 javascript 将视频从客户端发送到使用 web 套接字的后端服务器。我正在将视频转换为 blob 文件并发送它。下面给出了将图像转换为blob的函数。

function dataURItoBlob(dataURI) {
    // convert base64 to raw binary data held in a string
    var byteString = atob(dataURI.split(',')[1]);

    // separate out the mime component
    var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];

    // write the bytes of the string to an ArrayBuffer
    var arrayBuffer = new ArrayBuffer(byteString.length);
    var _ia = new Uint8Array(arrayBuffer);
    for (var i = 0; i < byteString.length; i++) {
        _ia[i] = byteString.charCodeAt(i);
    }
    var dataView = new DataView(arrayBuffer);
    var blob = new Blob([dataView], { type: mimeString });
    return blob;
}

现在,我需要将在 python 后端接收到的图像转换为 numpy 数组并使用 opencv 进行处理。如何在 python 中将 Blob 转换为图像数组。

【问题讨论】:

    标签: javascript python numpy opencv blob


    【解决方案1】:

    blob 数据将存储在 request.body 中(我使用 tornado 作为 web 应用程序框架),类型为 bytes

    arr = np.asarray( bytearray( request.body ,'utf-8'), dtype = np.uint8 )
    bgr_image = cv2.imdecode( arr, -1 ) # load it as it is
    

    这行得通。

    【讨论】:

    • 我仍然没有得到图像。 arr 给出 [195 191 195 ... 191 195 153] 并且 arr 的长度是 5718。所以 bgr_image 没有返回。我使用 aiortc 使用 webRTC 流式传输获得了不同的解决方案。这是完美的工作。
    猜你喜欢
    • 2020-12-28
    • 2013-02-21
    • 2018-05-30
    • 2016-06-10
    • 1970-01-01
    • 2014-10-02
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多