OpenCV与Blazor WASM在.NET6中可行吗?

fzsnzjdm  于 2023-10-24  发布在  .NET
关注(0)|答案(3)|浏览(169)

我想用Blazor WASM和OpenCV在客户端处理图像。我可以用OpenCVSharp4 NuGet Package服务器端运行OpenCV,但这不会削减它。如果我尝试让它在客户端运行,我得到:
未行程的例外状况:System. TypeInitializationException:"OpenCvSharp. Internal. NativeMethods"的类型初始值设定项引发异常。-> System. DllNotFoundException:OpenCvSharpExtern
我给它的另一个尝试是使用opencv.js,但如果我尝试用<script src="opencv.js" async></script>加载它,但它失败了,
错误:无法启动平台。原因:类型错误:模块._malloc不是St(blazor. webassembly. js:1)上的函数
未捕获TypeError:无法读取未定义的属性(阅读'apply')在模块. stackSave(_framework/dotnet.6.0.0.tj42mwroj7.js:1)在ccall(_framework/dotnet.6.0.0.tj42mwroj7.js:1)在Object. wasm_setenv(_framework/dotnet.6.0.0.tj42mwroj7.js:1)在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)
未捕获(在承诺中)TypeError:Cannot read properties of undefined(阅读"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
我考虑过在. NET 6中使用本机自定义,但我也不能让它工作。我错过了什么重要的东西吗?

mznpcxlj

mznpcxlj1#

使用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>
nwnhqdif

nwnhqdif2#

OpenCVSharp4目前使用OpenCvSharp4.runtime.wasm与Blazor WASM工作得很好。(类似的问题和答案在这里)
My OpenCVSharp4 Blazor WASM demo展示了图像上的基本Canny边缘检测和使用网络摄像头和视频的Haar Cascade面部和眼睛检测。GitHub上的实时演示代码库SpawnDev.BlazorJS.OpenCVSharp4
该演示使用ShimatOpenCVSharp4OpenCvSharp4.runtime.wasm Nuget包
Shimat's OpenCvSharp4 repo

wfveoks0

wfveoks03#

是,可行,请参阅以下项目文件

<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>

查看Sample code

相关问题