如何将图像帧相机传递给wasm (C++)中的函数?
How to pass image frames camera to a function in wasm (C++)?
我正在尝试构建一个C++函数并使用Emscripten将其编译为Wasm。
此函数将要做的是接收图像并对其执行一些处理并返回结果。
我的第一个 POC 成功,用户使用file
输入上传图像,我使用 API 传递图像的数据FileReader
:
const fileReader = new FileReader();
fileReader.onload = (event) => {
const uint8Arr = new Uint8Array(event.target.result);
passToWasm(event.target.result);
};
fileReader.readAsArrayBuffer(file); // I got this `file` from `change` event of the file input.
但是当我实现相机馈送并开始获取帧以将其传递给 Wasm 时,我开始C++端出现异常,这是 JS 实现:
let imageData = canvasCtx.getImageData(0, 0, videoWidth, videoHeight);
var data=imageData.data.buffer;
var uint8Arr = new Uint8Array(data);
passToWasm(uint8Arr);
这C++方面抛出了一个异常。
现在passToWasm
实现是:
function passToWasm(uint8ArrData) {
// copying the uint8ArrData to the heap
const numBytes = uint8ArrData.length * uint8ArrData.BYTES_PER_ELEMENT;
const dataPtr = Module._malloc(numBytes);
const dataOnHeap = new Uint8Array(Module.HEAPU8.buffer, dataPtr, numBytes);
dataOnHeap.set(uint8ArrData);
// calling the Wasm function
const res = Module._myWasmFunc(dataOnHeap.byteOffset, uint8ArrData.length);
}
虽然C++实现将是这样的:
void EMSCRIPTEN_KEEPALIVE checkImageQuality(uint8_t* buffer, size_t size) {
// I'm using OpenCV in C++ to process the image data
// So I read the data of the image
cv::Mat raw_data = cv::Mat(1, size, CV_8UC1, buffer);
// Then I convert it
cv::Mat img_data = cv::imdecode(raw_data, cv::IMREAD_COLOR | cv::IMREAD_IGNORE_ORIENTATION);
// in one of the following steps I'm using cvtColor function which causes the issue for some reason
}
由于相机实现,我得到的异常说:
OpenCV(4.1.0-dev) ../modules/imgproc/src/color.cpp:182:错误:(-215:断言失败)函数'cvtColor'中的!_src.empty()
使用file
输入和获取数据传递它,以及从canvas
获取数据,只要两者都将其转换为Uint8Array
我为此找到了解决方案(也许只适合我的情况)。
当您尝试从canvas
获取图像数据时,您可以将其作为 4 个通道(RGBA 如 PNG 中),并且根据您的图像处理代码,您需要处理它。
我的代码考虑到图像应该是 3 个通道(RGB 如 jpeg),所以我不得不使用以下代码进行转换:
canvasBuffer.toBlob(function (blob) {
passToWASM(blob);
},'image/jpeg');
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 如何在c++中为模板函数实例创建快捷方式
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗