javascript - 将 Blob 数据从 javascript 转换为在 python 中使用 opencv 读取

标签 javascript python numpy opencv blob

我正在使用 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 转换为图像数组。

最佳答案

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

这有效。

关于javascript - 将 Blob 数据从 javascript 转换为在 python 中使用 opencv 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53223785/

相关文章:

python - numpy 二进制光栅图像到多边形转换

javascript - 为什么在解码 URI 编码的 JSON 字符串时会收到错误 "unexpected end of string while parsing JSON string"?

javascript - Kendo ui Grid 内联编辑,带有 editorTemplate 的字段

javascript - 使用 PhantomJS 访问 HTML 中 JS 全局变量的值

python - 如何简化 numpy 条件语句来查找给定值的元素的相邻索引?

python - 在 numpy 数组的列内独立洗牌

javascript - 读取 xml 文件出了什么问题?

Python:如何 reshape 具有不同维度的多维数组?

python - 如何从较小的快照创建具有较大磁盘的计算实例?

python - 根据Python中另一个文件中的元素对文本文件进行排序