optimize image get

This commit is contained in:
AiYangSky 2023-09-19 17:13:09 +08:00
parent 34f7d88ffa
commit d51037be4f
4 changed files with 71 additions and 3 deletions

View File

@ -285,6 +285,9 @@ target_link_libraries(EvalFpsOnVideo sv_world)
add_executable(GimbalTest samples/test/gimbal_test.cpp) add_executable(GimbalTest samples/test/gimbal_test.cpp)
target_link_libraries(GimbalTest sv_world) 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) add_executable(EvalModelOnCocoVal samples/test/eval_mAP_on_coco_val/eval_mAP_on_coco_val.cpp)
target_link_libraries(EvalModelOnCocoVal sv_world) target_link_libraries(EvalModelOnCocoVal sv_world)

View File

@ -12,6 +12,9 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/in.h> // for sockaddr_in #include <netinet/in.h> // for sockaddr_in
#include <mutex>
#include <condition_variable>
#define SV_RAD2DEG 57.2957795 #define SV_RAD2DEG 57.2957795
// #define X86_PLATFORM // #define X86_PLATFORM
// #define JETSON_PLATFORM // #define JETSON_PLATFORM
@ -358,7 +361,15 @@ protected:
void _run(); void _run();
bool _is_running; bool _is_running;
// new mutex
std::mutex _frame_mutex;
std::condition_variable_any _frame_empty;
//old flag
bool _is_updated; bool _is_updated;
std::thread _tt; std::thread _tt;
cv::VideoCapture _cap; cv::VideoCapture _cap;
cv::Mat _frame; cv::Mat _frame;
@ -397,3 +408,4 @@ void list_dir(std::string dir, std::vector<std::string>& files, std::string suff
} }
#endif #endif

View File

@ -0,0 +1,29 @@
#include <sv_world.h>
#include <iostream>
#include <string>
#include <chrono>
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::nanoseconds>(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::nanoseconds>(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;
}
}

View File

@ -1103,13 +1103,37 @@ void CameraBase::_run()
{ {
while (this->_is_running && this->_cap.isOpened()) while (this->_is_running && this->_cap.isOpened())
{ {
this->_cap >> this->_frame; // this->_cap >> this->_frame;
this->_is_updated = true; // this->_is_updated = true;
std::this_thread::sleep_for(std::chrono::milliseconds(2)); // std::this_thread::sleep_for(std::chrono::milliseconds(2));
if(this->_cap.grab())
{
std::lock_guard<std::mutex> 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 CameraBase::read(cv::Mat& image)
{ {
bool ret = false;
if (this->_type == CameraType::WEBCAM || this->_type == CameraType::G1 || this->_type == CameraType::MIPI)
{
std::lock_guard<std::mutex> 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) if (this->_type == CameraType::WEBCAM || this->_type == CameraType::G1 || this->_type == CameraType::MIPI)
{ {
int n_try = 0; int n_try = 0;