我正在使用 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/