来自OpenCVSharp捕获的ASP.NET Core流视频 [英] ASP.NET Core stream video from the OpenCVSharp capture

查看:925
本文介绍了来自OpenCVSharp捕获的ASP.NET Core流视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to stream video captured from the webcam using ASP.NET Core application. I also need to do some manipulations with the frames, that's why I'm using 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 >>);
      }
    }
    

    推荐答案

    如果某人需要该应用程序如此奇特的行为,我已经找到了一种方法. Blazor是可能的.我像读取字节数组一样读取每个帧,并将其发送到UI并将其转换为图像.

    If somebody needs so exotic behavior for the app, I've found a way. It is possible with Blazor. I read each frame like a bytes array and send it to the UI and convert it to the image.

    这是我的Blazor组件 LiveVideo.razor

    Here is my Blazor component 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();
                }
            }
        }
    }
    

    这篇关于来自OpenCVSharp捕获的ASP.NET Core流视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆