Pre Merge pull request !20 from AiYangSky/llc
This commit is contained in:
commit
e9972e88f8
|
@ -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)
|
||||
|
||||
|
|
|
@ -12,6 +12,9 @@
|
|||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h> // for sockaddr_in
|
||||
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
|
||||
#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<std::string>& files, std::string suff
|
|||
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
|
@ -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.setWH(1280, 720);
|
||||
cap.setFps(120);
|
||||
cap.open(sv::CameraType::G1);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1103,33 +1103,62 @@ 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<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 ret = false;
|
||||
if (this->_type == CameraType::WEBCAM || this->_type == CameraType::G1 || this->_type == CameraType::MIPI)
|
||||
{
|
||||
int n_try = 0;
|
||||
while (n_try < 5000)
|
||||
static int falseCount=0;
|
||||
std::lock_guard<std::mutex> locker(this->_frame_mutex);
|
||||
if(this->_frame_empty.wait_for(this->_frame_mutex,std::chrono::milliseconds(10)) == std::cv_status::timeout)
|
||||
{
|
||||
if (this->_is_updated)
|
||||
falseCount ++;
|
||||
if(falseCount >= 1000)
|
||||
{
|
||||
this->_is_updated = false;
|
||||
this->_frame.copyTo(image);
|
||||
break;
|
||||
throw std::runtime_error("SpireCV (101) Camera has offline, check CAMERA!");
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
||||
n_try ++;
|
||||
}
|
||||
else{
|
||||
falseCount = 0;
|
||||
}
|
||||
this->_frame.copyTo(image);
|
||||
ret = true;
|
||||
}
|
||||
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;
|
||||
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;
|
||||
}
|
||||
void CameraBase::release()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue