1. 背景:
在ros系統中有發布攝像頭的相關驅動rgb數據,需求端需要將rgb數據可以直接在網頁上去顯示。
問題解決:
web_video_server功能包,相關鏈接:
web_video_server - ROS Wiki
2. 下載,安裝和編譯:
由于我們項目使用的是ros2系統,所以下載web video server在ros2的分支,可以根據自己的實際需求來下載:
git clone https://github.com/RobotWebTools/web_video_server.git
下載之后可以查看分支,并且編譯代碼:
cindy@ubuntu:~/web_video_server$ git branchdevelop
* ros2
cindy@ubuntu:~/web_video_server$ colcon build --packages-select web_video_server
Starting >>> web_video_server
--- stderr: web_video_server
CMake Error at CMakeLists.txt:6 (find_package):By not providing "Findasync_web_server_cpp.cmake" in CMAKE_MODULE_PATH thisproject has asked CMake to find a package configuration file provided by"async_web_server_cpp", but CMake did not find one.Could not find a package configuration file provided by"async_web_server_cpp" with any of the following names:async_web_server_cppConfig.cmakeasync_web_server_cpp-config.cmakeAdd the installation prefix of "async_web_server_cpp" to CMAKE_PREFIX_PATHor set "async_web_server_cpp_DIR" to a directory containing one of theabove files. If "async_web_server_cpp" provides a separate developmentpackage or SDK, be sure it has been installed.---
Failed <<< web_video_server [1.22s, exited with code 1]Summary: 0 packages finished [1.49s]1 package failed: web_video_server1 package had stderr output: web_video_server
cindy@ubuntu:~/web_video_server$
可以看到編譯的過程中無法找到async_web_server_cpp的數據包:
?Could not find a package configuration file provided by
? "async_web_server_cpp"
3. async_web_server_cpp的下載,安裝
后面要安裝async_web_server_cpp的相關服務包:
代碼下載:
git clone https://github.com/fkie/async_web_server_cpp.git
cindy@ubuntu:~/ros2_ws$ git clone https://github.com/fkie/async_web_server_cpp.git
Cloning into 'async_web_server_cpp'...
remote: Enumerating objects: 594, done.
remote: Counting objects: 100% (110/110), done.
remote: Compressing objects: 100% (64/64), done.
remote: Total 594 (delta 52), reused 76 (delta 35), pack-reused 484
Receiving objects: 100% (594/594), 113.65 KiB | 440.00 KiB/s, done.
Resolving deltas: 100% (358/358), done.
(1)首先,確保您已經安裝了git和cmake。如果沒有,請使用以下命令安裝:
sudo apt-get install git cmake
(2)進入async_web_server_cpp目錄并創建一個名為build的新目錄:
cd async_web_server_cpp
mkdir build
cd build
(3)使用cmake命令生成Makefile:
cmake ..
(4)編譯async_web_server_cpp:
make
?
(5)安裝async_web_server_cpp:
sudo make install
完成后,您應該能夠成功安裝async_web_server_cpp,并且可以在video web server中使用它。
4. 再次編譯web video server
cindy@ubuntu:~/web_video_server$ colcon build --packages-select web_video_server
Starting >>> web_video_server
--- stderr: web_video_server
/home/cindy/web_video_server/src/libav_streamer.cpp: In constructor ‘web_video_server::LibavStreamer::LibavStreamer(const async_web_server_cpp::HttpRequest&, async_web_server_cpp::HttpConnectionPtr, rclcpp::Node::SharedPtr, const string&, const string&, const string&)’:
/home/cindy/web_video_server/src/libav_streamer.cpp:65:55: warning: ‘int av_lockmgr_register(int (*)(void**, AVLockOp))’ is deprecated [-Wdeprecated-declarations]65 | av_lockmgr_register(&ffmpeg_boost_mutex_lock_manager);| ^
In file included from /home/cindy/web_video_server/include/web_video_server/libav_streamer.h:11,from /home/cindy/web_video_server/src/libav_streamer.cpp:1:
/usr/include/aarch64-linux-gnu/libavcodec/avcodec.h:6163:5: note: declared here6163 | int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op));| ^~~~~~~~~~~~~~~~~~~
/home/cindy/web_video_server/src/libav_streamer.cpp:65:55: warning: ‘int av_lockmgr_register(int (*)(void**, AVLockOp))’ is deprecated [-Wdeprecated-declarations]65 | av_lockmgr_register(&ffmpeg_boost_mutex_lock_manager);| ^
In file included from /home/cindy/web_video_server/include/web_video_server/libav_streamer.h:11,from /home/cindy/web_video_server/src/libav_streamer.cpp:1:
/usr/include/aarch64-linux-gnu/libavcodec/avcodec.h:6163:5: note: declared here6163 | int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op));| ^~~~~~~~~~~~~~~~~~~
/home/cindy/web_video_server/src/libav_streamer.cpp:66:19: warning: ‘void av_register_all()’ is deprecated [-Wdeprecated-declarations]66 | av_register_all();| ^
In file included from /home/cindy/web_video_server/include/web_video_server/libav_streamer.h:12,from /home/cindy/web_video_server/src/libav_streamer.cpp:1:
/usr/include/aarch64-linux-gnu/libavformat/avformat.h:2050:6: note: declared here2050 | void av_register_all(void);| ^~~~~~~~~~~~~~~
/home/cindy/web_video_server/src/libav_streamer.cpp:66:19: warning: ‘void av_register_all()’ is deprecated [-Wdeprecated-declarations]66 | av_register_all();| ^
In file included from /home/cindy/web_video_server/include/web_video_server/libav_streamer.h:12,from /home/cindy/web_video_server/src/libav_streamer.cpp:1:
/usr/include/aarch64-linux-gnu/libavformat/avformat.h:2050:6: note: declared here2050 | void av_register_all(void);| ^~~~~~~~~~~~~~~
/home/cindy/web_video_server/src/libav_streamer.cpp: In member function ‘virtual void web_video_server::LibavStreamer::initialize(const cv::Mat&)’:
/home/cindy/web_video_server/src/libav_streamer.cpp:160:35: warning: ‘AVStream::codec’ is deprecated [-Wdeprecated-declarations]160 | codec_context_ = video_stream_->codec;| ^~~~~
In file included from /home/cindy/web_video_server/include/web_video_server/libav_streamer.h:12,from /home/cindy/web_video_server/src/libav_streamer.cpp:1:
/usr/include/aarch64-linux-gnu/libavformat/avformat.h:885:21: note: declared here885 | AVCodecContext *codec;| ^~~~~
/home/cindy/web_video_server/src/libav_streamer.cpp:160:35: warning: ‘AVStream::codec’ is deprecated [-Wdeprecated-declarations]160 | codec_context_ = video_stream_->codec;| ^~~~~
In file included from /home/cindy/web_video_server/include/web_video_server/libav_streamer.h:12,from /home/cindy/web_video_server/src/libav_streamer.cpp:1:
/usr/include/aarch64-linux-gnu/libavformat/avformat.h:885:21: note: declared here885 | AVCodecContext *codec;| ^~~~~
/home/cindy/web_video_server/src/libav_streamer.cpp:160:35: warning: ‘AVStream::codec’ is deprecated [-Wdeprecated-declarations]160 | codec_context_ = video_stream_->codec;| ^~~~~
In file included from /home/cindy/web_video_server/include/web_video_server/libav_streamer.h:12,from /home/cindy/web_video_server/src/libav_streamer.cpp:1:
/usr/include/aarch64-linux-gnu/libavformat/avformat.h:885:21: note: declared here885 | AVCodecContext *codec;| ^~~~~
/usr/bin/ld: warning: libopencv_imgcodecs.so.4.2, needed by /opt/ros/foxy/lib/libcv_bridge.so, may conflict with libopencv_imgcodecs.so.4.5
/usr/bin/ld: warning: libopencv_imgproc.so.4.2, needed by /opt/ros/foxy/lib/libcv_bridge.so, may conflict with libopencv_imgproc.so.4.5
/usr/bin/ld: warning: libopencv_core.so.4.2, needed by /opt/ros/foxy/lib/libcv_bridge.so, may conflict with libopencv_core.so.4.5
---
Finished <<< web_video_server [20.5s]Summary: 1 package finished [20.8s]1 package had stderr output: web_video_server
可以發現已經可以正常編譯了
5.圖像顯示:
?編譯好之后可以直接使用可執行文件:
cindy@ubuntu:~/web_video_server$ ./build/web_video_server/web_video_server
[INFO] [1692002533.657719757] [web_video_server]: Waiting For connections on 10.141.5.154:8080
?打開ros2驅動攝像頭,就可以直接在網頁上看到相關的圖像了
相關用法和詳細配置:
web_video_server打開一個本地端口并等待傳入的HTTP請求。一旦通過HTTP請求ROS圖像主題的視頻流,它就會訂閱相應的主題并創建視頻編碼器的實例。編碼的原始視頻分組被提供給客戶端。可以通過將其他參數添加到查詢字符串中來指定參數。要使用瀏覽器連接到節點,請使用以下URL:
Available URLs
-
Overview of available ROS topics: http://localhost:8080/
-
Webpage showing a video stream: http://localhost:8080/stream_viewer?topic={ROS_TOPIC}
-
Video stream URL: http://localhost:8080/stream?topic={ROS_TOPIC}
-
Snapshot of next image: http://localhost:8080/snapshot?topic={ROS_TOPIC}
Several parameters can be configure via the video stream URL - Example: http://localhost:8888/stream?topic=depthcloud_encoded&bitrate=250000&type=vp8
?
Image Parameters
These are the parameters that can be used to configure a stream or snapshot:
width (integer, default: original width)
- The image stream will be resized to a new width and height. This parameter has to be used in conjunction with the height parameter.
height (integer, default: original height)
- The image stream will be resized to a new width and height. This parameter has to be used in conjunction with the height parameter.
quality (integer, default: 90)
- The jpeg image quality (1...100). This parameter can be used to reduce the size of the resulting stream.
invert (none, default: )
- Rotates the image by 180 degrees before streaming.
default_transport (string, default: "raw")
- The image transport to use (raw, compressed, theora).
Snapshot Parameters
These are the parameters that can be used to configure a snapshot:
quality (integer, default: 95)
- The jpeg image quality (1...100). This parameter can be used to reduce the size of the resulting stream.
Stream Parameters
These are the parameters that can be used to configure a stream:
type (string, default: mjpeg)
- The encoding method for the stream (mjpeg, vp8, ros_compressed, png, h264).
Additional parameters can be specified for each encoding type as described below.
Mjpeg Stream Parameters
These are the parameters that can be used to configure a mjpeg stream:
quality (integer, default: 95)
- The jpeg image quality (1...100). This parameter can be used to reduce the size of the resulting stream.
VP8 Stream Parameters
These are the parameters that can be used to configure a vp8 stream:
bitrate (integer, default: 100000)
- Maximum bitrate setting. Note that a small bitrate could significantly increase the latency due to a delayed transmission of larger intra frames.
qmin (integer, default: 10)
- Minimum quantizer.
qmax (integer, default: 42)
- Maximum quantizer.
gop (integer, default: 250)
- Keyframe interval, this determines the maximum distance between I-frames.
quality (string, default: realtime)
- The encoding quality.
More information on the quality and profile parameter of the VP8 codec can be found here: http://www.webmproject.org/docs/encoder-parameters
Unsupported customizations
Control image subscription rate
Rate at which web_video_server subscribes a image topic depends on the publisher's publish rate. With a fast publish rate, a client host may unintentionally get busy.
As of version 0.2.1, web_video_server does not come with a feature to control the rate at which the frontend subscribes to an image topic. You can work this around on your client.
One way is to republish the image topic with a lower rate. You can use throttle from topic_tools package.
ROS-Compressed Stream Example
rostopic list output:
$ rostopic list /usb_cam/camera_info /usb_cam/image_raw/compressed
View compressed stream at: http://localhost:8080/stream?topic=/usb_cam/image_raw&type=ros_compressed