[英]ASP.NET Core stream video from the OpenCVSharp capture
我想使用 ASP.NET 核心应用程序从网络摄像头捕获 stream 视频。 我还需要对框架进行一些操作,这就是我使用 OpenCVSharp 的原因。
目前我有下一个发展:
<video id="video" preload="auto">
<source src="LiveVideo" type="<< don't know the type >>"/>
</video>
[ApiController]
[Route("[controller]")]
public class LiveVideoController : ControllerBase
{
[HttpGet]
public async Task<FileStreamResult> GetVideo()
{
// capture frames from webcam
// https://github.com/shimat/opencvsharp/wiki/Capturing-Video
var capture = new VideoCapture(0);
var stream = await << somehow get the stream >>;
return new FileStreamResult(stream, << don't know the content type >>);
}
}
如果有人需要该应用程序如此奇特的行为,我已经找到了一种方法。 Blazor有可能。 我像读取字节数组一样读取每个帧,并将其发送到UI并将其转换为图像。
这是我的Blazor组件LiveVideo.razor
@page "/live-video"
@using OpenCvSharp;
<img src="@_imgSrc" />
@code {
private string _imgSrc;
protected override async Task OnInitializedAsync()
{
using (var capture = new VideoCapture(0))
using (var frame = new Mat())
{
while (true)
{
capture.Read(frame);
var base64 = Convert.ToBase64String(frame.ToBytes());
_imgSrc = $"data:image/gif;base64,{base64}";
await Task.Delay(1);
StateHasChanged();
}
}
}
}
试试这个参考下面的 Proj 文件
<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>
查看示例代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.