.NET6 中带有 Blazor WASM 的 OpenCV 是否可行?
我想使用 Blazor WASM 和 OpenCV 在客户端处理图像。我可以使用 OpenCVSharp4 NuGet 包服务器端运行 OpenCV,但这行不通。如果我尝试让它在客户端上运行,我会得到:
Unhandled Exception: System.TypeInitializationException: The type initializer for 'OpenCvSharp.Internal.NativeMethods' threw an exception. ---> System.DllNotFoundException: OpenCvSharpExtern
我给出的另一个尝试是使用 opencv.js,但如果我尝试使用
<script src="opencv.js" async></script>
加载它,但它会失败
Error: Failed to start platform. Reason: TypeError: Module._malloc is not a function at St (blazor.webassembly.js:1)
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'apply') at Module.stackSave (_framework/dotnet.6.0.0.tj42mwroj7.js:1) at ccall (_framework/dotnet.6.0.0.tj42mwroj7.js:1) at Object.wasm_setenv (_framework/dotnet.6.0.0.tj42mwroj7.js:1) at Object.mono_wasm_setenv (_framework/dotnet.6.0.0.tj42mwroj7.js:1) at blazor.webassembly.js:1 at callRuntimeCallbacks (opencv.js:30) at postRun (opencv.js:30) at doRun (opencv.js:30) at run (opencv.js:30) at runCaller (opencv.js:30)
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'apply') at Object.Module._malloc (_framework/dotnet.6.0.0.tj42mwroj7.js:1) at Object.mono_wasm_load_bytes_into_heap (_framework/dotnet.6.0.0.tj42mwroj7.js:1) at blazor.webassembly.js:1
我考虑过在 .NET6 中使用本机依赖项,​​但我也无法让它工作。我是否遗漏了什么重要的东西?
使用 opencv-3.4.7.js 更高版本对我来说不起作用。并且在加载 blazor wasm 之前加载 opencv.js 文件。
<body>
<div id="app">Loading...</div>
<script src="/lib/opencv-3.4.7.js"></script>
<script src="_framework/blazor.webassembly.js"></script>
<script src="_content/MudBlazor/MudBlazor.min.js"></script>
<script>navigator.serviceWorker.register('service-worker.js');</script>
</body>
OpenCVSharp4 目前通过使用 OpenCvSharp4.runtime.wasm 与 Blazor WASM 配合良好。( 此处有类似的问题和答案 )
我的 OpenCVSharp4 Blazor WASM 演示 展示了图像上的基本 Canny 边缘检测以及使用网络摄像头和视频的 Haar Cascade 人脸和眼睛检测。 GitHub 上的实时演示仓库 SpawnDev.BlazorJS.OpenCVSharp4 。
该演示使用了 OpenCVSharp4 和 OpenCvSharp4.runtime.wasm Nuget 包,由 Shimat
Shimat 的 OpenCvSharp4 仓库
是的,这是可行的 请参阅下面的项目文件
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<WasmBuildNative>true</WasmBuildNative>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<RunAOTCompilation>True</RunAOTCompilation>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<RunAOTCompilation>True</RunAOTCompilation>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.1" PrivateAssets="all" />
<PackageReference Include="OpenCvSharp4" Version="4.5.5.20211231" />
<PackageReference Include="OpenCvSharp4.runtime.wasm" Version="4.5.5.20211231" />
</ItemGroup>
</Project>
查看 示例代码