【问题标题】:ASP.NET Core stream video from the OpenCVSharp capture来自 OpenCVSharp 捕获的 ASP.NET Core 流视频
【发布时间】:2019-08-15 18:34:26
【问题描述】:

我想使用 ASP.NET Core 应用程序流式传输从网络摄像头捕获的视频。 我还需要对框架进行一些操作,这就是我使用 OpenCVSharp 的原因。

目前我有接下来的发展:

  • html 在我看来-这里我不知道我应该使用什么类型
<video id="video" preload="auto">
  <source src="LiveVideo" type="<< don't know the type >>"/>
</video>
  • 我的控制器 - 这里我也不知道内容类型,主要问题是:我不知道如何流式传输 OpenCVSharp 捕获的视频
[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 >>);
  }
}

【问题讨论】:

    标签: opencv asp.net-core opencvsharp


    【解决方案1】:

    如果有人需要应用如此奇特的行为,我已经找到了一种方法。使用 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();
                }
            }
        }
    }
    

    【讨论】:

    • 您好,我对您的解决方案有 2 个问题: 1. 您为 opencv 使用了哪些 nuget 包? 2. 您构建的是 serverapp 还是 webassembly 应用程序?
    • 经过小幅调整后,它对我有用,但有一个问题:它访问服务器网络摄像头,我想访问客户端网络摄像头。
    • @StefanW。那么我认为服务器端 Blazor 不是你的选择。我认为您应该使用 Blazor Web Assembly 进行一些实验。
    猜你喜欢
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多