gpt4 book ai didi

javascript - 将 openCV C++ 视频流式传输到浏览器

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:22:07 24 4
gpt4 key购买 nike

我正在尝试使用 C++ 中的 openCV 捕获我的内置网络摄像头,进行一些进动。到目前为止,这是有效的。

现在我想将网络摄像头流式传输到浏览器。我怎样才能做到这一点?

  • 我应该创建一个 WebSocket 吗?或者使用 UPD 套接字?
  • 如何在浏览器中显示该内容? HTML5 和 JS 有可能吗?

谢谢。

最佳答案

我可能来晚了一点,但由于我没有在 StackOverflow 中找到针对 C++ 和 mjpeg 的完全更新的解决方案,所以考虑写一个新的答案。

现在有一些用于 C++ 任务的好而简单的库(c++ mjpg 流式传输到 html)

https://github.com/nadjieb/cpp-mjpeg-streamer

https://github.com/jacksonliam/mjpg-streamer

https://github.com/codewithpassion/mjpg-streamer/tree/master/mjpg-streamer

我发现第一个非常简单。你需要CMake,并在系统中安装。

git clone https://github.com/nadjieb/cpp-mjpeg-streamer.git;
cd cpp-mjpeg-streamer;
mkdir build && cd build;
cmake ../;
make;
sudo make install;
  • 确保您安装了正确版本的 OpenCV。

现在,编写流光:

mjpeg_server.cc

#include <opencv2/opencv.hpp>

#include <nadjieb/mjpeg_streamer.hpp>

// for convenience
using MJPEGStreamer = nadjieb::MJPEGStreamer;

int main()
{
cv::VideoCapture cap;
cap.open("demo.mp4");
if (!cap.isOpened())
{
std::cerr << "VideoCapture not opened\n";
exit(EXIT_FAILURE);
}

std::vector<int> params = {cv::IMWRITE_JPEG_QUALITY, 90};

MJPEGStreamer streamer;

// By default 1 worker is used for streaming
// if you want to use 4 workers:
// streamer.start(8080, 4);
streamer.start(8000);

// Visit /shutdown or another defined target to stop the loop and graceful shutdown
while (streamer.isAlive())
{
cv::Mat frame;
cap >> frame;
if (frame.empty())
{
std::cerr << "frame not grabbed\n";
//continue;
exit(EXIT_FAILURE);
}

// http://localhost:8080/bgr
std::vector<uchar> buff_bgr;
cv::imencode(".jpg", frame, buff_bgr, params);
streamer.publish("/bgr", std::string(buff_bgr.begin(), buff_bgr.end()));

cv::Mat hsv;
cv::cvtColor(frame, hsv, cv::COLOR_BGR2HSV);

// http://localhost:8080/hsv
std::vector<uchar> buff_hsv;
cv::imencode(".jpg", hsv, buff_hsv, params);
streamer.publish("/hsv", std::string(buff_hsv.begin(), buff_hsv.end()));

// std::cout<< "published" << std::endl;
}

streamer.stop();
}

编写CMakeLists.txt

cmake_minimum_required(VERSION 3.1)

project(mjpeg_streamer CXX)

find_package(OpenCV 4.2 REQUIRED)
find_package(nadjieb_mjpeg_streamer REQUIRED)

include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(stream_test
"mjpeg_server.cc")
target_compile_features(stream_test PRIVATE cxx_std_11)
target_link_libraries(stream_test PRIVATE nadjieb_mjpeg_streamer::nadjieb_mjpeg_streamer
${OpenCV_LIBS})


| --- mjpeg_server.cc
| --- CMakeLists.txt
| --- ...
| --- build
| --- demo.mp4
| --- ...

现在,我们可以构建流媒体。

mkdir build && cd build;
cmake ../;
make;
./stream_test

现在,如果您转到 "http://ip_address:port/bgr""http://ip_address:port/hsv",您应该能够看到流。在我的例子中,ip = 192.168.1.7/localhost,port = 8000。

如果想用别的服务器抓流,

index.html

<html>
<body>
<img src="http://localhost:8000/bgr">
<img src="http://localhost:8000/hsv">
</body>
</html>

serve.py

import http.server
import socketserver

class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
self.path = 'index.html'
return http.server.SimpleHTTPRequestHandler.do_GET(self)

# Create an object of the above class
handler_object = MyHttpRequestHandler

PORT = 8080
my_server = socketserver.TCPServer(("", PORT), handler_object)

# Star the server
my_server.serve_forever()

python3 serve.py

最后,尽管它非常简单,但并不安全。

关于javascript - 将 openCV C++ 视频流式传输到浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35479211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com