From d51037be4fe3ede5582bd0986d5a9e3d28222b9d Mon Sep 17 00:00:00 2001 From: AiYangSky <1732570904@qq.com> Date: Tue, 19 Sep 2023 17:13:09 +0800 Subject: [PATCH 1/3] optimize image get --- CMakeLists.txt | 3 +++ include/sv_video_base.h | 12 ++++++++++++ samples/test/camera_fps_test.cpp | 29 +++++++++++++++++++++++++++++ video_io/sv_video_base.cpp | 30 +++++++++++++++++++++++++++--- 4 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 samples/test/camera_fps_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b26f6d..72c399e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -285,6 +285,9 @@ target_link_libraries(EvalFpsOnVideo sv_world) add_executable(GimbalTest samples/test/gimbal_test.cpp) target_link_libraries(GimbalTest sv_world) +add_executable(CameraTest samples/test/camera_fps_test.cpp) +target_link_libraries(CameraTest sv_world) + add_executable(EvalModelOnCocoVal samples/test/eval_mAP_on_coco_val/eval_mAP_on_coco_val.cpp) target_link_libraries(EvalModelOnCocoVal sv_world) diff --git a/include/sv_video_base.h b/include/sv_video_base.h index f026ea3..687c87f 100644 --- a/include/sv_video_base.h +++ b/include/sv_video_base.h @@ -12,6 +12,9 @@ #include #include // for sockaddr_in +#include +#include + #define SV_RAD2DEG 57.2957795 // #define X86_PLATFORM // #define JETSON_PLATFORM @@ -358,7 +361,15 @@ protected: void _run(); bool _is_running; + +// new mutex + std::mutex _frame_mutex; + std::condition_variable_any _frame_empty; + +//old flag bool _is_updated; + + std::thread _tt; cv::VideoCapture _cap; cv::Mat _frame; @@ -397,3 +408,4 @@ void list_dir(std::string dir, std::vector& files, std::string suff } #endif + diff --git a/samples/test/camera_fps_test.cpp b/samples/test/camera_fps_test.cpp new file mode 100644 index 0000000..83c7878 --- /dev/null +++ b/samples/test/camera_fps_test.cpp @@ -0,0 +1,29 @@ + +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + sv::Camera cap; + cap.setIp(argv[1]); + cap.open(sv::CameraType::G1); + cap.setWH(1280, 720); + cap.setFps(30); + + cv::Mat img; + + while (1) + { + auto time1 = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + for (uint16_t i = 0; i < 120; i++) + { + cap.read(img); + } + auto time2 = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + auto Ts = time2 - time1; + std::cout << "read 120 count;Ts = " << Ts / (1000) << "us" << std::endl; + std::cout << "average FPS = " << (1000 * 1000 * 1000) / (Ts / 120) << std::endl; + } +} \ No newline at end of file diff --git a/video_io/sv_video_base.cpp b/video_io/sv_video_base.cpp index cb57fe8..ac727d5 100644 --- a/video_io/sv_video_base.cpp +++ b/video_io/sv_video_base.cpp @@ -1103,13 +1103,37 @@ void CameraBase::_run() { while (this->_is_running && this->_cap.isOpened()) { - this->_cap >> this->_frame; - this->_is_updated = true; - std::this_thread::sleep_for(std::chrono::milliseconds(2)); + // this->_cap >> this->_frame; + // this->_is_updated = true; + // std::this_thread::sleep_for(std::chrono::milliseconds(2)); + + if(this->_cap.grab()) + { + std::lock_guard locker(this->_frame_mutex); + this->_cap.retrieve(this->_frame); + this->_frame_empty.notify_all(); + } + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } } bool CameraBase::read(cv::Mat& image) { + bool ret = false; + if (this->_type == CameraType::WEBCAM || this->_type == CameraType::G1 || this->_type == CameraType::MIPI) + { + std::lock_guard locker(this->_frame_mutex); + if(this->_frame_empty.wait_for(this->_frame_mutex,std::chrono::milliseconds(2000)) == std::cv_status::no_timeout) + { + this->_frame.copyTo(image); + ret = true; + } + else + { + throw std::runtime_error("SpireCV (101) Camera cannot OPEN, check CAMERA_ID!"); + } + } + return ret; + if (this->_type == CameraType::WEBCAM || this->_type == CameraType::G1 || this->_type == CameraType::MIPI) { int n_try = 0; From 07f7cac7b7d36f7a4ba38aac9f9ad7daf5ec020b Mon Sep 17 00:00:00 2001 From: AiYangSky <1732570904@qq.com> Date: Tue, 19 Sep 2023 17:34:46 +0800 Subject: [PATCH 2/3] optimize image get --- samples/test/camera_fps_test.cpp | 4 ++-- video_io/sv_video_base.cpp | 40 ++++++++++++++++---------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/samples/test/camera_fps_test.cpp b/samples/test/camera_fps_test.cpp index 83c7878..d1b8622 100644 --- a/samples/test/camera_fps_test.cpp +++ b/samples/test/camera_fps_test.cpp @@ -8,9 +8,9 @@ int main(int argc, char *argv[]) { sv::Camera cap; cap.setIp(argv[1]); - cap.open(sv::CameraType::G1); cap.setWH(1280, 720); - cap.setFps(30); + cap.setFps(120); + cap.open(sv::CameraType::G1); cv::Mat img; diff --git a/video_io/sv_video_base.cpp b/video_io/sv_video_base.cpp index ac727d5..aaf716a 100644 --- a/video_io/sv_video_base.cpp +++ b/video_io/sv_video_base.cpp @@ -1134,26 +1134,26 @@ bool CameraBase::read(cv::Mat& image) } return ret; - if (this->_type == CameraType::WEBCAM || this->_type == CameraType::G1 || this->_type == CameraType::MIPI) - { - int n_try = 0; - while (n_try < 5000) - { - if (this->_is_updated) - { - this->_is_updated = false; - this->_frame.copyTo(image); - break; - } - std::this_thread::sleep_for(std::chrono::milliseconds(20)); - n_try ++; - } - } - if (image.cols == 0 || image.rows == 0) - { - throw std::runtime_error("SpireCV (101) Camera cannot OPEN, check CAMERA_ID!"); - } - return image.cols > 0 && image.rows > 0; + // if (this->_type == CameraType::WEBCAM || this->_type == CameraType::G1 || this->_type == CameraType::MIPI) + // { + // int n_try = 0; + // while (n_try < 5000) + // { + // if (this->_is_updated) + // { + // this->_is_updated = false; + // this->_frame.copyTo(image); + // break; + // } + // std::this_thread::sleep_for(std::chrono::milliseconds(20)); + // n_try ++; + // } + // } + // if (image.cols == 0 || image.rows == 0) + // { + // throw std::runtime_error("SpireCV (101) Camera cannot OPEN, check CAMERA_ID!"); + // } + // return image.cols > 0 && image.rows > 0; } void CameraBase::release() { From 0d3ba503e480be6ce721f1473e966842ec036ea1 Mon Sep 17 00:00:00 2001 From: AiYangSky <1732570904@qq.com> Date: Tue, 26 Sep 2023 03:25:00 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E7=A8=B3=E5=AE=9A=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E5=B8=A7=E7=8E=87=EF=BC=8C=E8=B0=83=E6=95=B4=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: AiYangSky <1732570904@qq.com> --- video_io/sv_video_base.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/video_io/sv_video_base.cpp b/video_io/sv_video_base.cpp index aaf716a..e98941f 100644 --- a/video_io/sv_video_base.cpp +++ b/video_io/sv_video_base.cpp @@ -1118,19 +1118,24 @@ void CameraBase::_run() } bool CameraBase::read(cv::Mat& image) { - bool ret = false; + bool ret = false; if (this->_type == CameraType::WEBCAM || this->_type == CameraType::G1 || this->_type == CameraType::MIPI) { + static int falseCount=0; std::lock_guard locker(this->_frame_mutex); - if(this->_frame_empty.wait_for(this->_frame_mutex,std::chrono::milliseconds(2000)) == std::cv_status::no_timeout) + if(this->_frame_empty.wait_for(this->_frame_mutex,std::chrono::milliseconds(10)) == std::cv_status::timeout) { - this->_frame.copyTo(image); - ret = true; + falseCount ++; + if(falseCount >= 1000) + { + throw std::runtime_error("SpireCV (101) Camera has offline, check CAMERA!"); + } } - else - { - throw std::runtime_error("SpireCV (101) Camera cannot OPEN, check CAMERA_ID!"); + else{ + falseCount = 0; } + this->_frame.copyTo(image); + ret = true; } return ret;