WSL2 calls and displays the RTSP video stream of the native camera through OpenCV

foreword

The origin of this blog is as shown above haha, WSL2 related installation tutorial can refer to my previous blog: Win11 install WSL2 and Nvidia driver

Welcome to my blog for more articles, there will be more technical details~

Install CMake

Please execute on ubuntu

sudo apt install cmake -y

or compile and install

# 以v3.25.1版本为例
git clone -b v3.25.1 https://github.com/Kitware/CMake.git 
cd CMake
# 你使用`--prefix`来指定安装路径,或者去掉`--prefix`,安装在默认路径。
./bootstrap --prefix=<安装路径> && make && sudo make install

# 验证
cmake --version

If an error is reported Could NOT find OpenSSL, install the following dependencies to solve it

sudo apt update
sudo apt upgrade
sudo apt install libssl-dev

Install OpenCV and FFmpeg

sudo apt install libopencv-dev
sudo apt install ffmpeg

Start Windows native RTSP video streaming

Download and unzip EasyDarwin

Easydarwin is an open source streaming media framework developed by a domestic team. It is developed based on the Go language. It has been created and developed since December 2012. It has expanded from the original single-service streaming media server form to the current cloud platform architecture open source project. It belongs to the high-performance open source RTSP streaming media server. On Github widely welcomed.

Features: RTSP push mode forwarding, RTSP pull mode forwarding, video recording, retrieval, playback, key frame cache, second open screen, RESTful interface, WEB background management, distributed load balancing.

Download and decompress the release package

Run EasyDarwin.exe directly

Stop the service with Ctrl + C. Open the browser and enter http://localhost:10008 to enter the control page. The default username and password are admin/admin

View native camera devices

Install ffmpeg locally on Windows

View the native camera device command as follows

ffmpeg -list_devices true -f dshow -i dummy

start streaming

ffmpeg -f dshow -i video="USB2.0 Camera" -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -rtsp_transport tcp -f rtsp rtsp://192.168.1.101/test

parameter explanation

  • -f dshow -i video="摄像头名称"Specifies to read the video stream from the local camera. Replace "Camera Name" with your camera name, for example "USB2.0 Camera".
  • -vcodec libx264Specifies to use the x264 encoder for video encoding.
  • -preset ultrafastSet the encoding speed. The fastest encoding speed is used here, but may result in lower video quality.
  • -tune zerolatencySet up the encoder for zero latency.
  • -f rtspSpecifies that the format of the output stream is RTSP.
  • rtsp://<IP地址>/<路径>Specifies the destination address of the RTSP stream. Please <IP地址>replace with the Windows native IP address and <路径>with the path you want to specify for the stream.

The successful streaming is as follows, pay attention to keep the console running

insert image description here

Open the local firewall (optional)

Because we want to access the RTSP video stream of this machine in WSL2, we need to open the firewall of this machine, as shown in the figure below

Turn off private and public networks

Receive video stream with OpenCV

code show as below

#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>

int main(int argc, char **argv)
{
    
    
    // Ubuntu安装ffmpeg:sudo apt-get install ffmpeg
    // rtsp地址,模拟四路视频流进行展示
    std::string rtsp1 = "rtsp://172.27.148.34/test";
    std::string rtsp2 = rtsp1;
    std::string rtsp3 = rtsp1;
    std::string rtsp4 = rtsp1;

    // CAP_FFMPEG:使用ffmpeg解码
    cv::VideoCapture stream1 = cv::VideoCapture(rtsp1, cv::CAP_FFMPEG);
    cv::VideoCapture stream2 = cv::VideoCapture(rtsp2, cv::CAP_FFMPEG);
    cv::VideoCapture stream3 = cv::VideoCapture(rtsp3, cv::CAP_FFMPEG);
    cv::VideoCapture stream4 = cv::VideoCapture(rtsp4, cv::CAP_FFMPEG);

    if (!stream1.isOpened() || !stream2.isOpened() || !stream3.isOpened() || !stream4.isOpened())
    {
    
    
        std::cout << "有视频流未打开" << std::endl;
        return -1;
    }

    cv::Mat frame1;
    cv::Mat frame2;
    cv::Mat frame3;
    cv::Mat frame4;

    cv::Mat H1, H2, V, blur;

    // 使用namedWindow创建窗口,WINDOW_AUTOSIZE:自动调整窗口大小
    cv::namedWindow("rtsp_demo", cv::WINDOW_AUTOSIZE);

    while (true)
    {
    
    
        if (!stream1.read(frame1) || !stream2.read(frame2) || !stream3.read(frame3) || !stream4.read(frame4))
        {
    
    
            std::cout << "有视频流未读取" << std::endl;
            continue;
        }
        // 缩放等处理
        cv::resize(frame1, frame1, cv::Size(500, 300));

        cv::resize(frame2, frame2, cv::Size(500, 300));
        cv::flip(frame2, frame2, 1);

        cv::resize(frame3, frame3, cv::Size(500, 300));
        cv::cvtColor(frame1, frame1, cv::COLOR_BGR2GRAY);
        cv::cvtColor(frame1, frame1, cv::COLOR_GRAY2BGR);

        cv::resize(frame4, frame4, cv::Size(500, 300));
        cv::putText(frame4, "RTSP demo", cv::Point(100, 100), cv::FONT_ITALIC, 1, cv::Scalar(0, 0, 255), 2);
        // 拼接
        cv::hconcat(frame1, frame2, H1);
        cv::hconcat(frame3, frame4, H2);
        cv::vconcat(H1, H2, V);

        // 高斯模糊一下
        cv::GaussianBlur(V, blur, cv::Size(25, 25), 0);

        cv::imshow("rtsp_demo", blur);

        if (cv::waitKey(1) == 27)
        {
    
    
            break;
        }
    }

    return 0;
}

The content of CMakeLists.txt is as follows

# 最低版本要求
cmake_minimum_required(VERSION 3.10)

# 项目信息
project(rtsp_demo)

# 添加opencv库
find_package(OpenCV REQUIRED)

# 添加头文件
include_directories(${OpenCV_INCLUDE_DIRS})
# 添加库文件
link_libraries(${OpenCV_LIBS})

# 添加可执行程序
add_executable(rtsp_demo src/main.cpp)

Start cmake configuration and build

cmake -S . -B build 
cmake --build build 

run executable

./build/rtsp_demo

Result display

Successfully used WSL2 to display four RTSP video streams~

Guess you like

Origin blog.csdn.net/Apple_Coco/article/details/129292953